Skip to content

Commit

Permalink
Add Tasks/Direct-2
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Jan 11, 2025
1 parent c5ddf55 commit 8fb540f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ task module version, markdown, help, {
[System.IO.File]::WriteAllText("$dir\InvokeBuild.psd1", @"
@{
ModuleVersion = '$script:Version'
ModuleToProcess = 'InvokeBuild.psm1'
RootModule = 'InvokeBuild.psm1'
GUID = 'a0319025-5f1f-47f0-ae8d-9c7e151a5aae'
Author = 'Roman Kuzmin'
CompanyName = 'Roman Kuzmin'
Expand Down
8 changes: 8 additions & 0 deletions Tasks/Direct-2/.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

task all {
./Build.ps1
}

task show {
./Build.ps1 ?
}
61 changes: 61 additions & 0 deletions Tasks/Direct-2/Build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<#
.Synopsis
Directly invocable script with build tasks (variant 2).
#>

[CmdletBinding()]
param(
# Choose the default: '*' ~ run all; '?' - show tasks; '.' or nothing ~ default task.
[string[]]$Tasks = '*'
,
# Other parameters as usual.
[string]$Param1 = 'Value1'
)

# Outer script scope, suitable for common functions and variables for reading.

# This function is available for tasks.
function Get-CommonSomething {
'CommonSomething'
}

# This variable is available for reading as $Var1 and not "easily" available for writing.
$Var1 = 10

# Call the engine with the script block adding tasks.
Invoke-Build $Tasks {
# Inner script scope, this is the usual build script body with tasks, variables, functions.

# This variable is available for reading as $Var1 or $Script:Var2 and for writing as $Script:Var2
$Var2 = 20

# Synopsis: $ErrorActionPreference is set to 1, equivalent "Stop" or [System.Management.Automation.ActionPreference]::Stop.
task ErrorActionPreferenceIsStop {
equals $ErrorActionPreference 1
}

# Synopsis: The build folder is this script folder regardless of the location where this script is invoked from.
task BuildRootIsThisScriptRoot {
equals $BuildRoot $PSScriptRoot
equals "$PWD" $PSScriptRoot
}

# Synopsis: Script functions are available for tasks.
task ScriptFunctions {
Get-CommonSomething
}

# Synopsis: Script parameters.
task ScriptParameters {
"Tasks = $Tasks"
"Param1 = $Param1"
}

# Synopsis: Script variables.
task ScriptVariables {
"Var1 = $Var1"
"Var2 = $Var2"
++$Script:Var2
"Var2 = $Var2"
}
}
45 changes: 45 additions & 0 deletions Tasks/Direct-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Directly invokable build scripts (variant 2)

Build scripts are normally invoked by the engine `Invoke-Build`, not directly.
If this is inconvenient then decorate a script to make it directly invokable.
Variant 1 is described here: [Direct](../Direct), with scripts either invoked
directly or by `Invoke-Build`.

Another way, variant 2, is defining build tasks in a script like this:

```powershell
[CmdletBinding()]
param(
[string[]]$Tasks
)
Invoke-Build $Tasks {
task build {
...
}
task clean {
...
}
...
}
```

Such scripts cannot be invoked by `Invoke-Build`, they are not composed as
build scripts. They are normal PowerShell scripts but they have tasks and
work similar to build scripts.

See the example script [Build.ps1](Build.ps1) and comments, try some calls:

```powershell
# run all tasks (because $Tasks default is set to '*')
./Build.ps1
# run with parameters
./Build.ps1 -Param1 testing
# run specified tasks
./Build.ps1 ScriptParameters, ScriptVariables
# show task descriptions
./Build.ps1 ?
```
2 changes: 2 additions & 0 deletions Tasks/Direct/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Directly invokable build scripts

See also [Direct-2](../Direct-2)

Build scripts are normally invoked by the engine `Invoke-Build`, not directly.
If this is inconvenient then decorate a script to make it directly invokable.
Add `Tasks` as the first parameter and the code block redirecting the call:
Expand Down
1 change: 1 addition & 0 deletions Tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Bootstrap](Bootstrap) How to install the module automatically.
- [Confirm](Confirm) How to use `Confirm-Build` to confirm some tasks.
- [Direct](Direct) How to make build scripts invokable directly.
- [Direct-2](Direct-2) How to make build scripts invokable directly (variant 2).
- [Dynamic](Dynamic) How to use a dynamic script with dynamic tasks.
- [Header](Header) How to define custom task headers and footers.
- [Import](Import) How to share and import tasks from external task scripts including exported by modules.
Expand Down

0 comments on commit 8fb540f

Please sign in to comment.