2

I have the following PowerShell script being run on my build server

Write-Host "Current Path  $env:Agent_BuildDirectory"
Write-Host "Build Number  $env:Build_BuildNumber"
$squirrel = "$env:BUILD_SOURCESDIRECTORY\packages\squirrel.windows.*\tools\Squirrel.exe"
$releaseDir = '.\Releases'
$nugetPackFile = ".\MyApp\MyApp.$env:Build_BuildNumber.nupkg"
Write-Host $squirrel
Write-Host $nugetPackFile

if((Test-Path $nugetPackFile) -and (Test-Path $squirrel)) {
    $squirrelArg1 = '--releasify=' + $nugetPackFile
    $squirrelArg2 = '--releaseDir=' + $releaseDir
    & $squirrel $squirrelArg1 $squirrelArg2 
}

It runs and it creates only a nupkg in the .\Releases folder. If I run the same --releasify command in the Visual Studio instance on my build server agent it creates all the setup.exe and Releases file. Why is this PowerShell script not working the same way the command being run in the NuGet PowerShell window in VS is?

sodawillow
  • 12,497
  • 4
  • 34
  • 44
PlTaylor
  • 7,345
  • 11
  • 52
  • 94

2 Answers2

2

I've not played with Squirrel, so this may not work; but too much code here to just submit as a comment...

Try this:

Write-Host "Current Path  $env:Agent_BuildDirectory"
Write-Host "Build Number  $env:Build_BuildNumber"
$squirrel = Get-Item (Join-Path $env:BUILD_SOURCESDIRECTORY "packages\squirrel.windows.*\tools\Squirrel.exe") | select -First 1 -Expand FullName
$releaseDir = '.\Releases'
$nugetPackFile = ".\MyApp\MyApp.$env:Build_BuildNumber.nupkg"
Write-Host $squirrel
Write-Host $nugetPackFile

if((Test-Path $nugetPackFile) -and (Test-Path $squirrel)) {
    $squirrelArg1 = "--releasify=`"$nugetPackFile`""
    $squirrelArg2 = "--releaseDir=`"$releaseDir`""
    & $squirrel $squirrelArg1 $squirrelArg2 
}

Getting Squirrel.exe Path

  • (Join-Path $env:BUILD_SOURCESDIRECTORY "packages\squirrel.windows.*\tools\Squirrel.exe") - here I use Join-Path to avoid any issues around whether or not the value of $env:BUILD_SOURCESDIRECTORY ends in a backslash.
  • Get-Item - I put this before that path so that it will resolve the path to a valid path (i.e. working out any matches of the asterisk/wildcard).
  • | select -First 1 -Expand FullName I then add this to get the first path which matches the result, and to return the full file path to squirrel.exe

Passing Parameters

For the statements below, I added double quotes around the paths; sometimes this is required to clarify which argument they relate to; particularly if there are any spaces or special characters in the paths. I also switched from using + to putting the variable within double quotes as this makes it simpler to concatenate the quotes within the string. I used backticks on the quotes in the string to escape those characters.

  • $squirrelArg1 = "--releasify=`"$nugetPackFile`""
  • $squirrelArg2 = "--releaseDir=`"$releaseDir`""

Hope that helps, but sadly this is very much guesswork by me; sorry.


Update

Getting the latest version; assuming asterisk in the path packages\squirrel.windows.*\tools\Squirrel.exe represents the version number in the form: Major.Minor.Build.

$squirrel = Get-Item (Join-Path $env:BUILD_SOURCESDIRECTORY "packages\squirrel.windows.*\tools\Squirrel.exe") | %{
    if ($_ -match '.*\\squirrel\.windows\.(?<Major>\d+)\.(?<Minor>\d+)\.(?<Build>\d+)\\tools\\Squirrel\.exe') {
        (new-object -TypeName PSObject -Property $matches)
    }
} | sort @{e={$_.Major};a=0}, @{e={$_.Minor};a=0}, @{e={$_.Build};a=0} | select -First 1 -ExpandProperty '0'
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • 1
    Sorry for the delayed response, I got caught up and then tired. I am trying this now. One question about the 'First' command....would it be possible to do a 'Last' command so a folder with a higher version number would be selected? – PlTaylor Feb 10 '17 at 12:47
  • 1
    These changes appear to have accomplished the same thing on the build server as my code did. They are an upgrade, but don't solve my problem with squirrel. – PlTaylor Feb 10 '17 at 13:03
  • 1
    Hey @PlTaylor; sorry to hear this didn't resolve your issue. Regarding `-Last`; absolutely that will do what you want. i.e. `1..10 | select -Last 1` returns `10`. – JohnLBevan Feb 10 '17 at 13:44
  • would Last be able to tell the difference between version numbers like 1.20, 1.58, 2.10 for example. Just covering my bases as I am not that familiar with powershell. – PlTaylor Feb 10 '17 at 13:45
  • 1
    Good point; it would sort things in string order; so with the above examples all would be OK as they're the same in string & numeric order; but there could be scenarios where that doesn't work; i.e. `1, 10, 11, 2, 3` is sorted in alphanumeric order rather than numeric order. There's probably a workaround if there's a consistent format for these strings; i.e. use a regex to pull out the version number, convert to a number, then sort it; but would require a tweak... Will take a look this evening to find out what Squirrel paths look like... – JohnLBevan Feb 10 '17 at 16:07
  • 1
    Still a lot more likely to do what I actually want than any other method :) Just trying to line up possible causes of failures. – PlTaylor Feb 10 '17 at 16:08
  • 1
    Updated to include logic for sorting by version number. – JohnLBevan Feb 10 '17 at 23:09
1

I found the answer here

Write-Host "Current Path  $env:Agent_BuildDirectory"
Write-Host "Build Number  $env:Build_BuildNumber"

$squirrel = Get-Item (Join-Path $env:BUILD_SOURCESDIRECTORY "packages\squirrel.windows.*\tools\Squirrel.exe") | %{
if ($_ -match '.*\\squirrel\.windows\.(?<Major>\d+)\.(?<Minor>\d+)\.(?<Build>\d+)\\tools\\Squirrel\.exe') {
    (new-object -TypeName PSObject -Property $matches)
    }
} | sort @{e={$_.Major};a=0}, @{e={$_.Minor};a=0}, @{e={$_.Build};a=0} | select -First 1 -ExpandProperty '0'

Set-Alias Squirrel $squirrel

$releaseDir = '.\Releases'
$nugetPackFile = ".\MyApp\MyApp.$env:Build_BuildNumber.nupkg"
Write-Host $squirrel
Write-Host $nugetPackFile

if((Test-Path $nugetPackFile) -and (Test-Path $squirrel)) {
    Squirrel --releasify $nugetPackFile --releaseDir $releaseDir | Write-Output
}

Much thanks goes to @JohnLBevan for helping to fix up my powershell code.

PlTaylor
  • 7,345
  • 11
  • 52
  • 94