在 1 个任务中设置属性值并在另一个任务中使用更新后的值

发布于 2024-12-29 13:10:38 字数 650 浏览 1 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请止步禁区 2025-01-05 13:10:38

我通常将构建模式设置为 @manojlds 建议的,作为 Invoke-Psake 调用中的参数传递。但是,如果您再次发现自己想要修改任务 A 中对象的值并有权访问任务 B 中修改后的值,则可以执行以下操作:

事实上,$build_mode 的修改值由于 powershell 作用域的原因,任务 B 中无法访问。当您在任务 A 中设置 $buildMode 变量的值时,该更改是在任务 A 的范围内进行的,因此在任务 A 的范围之外,变量值保持不变。

实现您想要的目的的一种方法是使用作用域为整个脚本的哈希表来存储您的对象:

代码:

properties {
    #initializes your global hash
    $script:hash = @{}
    $script:hash.build_mode = "Release"
}

task default -depends Compile_Debug, Test

task Compile_Debug {
    $script:hash.build_mode = "Debug"
    # Compilation tasks here that use the Debug value
}

task Test {
        # Test related tasks that depend on $script:hash.build_mode being updated.
}

唯一的警告是,每次您想要引用构建模式时,您都必须使用长 $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:

properties {
    #initializes your global hash
    $script:hash = @{}
    $script:hash.build_mode = "Release"
}

task default -depends Compile_Debug, Test

task Compile_Debug {
    $script:hash.build_mode = "Debug"
    # Compilation tasks here that use the Debug value
}

task Test {
        # Test related tasks that depend on $script:hash.build_mode being updated.
}

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

故事灯 2025-01-05 13:10:38

为什么不将构建模式作为参数传递给 Invoke-Psake 中的任务?

 Invoke-Psake "$PSScriptRoot\Deploy\Deploy.Tasks.ps1" `
        -framework $conventions.framework `
        -taskList $tasks `
        -parameters @{
                "build_mode" = "Debug"
            }

在任务中,您现在可以使用 $build_mode

Why don't you pass the build mode as parameter to the tasks from the Invoke-Psake?

 Invoke-Psake "$PSScriptRoot\Deploy\Deploy.Tasks.ps1" `
        -framework $conventions.framework `
        -taskList $tasks `
        -parameters @{
                "build_mode" = "Debug"
            }

And in the tasks you can now use $build_mode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文