3

私の psake ビルド スクリプトには、"Release" に設定した $build_mode というプロパティがあります。

2 つのタスクがあります。「Compile_Debug」、「Compile_Release」。Compile_Debug で、$build_mode を「Debug」に変更すると、そのタスクの実行中に問題なく動作します。ただし、後で $build_mode を使用する別のタスクを実行すると、$build_mode は "Release" に戻ります。

タスク間で更新された値を使用できるように、Psake ビルド スクリプトで変数をグローバルに変更または設定する方法はありますか?

(「Test_Debug」などの代わりに、1 つの「テスト」または 1 つの「パッケージ」タスクを作成しようとしています。)

コード:

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 で変更された値にアクセスしたい状況に再び遭遇した場合は、次のようにします。

$build_mode の変更された値がタスク B でアクセスできないという事実は、powershell スコープによるものです。タスク A で $buildMode 変数の値を設定すると、その変更はタスク A のスコープ内で行われるため、変数の値はタスク A のスコープ外では変更されません。

目的を達成する 1 つの方法は、スクリプト全体をスコープとするハッシュテーブルを使用してオブジェクトを格納することです。

コード:

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.
}

唯一の注意点は、ビルド モードを参照するたびに、単に $build_mode ではなく、長い $script:hash.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 に答える