Last active
October 17, 2023 10:55
-
-
Save Fireforge/c59c85b6f7f3e191c283ff99b5cb2af3 to your computer and use it in GitHub Desktop.
powershell task runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple task runner implemented in Powershell | |
Param( | |
[String]$TaskName, # The name of the task to run | |
[parameter(ValueFromRemainingArguments=$true)][String[]]$TaskArgs # The list of repos to run the task on | |
) | |
# Define the tasks | |
$tasks = @{} | |
$tasks.Add('install',@{ | |
description="Runs Maven `install` with default parameters"; | |
script = { | |
mvn install "-Dcmake.compiler=vc2012" | |
} | |
}) | |
$tasks.Add('clean',@{ | |
description="Clean the out-of-source build folder"; | |
script = { | |
Remove-Item -Force -Recurse "build" | |
} | |
}) | |
# Helper functions | |
# Some helpful strings for formatting output | |
$indent = (" " * 4); | |
$spacer = ("-" * 40); | |
function DisplayHelpText { | |
$help_text = Get-Help $MyInvocation.ScriptName | |
$syn = $help_text.Synopsis | |
Write-Output " | |
Task Runner - runtask TaskName [TaskArgs] | |
DisplayTaskList | |
} | |
function DisplayTaskList{ | |
Write-Output " | |
List of Tasks: | |
$spacer" | |
foreach ($task in $tasks.GetEnumerator()) { | |
Write-Output "$indent$($task.Key)" | |
Write-Output "$($indent * 2)$($task.Value.description)" | |
} | |
} | |
# Now process the given task name | |
if (-not $taskname) { | |
DisplayHelpText | |
exit | |
} | |
$task = $tasks.Get_Item($taskname) | |
if ($task) { | |
Invoke-Command $task.script -ArgumentList (,$TaskArgs) | |
} | |
else { | |
Write-Output "'$taskname' is not a valid task name." | |
DisplayTaskList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment