Skip to content

Commit

Permalink
Support directory path as -File
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Nov 27, 2024
1 parent fc3c7e0 commit c5ddf55
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
19 changes: 11 additions & 8 deletions Help.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,27 @@
current directory or a directory specified by the parameter File.
'@
File = @'
A build script which adds tasks by the command 'task' (Add-BuildTask).
The script which adds tasks using the command 'task' (Add-BuildTask).
If the file is omitted then Invoke-Build looks for *.build.ps1 files in
the current location and takes the first in Sort-Object order.
If File is omitted then Invoke-Build looks for *.build.ps1 files in the
current location and takes the first in Sort-Object order.
If the file is not found then a command set by the environment variable
InvokeBuildGetFile is invoked with the directory path as an argument.
This command may return the full path of a special build script.
If the file is still not found then parent directories are searched.
INLINE SCRIPT
DIRECTORY PATH
File accepts directory paths as well. The build script is resolved as
described above for the specified directory without searching parents.
'File' is a script block which is normally used in order to assemble a
build on the fly without creating and using an extra build script file.
INLINE SCRIPT
$BuildFile is the calling script (or null, e.g. in jobs).
The default $BuildRoot is its directory (or the current location).
File also accepts script blocks. In this case $BuildFile is the calling
script, if any, or null. The default $BuildRoot is $BuildFile directory
or the current location.
Script parameters, parallel, and persistent builds are not supported.
'@
Expand Down
10 changes: 7 additions & 3 deletions Invoke-Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function *Die($M, $C=0) {
$PSCmdlet.ThrowTerminatingError((New-Object System.Management.Automation.ErrorRecord ([Exception]"$M"), $null, $C, $null))
}

function Get-BuildFile($Path) {
function Get-BuildFile($Path, [switch]$Here) {
do {
if (($f = [System.IO.Directory]::GetFiles($Path, '*.build.ps1')).Length -eq 1) {return $f}
if ($f) {return $($f | Sort-Object)[0]}
if (($c = $env:InvokeBuildGetFile) -and ($f = & $c $Path)) {return $f}
} while($Path = Split-Path $Path)
} while(!$Here -and ($Path = Split-Path $Path))
}

if ($MyInvocation.InvocationName -eq '.') {return}
Expand Down Expand Up @@ -101,7 +101,11 @@ if ($BuildTask -eq '**') {
}

if ($BuildFile) {
if (![System.IO.File]::Exists(($BuildFile = *Path $BuildFile))) {throw "Missing script '$BuildFile'."}
if (![System.IO.File]::Exists(($BuildFile = *Path $BuildFile))) {
if (![System.IO.Directory]::Exists($BuildFile)) {throw "Missing script '$BuildFile'."}
if (!($_ = Get-BuildFile $BuildFile -Here)) {throw "Missing script in '$BuildFile'."}
$BuildFile = $_
}
}
elseif (!($BuildFile = Get-BuildFile ${*}.CD)) {
throw 'Missing default script.'
Expand Down
5 changes: 5 additions & 0 deletions Release-Notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Invoke-Build Release Notes

## v5.12.1

`Invoke-Build` parameter `File` accepts directory paths as well.
The usual build script resolution applies but without parents.

## v5.12.0

Stop supporting and testing PowerShell v2.0 (unlikely practical).
Expand Down
10 changes: 5 additions & 5 deletions Tasks/Release.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ $BuildRoot = '..'
Set-Alias ask Confirm-Build
Set-StrictMode -Version Latest

# Synopsis: Run all other tasks with checkpoints.
task . -if {'.' -eq $BuildTask} {
Build-Checkpoint -Auto $HOME\z.releaseInvokeBuild.clixml @{Task='*'; File=$BuildFile}
}

# Synopsis: It should be the main branch with no changes.
task status -if {ask} {
assert ('main' -ceq (exec { git branch --show-current })) 'Please checkout main.'
Expand Down Expand Up @@ -75,3 +70,8 @@ task clean_and_browse -if {ask} {
Start-Process https://www.nuget.org/packages/Invoke-Build/
Start-Process https://www.nuget.org/packages/ib/
}

# Synopsis: Run all tasks with checkpoints.
task . -if {'.' -eq $BuildTask} {
Build-Checkpoint -Auto $HOME\z.releaseInvokeBuild.clixml @{Task='*'; File=$BuildFile}
}
56 changes: 31 additions & 25 deletions Tests/Default.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
task TwoCandidates {
remove z
$null = mkdir z
Push-Location z

Push-Location z
Set-Content 1.build.ps1 'task t1'
Set-Content 2.build.ps1 'task t2'
$tasks = Invoke-Build ??

assert ($tasks.Contains('t1'))
Pop-Location
remove z

# case: -File is folder
$tasks = Invoke-Build ?? z
assert ($tasks.Contains('t1'))

remove z
}

task ParentHasManyCandidates {
Expand All @@ -25,15 +28,17 @@ task ParentHasManyCandidates {

Push-Location z
$tasks = Invoke-Build ??
Pop-Location

assert ($tasks.Contains('AllTestScripts'))
Pop-Location

Push-Location z/1
$tasks = Invoke-Build ??
assert ($tasks.Contains('AllTestScripts'))
Pop-Location

assert ($tasks.Contains('AllTestScripts'))
# case: -File is folder
try { throw Invoke-Build ?? z }
catch { assert ($_ -like "Missing script in '*\Tests\z'.") }

remove z
}
Expand All @@ -48,41 +53,42 @@ task ParentHasOneCandidate {

Push-Location z/1
$tasks = Invoke-Build ??
Pop-Location

assert $tasks.Contains('SingleScript')
Pop-Location

Push-Location z/1/2
$tasks = Invoke-Build ??
Pop-Location

assert $tasks.Contains('SingleScript')
Pop-Location

remove z
}

task InvokeBuildGetFile {
remove z
$null = mkdir z
$null = mkdir z/1

# register the hook by the environment variable
$saved = $env:InvokeBuildGetFile
$env:InvokeBuildGetFile = "$BuildRoot/z/1/InvokeBuildGetFile.ps1"

# make the hook script which gets this script as a build file
Set-Content -LiteralPath $env:InvokeBuildGetFile "'$BuildFile'"

# invoke (remove the test script, if any)
Push-Location z
$tasks = Invoke-Build ??
Pop-Location

# restore the hook
$env:InvokeBuildGetFile = $saved

# test: the script returned by the hook is invoked
assert $tasks.Contains('InvokeBuildGetFile')
try {
# make the hook script which gets this script as a build file
Set-Content -LiteralPath $env:InvokeBuildGetFile "'$BuildFile'"

# invoke and test the script returned by the hook
Push-Location z
$tasks = Invoke-Build ??
assert $tasks.Contains('InvokeBuildGetFile')
Pop-Location

# case: -File is folder
$tasks = Invoke-Build ?? z
assert $tasks.Contains('InvokeBuildGetFile')
}
finally {
# restore the hook
$env:InvokeBuildGetFile = $saved
}

remove z
}
Expand Down

0 comments on commit c5ddf55

Please sign in to comment.