From a8e186bbb0c5eb7e1a6f048e805adb362fa2c6b7 Mon Sep 17 00:00:00 2001 From: Roman Kuzmin Date: Fri, 2 Aug 2024 06:56:44 +0000 Subject: [PATCH] Add Tasks/SubCases --- Show-BuildMermaid.ps1 | 2 +- Tasks/README.md | 3 +- Tasks/SubCases/.test.ps1 | 31 +++++++++++++++++++ Tasks/SubCases/README.md | 46 ++++++++++++++++++++++++++++ Tasks/SubCases/case1/case1.tasks.ps1 | 11 +++++++ Tasks/SubCases/case2/case2.tasks.ps1 | 14 +++++++++ Tasks/SubCases/root.build.ps1 | 21 +++++++++++++ 7 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 Tasks/SubCases/.test.ps1 create mode 100644 Tasks/SubCases/README.md create mode 100644 Tasks/SubCases/case1/case1.tasks.ps1 create mode 100644 Tasks/SubCases/case2/case2.tasks.ps1 create mode 100644 Tasks/SubCases/root.build.ps1 diff --git a/Show-BuildMermaid.ps1 b/Show-BuildMermaid.ps1 index 5238727..688ac5e 100644 --- a/Show-BuildMermaid.ps1 +++ b/Show-BuildMermaid.ps1 @@ -153,7 +153,7 @@ $text = @( @' diff --git a/Tasks/README.md b/Tasks/README.md index 29e5668..fbc9155 100644 --- a/Tasks/README.md +++ b/Tasks/README.md @@ -14,7 +14,8 @@ - [Paket](Paket) Build script with automatic bootstrapping using `paket`. - [Param](Param) How to create tasks which perform similar actions with some differences defined by parameters. - [StdErr](StdErr) Dealing with standard error output issues. -- [SubTasks](SubTasks) Sub tasks in child scripts technique. +- [SubCases](SubCases) Sub tasks in child scripts technique (1). +- [SubTasks](SubTasks) Sub tasks in child scripts technique (2). ## Custom tasks diff --git a/Tasks/SubCases/.test.ps1 b/Tasks/SubCases/.test.ps1 new file mode 100644 index 0000000..49feaf8 --- /dev/null +++ b/Tasks/SubCases/.test.ps1 @@ -0,0 +1,31 @@ +if ($PSVersionTable.PSVersion.Major -eq 2) {return task skipV2} + +# Default task of case1 with default parameters. +task case1 { + ($r = Invoke-Build -Case case1) + assert ($r -like 'case file : case1\case1.tasks.ps1') + assert ($r -like 'configuration : Release') + assert ($r -like 'var1 : root value 1') +} + +# Default task of case2 with custom parameters. +task case2 { + ($r = Invoke-Build -Case case2 -Configuration Debug) + assert ($r -like 'case file : case2\case2.tasks.ps1') + assert ($r -like 'configuration : Debug') + assert ($r -like 'var1 : changed by case2') +} + +# Common root task in case1. +task case1_root1 { + ($r = Invoke-Build root1 -Case case1) + assert ($r -like 'case : *\case1') + assert ($r -like 'var1 : root value 1') +} + +# Common root task in case2. +task case2_root1 { + ($r = Invoke-Build root1 -Case case2) + assert ($r -like 'case : *\case2') + assert ($r -like 'var1 : changed by case2') +} diff --git a/Tasks/SubCases/README.md b/Tasks/SubCases/README.md new file mode 100644 index 0000000..3162984 --- /dev/null +++ b/Tasks/SubCases/README.md @@ -0,0 +1,46 @@ +# SubCases + +Inspired by [#222](https://github.com/nightroman/Invoke-Build/issues/222). + +**File structure** + +- `root.build.ps1` +- `case1` + - `case1.tasks.ps1` +- `case2` + - `case2.tasks.ps1` + +**Build parameters** + +```powershell +param( + [Parameter(Mandatory=1)] + [string]$Case + , + [string]$Configuration = 'Release' +) +``` + +In this example the parameter `$Case` is mandatory and the specified case script is always dot-sourced. + +But the script could be designed with an optional `$Case` or using the default case. + +Examples: + +```powershell +# case1, default task with default parameters +Invoke-Build -Case case1 + +# case2, common task with custom parameters +Invoke-Build root1 -Case case2 -Configuration Debug +``` + +**Some features** + +Let's use the term "assets" for script scope variables and functions. + +- `Invoke-Build` without `-File` works in the root and child folders. +- The root script parameters and assets are available for case tasks. +- The case specific assets are available for common root tasks. +- Cases may change root assets defined before dot-sourcing. +- See features in action shown by [.test.ps1](.test.ps1). diff --git a/Tasks/SubCases/case1/case1.tasks.ps1 b/Tasks/SubCases/case1/case1.tasks.ps1 new file mode 100644 index 0000000..d8ba916 --- /dev/null +++ b/Tasks/SubCases/case1/case1.tasks.ps1 @@ -0,0 +1,11 @@ + +# Case assets, may be used by case tasks and root tasks. +$CaseRoot = $PSScriptRoot + +# Synopsis: This task is case specific. +# It may use assets from the root script. +task task1 { + "case file : $CaseFile" + "configuration : $Configuration" + "var1 : $Var1" +} diff --git a/Tasks/SubCases/case2/case2.tasks.ps1 b/Tasks/SubCases/case2/case2.tasks.ps1 new file mode 100644 index 0000000..4c39c12 --- /dev/null +++ b/Tasks/SubCases/case2/case2.tasks.ps1 @@ -0,0 +1,14 @@ + +# This case overrides the root variable. +$Var1 = 'changed by case2' + +# Case assets, may be used by case tasks and root tasks. +$CaseRoot = $PSScriptRoot + +# Synopsis: This task is case specific. +# It may use assets from the root script. +task task1 { + "case file : $CaseFile" + "configuration : $Configuration" + "var1 : $Var1" +} diff --git a/Tasks/SubCases/root.build.ps1 b/Tasks/SubCases/root.build.ps1 new file mode 100644 index 0000000..66b897a --- /dev/null +++ b/Tasks/SubCases/root.build.ps1 @@ -0,0 +1,21 @@ +param( + [Parameter(Mandatory=1)] + [string]$Case + , + [string]$Configuration = 'Release' +) + +# Common variables. Case scripts may use them and may override them. +$Var1 = 'root value 1' + +# Dot-source the specified case tasks. +$CaseFile = "$Case\$Case.tasks.ps1" +requires -Path $CaseFile +. $CaseFile + +# Synopsis: This task is common for all cases. +# It may use assets from the case script. +task root1 { + "case : $CaseRoot" # expected to be defined by cases + "var1 : $Var1" # defined by root, may be changed by cases (case2) +}