2010-02-05 62 views
7

我想注入的命令行參數代入我的psake構建腳本一樣: \ build.ps1部署環境=「發展」進樣的命令行參數爲psake

但psake會對待每一個參數的任務和將回答「任務不存在」

是否可以在psake中注入命令行參數?

build.ps1 --> 
Import-Module '.\psake.psm1' 
Invoke-psake '.\tasks.ps1' $args 
Remove-Module psake 
+1

在http://groups.google.com/group/psake-users或http://code.google.com/p/psake/ – 2010-02-05 23:51:35

+0

上,您可能有更好的運氣可以回答此問題。已向討論組添加了功能請求 - http://groups.google.com/group/psake-users/browse_thread/thread/cf59508789ab58eb – 2010-02-07 22:32:49

回答

10

latest release of psake現在supports passing parameters到調用-psake,例如

Invoke-psake .\parameters.ps1 -parameters @{"p1"="v1";"p2"="v2"} 

該功能剛剛添加完畢。 :)

+0

感謝您的反饋,它比在寫作的時候gobal變量 – orjan 2010-02-10 10:12:42

+0

PS一個更好的選擇,這個新功能不是官方「發佈」的一部分:你將不得不從主幹中獲得最新的源代碼(或者任何在GIT中調用的主幹) – 2010-02-10 11:05:23

0

我不是專家,但我不認爲有可能將參數傳遞給Invoke-Psake。找對Psake最新源代碼的調用,Psake功能PARAMS是:

param(
    [Parameter(Position=0,Mandatory=0)] 
    [string]$buildFile = 'default.ps1', 
    [Parameter(Position=1,Mandatory=0)] 
    [string[]]$taskList = @(), 
    [Parameter(Position=2,Mandatory=0)] 
    [string]$framework = '3.5', 
    [Parameter(Position=3,Mandatory=0)] 
    [switch]$docs = $false  
    ) 

有4個參數,生成文件,任務列表,.NET Framework版本,是否要輸出的文檔任務。

properties { 
    $environment = "default" 
} 

task PublishForLive -precondition { $environment = "Live"; return $true; } -depends Publish { 

} 

task PublishForStaging -precondition { $environment = "Staging"; return $true; } -depends Publish { 

} 

task Publish { 
    Write-Host "Building and publishing for $environment environment" 
    #Publish the project... 
} 

然後調用psake與PublishForLive或PublishForStaging:我是新來的PowerShell和psake,我試圖做同樣的事情,我有做這樣的事情在我的腳本來實現同樣的事情試驗,無論我需要什麼:

powershell -NoExit -ExecutionPolicy Unrestricted -Command "& {Import-Module .\tools\psake\psake.psm1; Invoke-psake .\psake-common.ps1 PublishForLive }" 

但它似乎並不適合我!在任務前置條件中設置$ environment變量似乎沒有效果。仍然試圖使這項工作...

+0

嘗試使$環境變量分配在scriptblock範圍外可見'$ script:environment =「Live」' – 2010-02-06 20:42:16

+0

這是您的示例中的競爭條件發佈將在PublishForStaging之前運行。另一種解決方案是從PublishForStaging中調用Publish,但不知道它是否可行? – orjan 2010-02-07 08:34:35

+0

這個工作對我來說: 任務PublishStage { $環境=「舞臺」 ExecuteTask(「發佈」) } – orjan 2010-02-07 10:13:33

1

一個全局變量將解決我現在的問題,只有一個引用$全局:arg_environent它將很容易改變,如果我找到一個更好的方式來注入屬性。

build.ps1

param(
    [Parameter(Position=0,Mandatory=0)] 
    [string]$task, 
    [Parameter(Position=1,Mandatory=0)] 
    [string]$environment = 'dev' 
) 

clear 
$global:arg_environent = $environment 
Import-Module .\psake.psm1 
Invoke-psake tasks.ps1 $task 
Remove-Module psake 

tasks.ps1

properties { 
    $environment = $global:arg_environent 
} 

task default -depends Deploy 

task Deploy { 
    echo "Copy stuff to $environment" 
}