用 PowerShell 编写的 MyGet 预构建脚本?

MyGet pre-build script written in PowerShell?

我的预构建脚本最初是一个 .bat。我在添加生成版本号的功能时,是用PowerShell写的(我实在不喜欢batch)。因此,我尝试将整个预构建脚本转换为 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.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 构建日志(以及其他)中收到这些错误

Build : The term 'Build' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again.
At D:\temp\fcd4ee1\pre-build.ps1:24 char:1
+ Build "%programfiles(x86)%\MSBuild.0\Bin\MSBuild.exe" MakeMeATable ...
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (Build:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Verbosity=Normal' is not recognized as the name  of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At 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 预构建脚本的方法的任何好的参考?所有的例子都是class批写的!

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

  • Build : The term 'Build' is not recognized as the name of a cmdlet, function, script file, or operable program.

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

    The term 'Verbosity=Normal' is not recognized as the name of a cmdlet, function, script file, or operable program.

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

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

    将语句更改为如下内容:

    & "${ProgramFiles(x86)}\MSBuild.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.0\Bin\MSBuild.exe" @params
    
  • PowerShell 和批处理中的变量赋值不同。虽然 set config=Release 不会产生错误(因为 set 是 cmdlet Set-Variable 的内置别名),但它不会执行您期望的操作。它没有使用值 Release 定义变量 config,而是定义了具有空值的变量 config=Release

    改变

    set config=Release
    

    $config = 'Release'