【问题标题】:MyGet pre-build script written in PowerShell?用 PowerShell 编写的 MyGet 预构建脚本?
【发布时间】:2017-08-23 16:36:59
【问题描述】:

我的预构建脚本最初是一个 .bat。当我添加生成版本号的功能时,我在 PowerShell 中编写了它(我真的不喜欢批处理)。所以我尝试将整个预构建脚本转换为 PowerShell,但我遇到了困难。

## pre-build.ps1
function SetPackageVersion
{
    if(gitversion /output json /showvariable PreReleaseTagWithDash) 
    {
       ##myget[buildNumber "$(gitversion /output json /showvariable Major).$(gitversion /output json /showvariable Minor).$(gitversion /output json /showvariable CommitsSinceVersionSource)-$(gitversion /output json /showvariable PreReleaseTagWithDash)"] 
    } 
    else 
    {
       ##myget[buildNumber "$(gitversion /output json /showvariable Major).$(gitversion /output json /showvariable Minor).$(gitversion /output json /showvariable CommitsSinceVersionSource)"] 
    } 

    GitVersion /updateassemblyinfo true
}

set config=Release

SetPackageVersion

## Package restore
NuGet restore Library\packages.config -OutputDirectory %cd%\packages -NonInteractive

Build "%programfiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe" MakeMeATable.sln /p:Configuration="%config%" /m /v:M /fl /flp:LogFile=msbuild.log;Verbosity=Normal /nr:false

## Package
mkdir Build
nuget pack "Library\MakeMeATable.csproj" -symbols -o Build -p Configuration=%config% %version%

我在 MyGet 构建日志(以及其他)中收到这些错误

构建:术语“构建”未被识别为 cmdlet、函数的名称, 脚本文件或可运行的程序。检查名称的拼写,或者如果路径 已包含,请验证路径是否正确,然后重试。 在 D:\temp\fcd4ee1\pre-build.ps1:24 char:1 + 构建 "%programfiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe" MakeMeATable ... + ~~~~~ + CategoryInfo : ObjectNotFound: (Build:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException 术语 'Verbosity=Normal' 未被识别为 cmdlet、函数的名称, 脚本文件或可运行的程序。检查名称的拼写,或者如果路径 已包含,请验证路径是否正确,然后重试。 在 D:\temp\fcd4ee1\pre-build.ps1:24 char:140 + ... onfig%" /m /v:M /fl /flp:LogFile=msbuild.log;Verbosity=Normal /nr:fal ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Verbosity=Normal:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException

我不明白为什么我找不到任何关于为 MyGet 创建 PowerShell 预构建脚本的方法的好的参考资料?所有的例子都是类批写的!

  • 是否有我错过的在线资源?
  • 在 PowerShell 中,使用 MyGet 构建和打包库的正确(官方/最简单)方法是什么?

【问题讨论】:

    标签: powershell myget


    【解决方案1】:

    我没有使用 MyGet 的经验,但我可以就错误和一般代码向您提供一些建议。

    • Build:术语“Build”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

      解释器无法将Build 识别为命令。它在哪里定义?是别名吗?包含在某处?不同路径中的可执行文件?此外,您的命令行看起来并不像一开始就需要它。尝试将其替换为 call operator (&)。

      术语“Verbosity=Normal”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

      PowerShell 中的分号与批处理中的 & 符号含义相同:它是菊花链命令的运算符。因此,PowerShell 尝试将 Verbosity=Normal 作为新语句执行,而不是将其作为参数 /flp 的参数的一部分进行处理。将参数放在引号中以避免这种情况。

      此外,您不能在 PowerShell 中使用 CMD 内置变量 (%cd%) 或一般的批处理变量语法 (%var%)。 %cd% 的等价物是$pwd(更具体地说是$pwd.Path),general syntax for variables$var${var}(如果变量名包含空格、括号等非单词字符,则必须使用后者等)。

      把语句改成这样:

      & "${ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild.exe" MakeMeATable.sln "/p:Configuration=$config" /m /v:M /fl "/flp:LogFile=msbuild.log;Verbosity=Normal" /nr:false
      

      它应该可以工作。更好的是,使用splatting 传递参数:

      $params = 'MakeMeATable.sln', "/p:Configuration=$config", '/m', '/v:M',
                '/fl', '/flp:LogFile=msbuild.log;Verbosity=Normal', '/nr:false'
      & "${ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild.exe" @params
      
    • PowerShell 和批处理中的变量分配不同。虽然set config=Release 不会产生错误(因为set 是cmdlet Set-Variable 的内置别名),但它并没有达到您的预期。不是用值Release 定义变量config,而是用空值定义变量config=Release

      改变

      set config=Release
      

      $config = 'Release'
      

    【讨论】:

    • 感谢您的宝贵时间。我完全重写了剧本。出于某种原因,我假设 PowerShell 将能够执行 Batch(或几乎),而它们实际上在语法方面完全不同。我遇到了其他问题,但您确实回答了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2018-07-19
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多