在 1 个任务中设置属性值并在另一个任务中使用更新后的值
在我的 psake 构建脚本中,我有一个名为 $build_mode 的属性,我将其设置为“Release”。
我有2个任务; “编译_调试”、“编译_发布”。在 Compile_Debug 中,我将 $build_mode 更改为“Debug”,并且在该任务执行时它工作正常;但是,如果我之后执行另一个使用 $build_mode 的任务,则 $build_mode 将返回到“Release”。
有没有办法在 Psake 构建脚本中全局更改或设置变量,以便可以在任务之间使用更新的值?
(我试图有 1 个“测试”或 1 个“包”任务,而不是“Test_Debug”等)
代码:
properties {
$build_mode = "Release"
}
task default -depends Compile_Debug, Test
task Compile_Debug {
$build_mode = "Debug"
# Compilation tasks here that use the Debug value
}
task Test {
# Test related tasks that depend on $build_mode being updated.
}
In my psake build script, I have a property called $build_mode that I set to "Release".
I have 2 tasks; "Compile_Debug", "Compile_Release". In Compile_Debug, I change $build_mode to "Debug" and it works fine while that task executes; however, if I have a have another task execute that uses $build_mode afterwards, $build_mode is back to "Release".
Is there a way to globally change or set a variable in a Psake build script so that an updated value can be used between tasks?
(I'm trying to have 1 "test" or 1 "package" task instead of a "Test_Debug", etc.)
Code:
properties {
$build_mode = "Release"
}
task default -depends Compile_Debug, Test
task Compile_Debug {
$build_mode = "Debug"
# Compilation tasks here that use the Debug value
}
task Test {
# Test related tasks that depend on $build_mode being updated.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常将构建模式设置为 @manojlds 建议的,作为 Invoke-Psake 调用中的参数传递。但是,如果您再次发现自己想要修改任务 A 中对象的值并有权访问任务 B 中修改后的值,则可以执行以下操作:
事实上,$build_mode 的修改值由于 powershell 作用域的原因,任务 B 中无法访问。当您在任务 A 中设置 $buildMode 变量的值时,该更改是在任务 A 的范围内进行的,因此在任务 A 的范围之外,变量值保持不变。
实现您想要的目的的一种方法是使用作用域为整个脚本的哈希表来存储您的对象:
代码:
唯一的警告是,每次您想要引用构建模式时,您都必须使用长 $script:hash.build_mode 名称而不是简单的 $build_mode
I usually set the build mode as @manojlds suggested, passing in as a paramenter in the Invoke-Psake call. But, if you find yourself again in a situation that you want to modify the value of a object in Task A and have access to the modified value in Task B, here is how to do it:
The fact that the modified value of $build_mode is not accessible in Task B is due to powershell scoping. When you set a value for the $buildMode variable in a Task A, that change is made within Task A's scope, therefore outside it the variable value keeps unchanged.
One way to achieve what you want is to use a hashtable scoped to the whole script to store your objects:
Code:
The only caveat is that every time you want to refer to your build mode you have to use the long $script:hash.build_mode name instead of simply $build_mode
为什么不将构建模式作为参数传递给 Invoke-Psake 中的任务?
在任务中,您现在可以使用
$build_mode
Why don't you pass the build mode as parameter to the tasks from the Invoke-Psake?
And in the tasks you can now use
$build_mode