Psake ignores properties and parameters

Using psake 4.5.0 I tried to run my default.ps1 file in different ways, passing both -properties and -parameters , but these values ​​are simply ignored in the root area of ​​the .ps1 script.

Perform relative (using psake in a subfolder) :

 .\psake-4.5.0\psake.ps1 .\default.ps1 BuildSolution -properties @{"a"="a";"b"="b";"c"="c";"d"="d"} -parameters @{"w"="w";"x"="x";"y"="y";"z"="z"} 

Run with the imported module :

 Import-Module .\psake-4.5.0\psake.psm1 Invoke-Psake .\default.ps1 BuildSolution -properties @{"a"="a";"b"="b";"c"="c";"d"="d"} -parameters @{"w"="w";"x"="x";"y"="y";"z"="z"} 

Run through the installed Chocolatey package :

 psake .\default.ps1 BuildSolution -properties "@{'a'='a';'b'='b';'c'='c';'d'='d'}" -parameters "@{'w'='w';'x'='x';'y'='y';'z'='z'}" 

Run through cmd.exe :

 psake-4.5.0\psake.cmd default.ps1 BuildSolution -properties "@{'a'='a';'b'='b';'c'='c';'d'='d'}" -parameters "@{'w'='w';'x'='x';'y'='y';'z'='z'}" 

Currently, default.ps1 just simply debugs all of these values:

 // Since "properties" doesn't get populated, I also try "Param" Param( $w = $w, // Trying to populate from passed param $x = $null, // Trying to default to null-value $y // Trying another syntax, hoping for population // "$z" left out, hoping for population ) properties { $a = $a $b = $null $c } Write-Host "a: $a" Write-Host "b: $b" Write-Host "c: $c" Write-Host "d: $d" Write-Host "w: $w" Write-Host "x: $x" Write-Host "y: $y" Write-Host "z: $z" Task BuildSolution -Depends Clean { Write-Host "Running BuildSolution" } Task Clean { Write-Host "Running Clean" } 

The output in all cases:

 a: b: c: d: w: x: y: z: 

What is the correct syntax for passing properties and / or parameters to psake?

+6
source share
1 answer

I have not looked into this for too long, but it seems that while you are declaring the variable $ in the properties {... here ...} or in Param (... here ...), so that psake can fill them, they come although everything is in order. You missed $ z. If they lack something fundamental in why do you use parameters and properties together?

default.ps1

 properties { $a = $null, $b = $null, $c = $null, $w = $null, $x = $null, $y = $null, $z = $null } Task default -Depends BuildSolution Task BuildSolution -Depends Clean { Write-Host "Running BuildSolution" echo "x -> $x" echo "y -> $y" echo "z -> $z" } Task Clean { Write-Host "Running Clean" echo "a -> $a" echo "b -> $b" echo "c -> $c" } 

call a sample code (psake installed via chocolate to c :)

 Import-Module C:\ProgramData\chocolatey\lib\psake\tools\psake.psm1 Invoke-Psake .\default.ps1 BuildSolution -properties @{'a'="a";"b"="b";"c"="c";'x'="x";'y'="y";'z'="z"} 

EDIT:

This is the Properties function Line 256 in psake.psm1, noticing that it takes the arguments of the script block and adds them to the properties array on the context stack found in $ psake, still the script blocks here

 function Properties { [CmdletBinding()] param( [Parameter(Position=0,Mandatory=1)][scriptblock]$properties ) $psake.context.Peek().properties += $properties } 

Line 372 of the Invoke-psake in psake.psm1 Loads your scope default.ps1 build script into scope (you are currently seeing your write-host calls, but no variables have been loaded)

 . $psake.build_script_file.FullName 

Line 394 and line 397 Loads script blocks from parameters and properties into scope.

  foreach ($key in $parameters.keys) { if (test-path "variable:\$key") { set-item -path "variable:\$key" -value $parameters.$key -WhatIf:$false -Confirm:$false | out-null } else { new-item -path "variable:\$key" -value $parameters.$key -WhatIf:$false -Confirm:$false | out-null } } 

...

 foreach ($key in $properties.keys) { if (test-path "variable:\$key") { set-item -path "variable:\$key" -value $properties.$key -WhatIf:$false -Confirm:$false | out-null } } 

Line 420 and 423 calls invoke-Task (line 198), which, in turn, uses the above variables, there is also a statement that the variables are not null in this function.

I do not think the expected use case involved loading these variables in the root area when the script is the first. As a result, it spills out write-host calls, so the design probably assumed that you would first declare the Task method so that the region could pass variables to it, and this should be noted.

0
source

All Articles