Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for tasks to specify parameters #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/psake.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ param(
[string]$buildFile,

[Parameter(Position = 1, Mandatory = $false)]
[string[]]$taskList = @(),
[string]$task,

[Parameter(Position = 2, Mandatory = $false)]
[string]$framework,
Expand Down Expand Up @@ -64,4 +64,4 @@ if ($buildFile -and (-not (Test-Path -Path $buildFile))) {
}
}

Invoke-psake $buildFile $taskList $framework $docs $parameters $properties $initialization $nologo $detailedDocs $notr
Invoke-psake $buildFile $task $framework $docs $parameters $properties $initialization $nologo $detailedDocs $notr
28 changes: 26 additions & 2 deletions src/public/Invoke-Task.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ function Invoke-Task {
[string]$taskName
)

$entireTask = $taskName
if ($taskName.Contains(' ')) {
$taskName = $taskName.Split(' ', [StringSplitOptions]::RemoveEmptyEntries) | Select -First 1
}

Assert $taskName ($msgs.error_invalid_task_name)

$taskKey = $taskName.ToLower()
Expand Down Expand Up @@ -103,8 +108,27 @@ function Invoke-Task {
foreach ($variable in $task.requiredVariables) {
Assert ((Test-Path "variable:$variable") -and ($null -ne (Get-Variable $variable).Value)) ($msgs.required_variable_not_set -f $variable, $taskName)
}

& $task.Action

$argsTable = @{}
$parsedArgs = [management.automation.psparser]::Tokenize($entireTask, [ref]$null)

foreach ($p in $parsedArgs) {
# skip the "-" in front of the arg
$arg = $p.Content.Substring(1)

# if there isn't a "=", it's a switch parameter
if (-not ($arg.Contains("="))) {
$argsTable.Add($arg, $true)
continue
}
# todo: split on only the first instance...
$parts = $arg.Split("=")
$arg = $parts[0]
$val = $parts[1]
$argsTable.Add($arg, $val)
}
# splat args so it's bound to the task args properly
& $task.Action @argsTable
} finally {
if ($task.PostAction) {
& $task.PostAction
Expand Down
14 changes: 6 additions & 8 deletions src/public/Invoke-psake.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function Invoke-psake {
.PARAMETER buildFile
The path to the psake build script to execute

.PARAMETER taskList
A comma-separated list of task names to execute
.PARAMETER task
The task name to execute

.PARAMETER framework
The version of the .NET framework you want to use during build. You can append x86 or x64 to force a specific framework.
Expand Down Expand Up @@ -208,7 +208,7 @@ function Invoke-psake {
[string]$buildFile,

[Parameter(Position = 1, Mandatory = $false)]
[string[]]$taskList = @(),
[string]$task,

[Parameter(Position = 2, Mandatory = $false)]
[string]$framework,
Expand Down Expand Up @@ -246,7 +246,7 @@ function Invoke-psake {
elseif (!(Test-Path $buildFile -PathType Leaf) -and ($null -ne (Get-DefaultBuildFile -UseDefaultIfNoneExist $false))) {
# If the default file exists and the given "buildfile" isn't found assume that the given
# $buildFile is actually the target Tasks to execute in the $config.buildFileName script.
$taskList = $buildFile.Split(', ')
$task = $buildFile.Split(', ') | Select -First 1
$buildFile = Get-DefaultBuildFile
}

Expand Down Expand Up @@ -284,10 +284,8 @@ function Invoke-psake {
. $module $initialization

# Execute the list of tasks or the default task
if ($taskList) {
foreach ($task in $taskList) {
invoke-task $task
}
if ($task) {
invoke-task $task
} elseif ($currentContext.tasks.default) {
invoke-task default
} else {
Expand Down