使用 psake 4.5.0 ,我尝试了多种方式运行default.ps1
文件,同时传递了-properties
和-parameters
,但是在.ps1
-script的根作用域中,忽略了这些值。
执行相对(在子文件夹中为psake):.\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"}
使用导入的模块执行: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"}
通过安装的Chocolatey软件包执行:psake .\default.ps1 BuildSolution -properties "@{'a'='a';'b'='b';'c'='c';'d'='d'}" -parameters "@{'w'='w';'x'='x';'y'='y';'z'='z'}"
通过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'}"
现在,default.ps1
只是调试所有这些值:// 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"
}
在所有情况下,输出均为:a:
b:
c:
d:
w:
x:
y:
z:
将properties
和/或parameters
传递给psake的正确语法是什么?
最佳答案
我没有花太多时间研究它,但似乎只要您在属性中声明$ variable {... here ...}
或在Param中(...这里...),以便psake可以填充它们,尽管它们还可以。您错过了$ z。除非我缺少有关为什么同时使用参数和属性的基本知识?
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"
}
调用代码示例(通过Chocolatey安装到c的psake):
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"}
编辑:
这是psake.psm1中的
Properties
函数第256行,请注意它需要一个脚本块args并将其添加到$ psake上的上下文堆栈的属性数组中,此处仍是脚本块function Properties {
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][scriptblock]$properties
)
$psake.context.Peek().properties += $properties
}
psake.psm1中
Invoke-psake
函数的第372行将您的default.ps1构建脚本加载到范围中(这时您看到了write-host
调用,但未加载任何变量). $psake.build_script_file.FullName
394行和397行将脚本块从参数和属性加载到合并范围。
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
}
}
第420和423行调用
invoke-Task
(第198行),后者依次使用上面定义的变量,并且还声明该函数中的变量不为null。我不认为预期的用例是在脚本为first时将这些变量加载到根范围中。源于作用域,它吐出
write-host
调用,然后,设计可能打算让您首先声明Task方法,以便作用域可以将变量传递给它,因此应予以注意。关于build - Psake忽略属性和参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35291866/