Permalink
Browse files

Added RequiredVariables argument to Task function

Also unified formatting in Task
  • Loading branch information...
1 parent 2d4e56a commit 5237be1ffa3526278626d60361d5fc63a8c15bc7 @whut whut committed Jun 26, 2011
View
@@ -1401,6 +1401,10 @@ Task Clean {
<command:parameter require="false" variableLength="false" globbing="false" pipelineInput="false" postion="0">
<maml:name>Depends</maml:name>
<command:parameterValue required="false" variableLength="false">String[]</command:parameterValue>
+ </command:parameter>
+ <command:parameter require="false" variableLength="false" globbing="false" pipelineInput="false" postion="0">
+ <maml:name>RequiredVariables</maml:name>
+ <command:parameterValue required="false" variableLength="false">String[]</command:parameterValue>
</command:parameter>
<command:parameter require="false" variableLength="false" globbing="false" pipelineInput="false" postion="0">
<maml:name>Description</maml:name>
@@ -1505,6 +1509,18 @@ Task Clean {
</dev:type>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
+ <command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false (ByValue)" position="0">
+ <maml:name>RequiredVariables</maml:name>
+ <maml:description>
+ <maml:para>An array of names of variables that must be set to run this task.</maml:para>
+ </maml:description>
+ <command:parameterValue required="false" variableLength="false">String[]</command:parameterValue>
+ <dev:type>
+ <maml:name>String[]</maml:name>
+ <maml:uri />
+ </dev:type>
+ <dev:defaultValue></dev:defaultValue>
+ </command:parameter>
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false (ByValue)" position="0">
<maml:name>Description</maml:name>
<maml:description>
@@ -0,0 +1,14 @@
+properties {
+ $x = $null
+ $y = $null
+ $z = $null
+}
+
+task default -depends TestRequiredVariables
+
+# you can put arguments to task in multiple lines using `
+task TestRequiredVariables `
+ -description "This task shows how to make a variable required to run task. Run this script with -properties @{x = 1; y = 2; z = 3}" `
+ -requiredVariables x, y, z `
+{
+}
View
@@ -81,6 +81,10 @@ function Invoke-Task
Write-ColoredOutput ($currentContext.config.taskNameFormat -f $taskName) -foregroundcolor Blue
}
+ foreach ($variable in $task.requiredVariables) {
+ Assert ((test-path "variable:$variable") -and ((get-variable $variable).Value -ne $null)) ($msgs.required_variable_not_set -f $variable, $taskName)
+ }
+
& $task.Action
if ($task.PostAction) {
@@ -152,17 +156,17 @@ function Task
{
[CmdletBinding()]
param(
- [Parameter(Position=0,Mandatory=1)] [string]$name = $null,
- [Parameter(Position=1,Mandatory=0)] [scriptblock]$action = $null,
- [Parameter(Position=2,Mandatory=0)] [scriptblock]$preaction = $null,
- [Parameter(Position=3,Mandatory=0)] [scriptblock]$postaction = $null,
- [Parameter(Position=4,Mandatory=0)] [scriptblock]$precondition = {$true},
- [Parameter(Position=5,Mandatory=0)] [scriptblock]$postcondition = {$true},
- [Parameter(Position=6,Mandatory=0)] [switch]$continueOnError = $false,
- [Parameter(Position=7,Mandatory=0)] [string[]]$depends = @(),
- [Parameter(Position=8,Mandatory=0)] [string]$description = $null
+ [Parameter(Position=0,Mandatory=1)][string]$name = $null,
+ [Parameter(Position=1,Mandatory=0)][scriptblock]$action = $null,
+ [Parameter(Position=2,Mandatory=0)][scriptblock]$preaction = $null,
+ [Parameter(Position=3,Mandatory=0)][scriptblock]$postaction = $null,
+ [Parameter(Position=4,Mandatory=0)][scriptblock]$precondition = {$true},
+ [Parameter(Position=5,Mandatory=0)][scriptblock]$postcondition = {$true},
+ [Parameter(Position=6,Mandatory=0)][switch]$continueOnError = $false,
+ [Parameter(Position=7,Mandatory=0)][string[]]$depends = @(),
+ [Parameter(Position=8,Mandatory=0)][string[]]$requiredVariables = @(),
+ [Parameter(Position=9,Mandatory=0)][string]$description = $null
)
-
if ($name -eq 'default') {
Assert (!$action) ($msgs.error_default_task_cannot_have_action)
}
@@ -178,6 +182,7 @@ function Task
ContinueOnError = $continueOnError
Description = $description
Duration = 0
+ RequiredVariables = $requiredVariables
}
$taskKey = $name.ToLower()
@@ -647,6 +652,7 @@ convertfrom-stringdata @'
error_no_default_task = default task required
error_loading_module = Error loading module: {0}
warning_deprecated_framework_variable = Warning: Using global variable $framework to set .NET framework version used is deprecated. Instead use Framework function or configuration file psake-config.ps1
+ required_variable_not_set = Variable {0} must be set to run task {1}.
postcondition_failed = Postcondition failed for {0}
precondition_was_false = Precondition was false not executing {0}
continue_on_error = Error in Task [{0}] {1}
@@ -0,0 +1,10 @@
+properties {
+ $x = $null
+ $y = $null
+ $z = $null
+}
+
+task default -depends TestProperties
+
+task TestProperties -requiredVariables z{
+}
@@ -0,0 +1,9 @@
+properties {
+ $x = $null
+ $y = $null
+}
+
+task default -depends TestRequired
+
+task TestRequired -requiredVariables x, y {
+}

0 comments on commit 5237be1

Please sign in to comment.