3

在我的 psake 构建脚本中,我有一个名为 $build_mode 的属性,我将其设置为“Release”。

我有 2 个任务;“Compile_Debug”、“Compile_Release”。在 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.
}
4

2 回答 2

4

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

在任务 B 中无法访问 $build_mode 的修改值的事实是由于 powershell 作用域。当您在任务 A 中为 $buildMode 变量设置值时,该更改是在任务 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

于 2012-03-20T17:01:29.710 回答
2

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

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

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

于 2012-01-27T21:36:12.720 回答