Skip to content

Instantly share code, notes, and snippets.

@pitermarx
Last active December 9, 2019 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitermarx/747c4b41e7dcc895e46aed86fe7290a7 to your computer and use it in GitHub Desktop.
Save pitermarx/747c4b41e7dcc895e46aed86fe7290a7 to your computer and use it in GitHub Desktop.
Cake task autocomplete in powershell
[CmdletBinding()]
Param(
[string]$Script = "build.cake",
[ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
[string]$Verbosity = "Normal",
[Parameter(Position = 0, Mandatory = $false, ValueFromRemainingArguments = $true)]
[string[]]$ScriptArgs
)
dynamicparam {
# user defined files where to search for tasks
$files = Get-ChildItem .\BuildScripts\*.cake -Recurse
$files += (Get-ChildItem build.cake)
# Regex to get the task names
$values = $files | Select-String '^.*Task\("(.*)"\).*$' | % { $_.Matches[0].Groups[1].Value }
# create validate set attribute
$ValidateSet = new-object System.Management.Automation.ValidateSetAttribute($values)
$attributes = new-object System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "__AllParameterSets"
$attributes.Mandatory = $true
$attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
$attributeCollection.Add($ValidateSet)
$dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("Target", [string], $attributeCollection)
$paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("Target", $dynParam1)
return $paramDictionary
}
process {
# The actual bootstraping file here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment