Skip to content

Commit

Permalink
Add Tasks/SubCases
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Aug 2, 2024
1 parent 8e1cee3 commit a8e186b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Show-BuildMermaid.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ $text = @(
@'
</pre>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10.8.0/dist/mermaid.esm.min.mjs';
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10.9.1/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
</body>
Expand Down
3 changes: 2 additions & 1 deletion Tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions Tasks/SubCases/.test.ps1
Original file line number Diff line number Diff line change
@@ -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')
}
46 changes: 46 additions & 0 deletions Tasks/SubCases/README.md
Original file line number Diff line number Diff line change
@@ -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).
11 changes: 11 additions & 0 deletions Tasks/SubCases/case1/case1.tasks.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
14 changes: 14 additions & 0 deletions Tasks/SubCases/case2/case2.tasks.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
21 changes: 21 additions & 0 deletions Tasks/SubCases/root.build.ps1
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a8e186b

Please sign in to comment.