简体   繁体   中英

MyGet pre-build script written in PowerShell?

My pre-build script originally was a .bat. When I added the functionality to generate the version number, I wrote it in PowerShell (I really don't like batch). So I tried to convert the whole pre-build script into PowerShell but I'm running into difficulties.

## 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%

I get these errors in the MyGet build log (among others)

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\14.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

I do not understand why I can't find any good reference about the way to create a PowerShell prebuild script for MyGet? All the examples are written in class batch!

  • Are there any resources online that I missed ?
  • What is the proper (official/most simple) way, in PowerShell, to build and package a library with MyGet?

I don't have experience with MyGet, but a can give you some advice on the errors and your code in general.

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

    The interpreter doesn't recognize Build as a command. Where is it defined? Is it an alias? Included somewhere? An executable in a different path? Also, your commandline doesn't look like it would be required in the first place. Try replacing it with the call operator ( & ).

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

    The semicolon in PowerShell has the same meaning as the ampersand in batch: it's the operator for daisy-chaining commands. Because of that PowerShell tries to execute Verbosity=Normal as a new statement instead of handling it as part of the argument for the parameter /flp . Put the parameter in quotes to avoid this.

    Also, you cannot use CMD builtin variables ( %cd% ) or batch variable syntax in general ( %var% ) in PowerShell. The equivalent for %cd% is $pwd (more specifically $pwd.Path ), and the general syntax for variables is $var or ${var} (the latter must be used if the variable name contains non-word characters like spaces, parentheses, etc.).

    Change the statement to something like this:

     & "${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 

    and it should work. Better yet, use splatting for passing the arguments:

     $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 
  • Variable assignments in PowerShell and batch are different. While set config=Release won't produce an error (because set is a builtin alias for the cmdlet Set-Variable ) it doesn't do what you expect. Instead of defining a variable config with the value Release it defines a variable config=Release with an empty value.

    Change

     set config=Release 

    to

     $config = 'Release' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM