Last active
January 9, 2019 21:32
-
-
Save exlted/3da8f99c7797289285e0bebfcfeb5f69 to your computer and use it in GitHub Desktop.
Bugle (Powershell Task Runner)
This file contains hidden or 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
New-Alias Bugle Invoke-Local | |
function Invoke-Local(){ | |
param( | |
[Parameter(Position=0,mandatory=$true)][string] $task, | |
[Parameter(Position=1)][string[]] $parms, | |
[string] $file = ".\build.ps1" | |
) | |
if($task[0] -eq "_"){ | |
Write-Error "Scripts starting with _ are considered private" | |
exit | |
} | |
if(!(Test-Path $file)){ | |
Write-Error "$($file) not found in the current location, run this script against a different file with -file or create $($file)" | |
exit | |
} | |
$loc = Get-ChildItem $file | |
$global:BugleFile = $loc | |
if($task -eq "help"){ | |
. $loc | |
return | |
} | |
. $loc | out-null | |
Write-Host Starting task: $task | |
&$task $parms | |
$global:BugleFile = $null | |
} | |
New-Alias Bugle-Help Get-LocalScripts | |
function Get-LocalScripts(){ | |
param( | |
[string] $file = ".\build.ps1" | |
) | |
$loc = Get-ChildItem $file | |
. $loc | |
} | |
export-modulemember -function Invoke-Local, Get-LocalScripts -alias Bugle, Bugle-Help | |
if (Get-Command Register-ArgumentCompleter -ea Ignore){ | |
function BugleCompletion{ | |
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) | |
if($fakeBoundParameter.file){ | |
$loc = Get-ChildItem $fakeBoundParameter.file | |
$funcs = & $loc | |
} else { | |
$funcs = .\build.ps1 | |
} | |
foreach($func in $funcs){ | |
if($func -like "$wordToComplete*"){ | |
New-CompletionResult -CompletionText $func -ToolTip ($file) | |
} | |
} | |
} | |
Register-ArgumentCompleter -Command Invoke-Local -Parameter task -ScriptBlock $function:BugleCompletion | |
} |
This file contains hidden or 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
class File { | |
[System.IO.FileSystemInfo]$Info | |
[string]$SubPath | |
[string]$Content | |
File($Info, $SubPath, $Content){ | |
$this.Info = $Info | |
$this.SubPath = $SubPath | |
$this.Content = $Content | |
} | |
} | |
# Loads the specified locations/files into memory, passing it out to the cache | |
function _in([string[]]$fileIn, [string]$filter = ""){ | |
$cache = @() | |
foreach($loc in $fileIn){ | |
Write-Host Reading from $loc | |
Push-Location $loc | |
$files = Get-ChildItem -Recurse -File -Filter $filter | |
foreach($file in $files){ | |
$content = Get-Content $file.FullName -Raw | |
$SubPath = Resolve-Path -relative $file.Directory.FullName | |
$cache = $cache + @(New-Object File -ArgumentList $file, $SubPath, $content) | |
} | |
Pop-Location | |
} | |
return $cache | |
} | |
# Takes piped input files, writes them out to the specified directory/ies | |
# _out maintains the source directory's structure | |
function _out([string[]]$locOut){ | |
foreach($loc in $locOut){ | |
if(!$(Test-Path $loc)){ | |
New-Item $loc -ItemType Directory | Out-Null | |
} | |
} | |
foreach($file in $input){ | |
foreach($loc in $locOut){ | |
Push-Location $loc | |
if($file.SubPath){ | |
if(!$(Test-Path $file.SubPath)){ | |
New-Item $file.SubPath -ItemType Directory | Out-Null | |
} | |
} | |
$path = Join-Path $file.SubPath $file.Info.Name | |
Out-File $path -InputObject $file.Content | |
Pop-Location | |
} | |
} | |
return $input | |
} | |
# Takes piped input files, writes them out to the specified directory/ies | |
# _outFlat does not maintain the source directory's structure | |
function _outFlat([string[]]$locOut){ | |
foreach($loc in $locOut){ | |
if(!$(Test-Path $loc)){ | |
New-Item $loc -ItemType Directory | Out-Null | |
} | |
} | |
foreach($file in $input){ | |
foreach($loc in $locOut){ | |
$path = Join-Path $loc $file.Info.Name | |
Out-File $path -InputObject $file.Content | |
} | |
} | |
return $input | |
} | |
# Runs the specified tasks in parallel, waiting for all of them to be done | |
function _runParallel([string[]]$tasks){ | |
$cwd = (Get-Item -Path ".\").FullName | |
$init = [ScriptBlock]::Create("cd $cwd; . $($global:BugleFile.FullName) | Out-Null") | |
foreach($task in $tasks){ | |
Write-Host Starting $task | |
$scriptblock = [ScriptBlock]::Create($task) | |
Start-Job -ScriptBlock $scriptblock -Name "Bugle_RunBefore" -InitializationScript $init | Out-Null | |
} | |
$jobs = Get-Job -Name "Bugle_RunBefore" | |
Write-Host Waiting for tasks to finish | |
Wait-Job -Name "Bugle_RunBefore" | Out-Null | |
Write-Host Tasks Completed | |
foreach ($job in $jobs) { | |
if ($job.State -eq 'Failed') { | |
Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red | |
} | |
} | |
Remove-Job -Name "Bugle_RunBefore" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment