0

我的预构建脚本最初是一个 .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 构建和打包库的正确(官方/最简单)方法是什么?
4

1 回答 1

1

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

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

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

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

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

    此外,您不能在 PowerShell中使用 CMD 内置变量 ( %cd%) 或一般的批处理变量语法 ( )。%var%等价于%cd%is $pwd(更具体地说$pwd.Path),变量的一般语法$varor ${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),但它并没有达到您的预期。它不是config用值定义变量,而是用空值Release定义变量。config=Release

    改变

    set config=Release
    

    $config = 'Release'
    
于 2017-08-23T08:36:59.337 回答