diff --git a/.build.ps1 b/.build.ps1 index bb53777..2d2a2d1 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -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' diff --git a/Tasks/Direct-2/.test.ps1 b/Tasks/Direct-2/.test.ps1 new file mode 100644 index 0000000..6135748 --- /dev/null +++ b/Tasks/Direct-2/.test.ps1 @@ -0,0 +1,8 @@ + +task all { + ./Build.ps1 +} + +task show { + ./Build.ps1 ? +} diff --git a/Tasks/Direct-2/Build.ps1 b/Tasks/Direct-2/Build.ps1 new file mode 100644 index 0000000..8d79d7e --- /dev/null +++ b/Tasks/Direct-2/Build.ps1 @@ -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" + } +} diff --git a/Tasks/Direct-2/README.md b/Tasks/Direct-2/README.md new file mode 100644 index 0000000..66d52fb --- /dev/null +++ b/Tasks/Direct-2/README.md @@ -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 ? +``` diff --git a/Tasks/Direct/README.md b/Tasks/Direct/README.md index 683458b..0a5d270 100644 --- a/Tasks/Direct/README.md +++ b/Tasks/Direct/README.md @@ -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: diff --git a/Tasks/README.md b/Tasks/README.md index fbc9155..2b1209c 100644 --- a/Tasks/README.md +++ b/Tasks/README.md @@ -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.