Skip to content

Commit 7088e53

Browse files
committed
Clarify parameters vs properties with decision guide and flow diagram
Enhanced the parameters-properties.md documentation to address common confusion about when to use each approach: - Added "Quick Decision Guide" section upfront for immediate clarity - Included mermaid sequence diagram showing execution flow and variable lifespan throughout the build process - Restructured content with clear "When to Use" sections for both parameters (input values) and properties (final overrides) - Added side-by-side comparison examples demonstrating the timing difference - Included comparison table highlighting key differences - Added "Best Practices" section with real-world guidance - Added warning about properties not recomputing derived values - Concluded with summary and "Rule of Thumb" decision tip The document now provides a clear decision framework for users to determine which approach to use based on whether values are used for computation or are final overrides.
1 parent e0c67ab commit 7088e53

File tree

1 file changed

+248
-49
lines changed

1 file changed

+248
-49
lines changed

docs/tutorial-basics/parameters-properties.md

Lines changed: 248 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,286 @@ description: "How to use parameters and properties in your build script."
44
sidebar_position: 11
55
---
66

7-
To summarize the differences between passing parameters and properties to the
8-
`Invoke-psake` function:
7+
Parameters and properties are two different ways to pass values into your psake build scripts. Understanding when to use each one is crucial for creating flexible, maintainable build automation.
98

10-
* Parameters and "properties" can both be passed to the Invoke-psake function
11-
simultaneously
12-
* Parameters are set before any "properties" blocks are run
13-
* Properties are set after all "properties" blocks have run
9+
## Quick Decision Guide
1410

15-
## Parameters
11+
**Use Parameters when:**
12+
- You need to pass external values that your properties blocks will use to compute derived values
13+
- You want to provide inputs that determine how properties are calculated
14+
- You're passing configuration from CI/CD systems or command-line arguments
1615

17-
You can pass parameters to your build script using the "parameters" parameter of
18-
the Invoke-psake function. The following is an example:
16+
**Use Properties when:**
17+
- You need to override final property values after all computations are complete
18+
- You want to replace default values defined in your build script
19+
- You're overriding environment-specific settings (like connection strings or paths)
1920

20-
```powershell
21-
Invoke-psake .\parameters.ps1 -parameters @{"p1"="v1";"p2"="v2"}
21+
## Execution Flow & Lifespan
22+
23+
Understanding the execution order is key to choosing between parameters and properties:
24+
25+
```mermaid
26+
sequenceDiagram
27+
participant Invoke as Invoke-psake
28+
participant Params as Parameters
29+
participant Props as Properties Blocks
30+
participant Override as Properties Override
31+
participant Tasks as Task Execution
32+
33+
Invoke->>Params: 1. Set -parameters hashtable values
34+
Note over Params: Variables available to script
35+
Invoke->>Props: 2. Execute all properties { } blocks
36+
Note over Props: Compute values using parameters
37+
Invoke->>Override: 3. Apply -properties hashtable overrides
38+
Note over Override: Replace computed values
39+
Invoke->>Tasks: 4. Execute tasks
40+
Note over Tasks: Use final property values
2241
```
2342

24-
The example above runs the build script called `parameters.ps1` and passes in
25-
parameters `p1` and `p2` with values `v1` and `v2`. The parameter value for the
26-
"parameters" parameter (say that 10 times really fast!) is a PowerShell
27-
hashtable where the name and value of each parameter is specified.
43+
**Key Insight:** Parameters are set **before** properties blocks run, while property overrides are applied **after** all properties blocks have executed.
2844

29-
:::note
45+
## When to Use Each
3046

31-
You don't need to use the "$" character when specifying the parameter names in
32-
the hashtable.
47+
### Use Parameters For: Input Values
3348

34-
:::
49+
Parameters provide **input values** that your build script can use during the properties block execution phase. They're ideal for:
50+
51+
- Build configuration values (Debug/Release)
52+
- Version numbers to be incorporated into file paths
53+
- Feature flags that affect property calculations
54+
- CI/CD system variables
55+
56+
**Example:** Using parameters to compute derived properties
3557

3658
```powershell title="parameters.ps1"
3759
properties {
38-
$my_property = $p1 + $p2
60+
$config = $configuration # Use parameter value
61+
$outputPath = "bin\$config" # Compute derived path
62+
$version = "$majorVersion.$minorVersion.0" # Build version string
3963
}
4064
41-
task default -depends TestParams
65+
task default -depends Build
4266
43-
task TestParams {
44-
Assert ($my_property -ne $null) '$my_property should not be null'
67+
task Build {
68+
Write-Host "Building to: $outputPath"
69+
Write-Host "Version: $version"
4570
}
4671
```
4772

48-
The Assert in this example would pass because when it runs, `$my_property`
49-
would be set to `v1v2` and not be `$null`.
73+
```powershell title="Invocation"
74+
# Pass input values that properties blocks will use
75+
Invoke-psake .\parameters.ps1 -parameters @{
76+
"configuration" = "Release"
77+
"majorVersion" = "2"
78+
"minorVersion" = "1"
79+
}
5080
51-
## Properties
81+
# Output:
82+
# Building to: bin\Release
83+
# Version: 2.1.0
84+
```
5285

53-
You can override a property in your build script using the `properties`
54-
parameter of the Invoke-psake function. The following is an example:
86+
### Use Properties For: Final Value Overrides
5587

56-
```powershell
57-
Invoke-psake .\properties.ps1 -properties @{"x"="1";"y"="2"}
88+
Properties provide **final value overrides** that replace values after all properties blocks have run. They're ideal for:
89+
90+
- Overriding default values for different environments
91+
- Replacing computed values in specific scenarios
92+
- Emergency hotfix values that bypass normal logic
93+
94+
**Example:** Overriding final property values
95+
96+
```powershell title="properties.ps1"
97+
properties {
98+
$server = "localhost" # Default value
99+
$database = "DevDB" # Default value
100+
$connectionString = "Server=$server;Database=$database"
101+
}
102+
103+
task default -depends Deploy
104+
105+
task Deploy {
106+
Write-Host "Deploying to: $connectionString"
107+
}
58108
```
59109

60-
The example above runs the build script called `properties.ps1` and passes in
61-
parameters `x` and `y` with values `1` and `2`. The parameter value for the
62-
"properties" parameter is a PowerShell hashtable where the name and value of
63-
each property is specified.
110+
```powershell title="Invocation"
111+
# Override final values after properties blocks run
112+
Invoke-psake .\properties.ps1 -properties @{
113+
"server" = "prod-sql-01"
114+
"database" = "ProductionDB"
115+
}
116+
117+
# Output:
118+
# Deploying to: Server=prod-sql-01;Database=ProductionDB
119+
# Note: $connectionString still has the OLD computed value!
120+
```
64121

65-
:::note
122+
:::warning Properties Don't Recompute Derived Values
66123

67-
You don't need to use the "$" character when specifying the property names in
68-
the hashtable.
124+
When you override a property, any derived values that were computed from it in the properties block are **NOT** automatically recalculated. In the example above, `$connectionString` would still contain the original computed value unless you override it directly as well.
69125

70126
:::
71127

72-
```powershell title="properties.ps1"
128+
## Comparison: Side by Side
129+
130+
Let's see the difference with a concrete example:
131+
132+
```powershell title="build.ps1"
133+
properties {
134+
$env = $environment # Will be set if passed as parameter
135+
$deployPath = "C:\Deploy\$env" # Computed from $env
136+
}
137+
138+
task default -depends Info
139+
140+
task Info {
141+
Write-Host "Environment: $env"
142+
Write-Host "Deploy Path: $deployPath"
143+
}
144+
```
145+
146+
### Using Parameters (Input Values)
147+
148+
```powershell
149+
Invoke-psake .\build.ps1 -parameters @{"environment"="Production"}
150+
151+
# Output:
152+
# Environment: Production
153+
# Deploy Path: C:\Deploy\Production
154+
```
155+
156+
✅ The `$deployPath` property is correctly computed using the parameter value.
157+
158+
### Using Properties (Override After Computation)
159+
160+
```powershell
161+
Invoke-psake .\build.ps1 -properties @{"environment"="Production"}
162+
163+
# Output:
164+
# Environment: Production
165+
# Deploy Path: C:\Deploy\
166+
```
167+
168+
❌ The `$deployPath` is computed BEFORE the override, so it uses `$null` for `$env`.
169+
170+
### Using Both Together
171+
172+
```powershell
173+
Invoke-psake .\build.ps1 `
174+
-parameters @{"environment"="Production"} `
175+
-properties @{"deployPath"="\\FileServer\Builds\Prod"}
176+
177+
# Output:
178+
# Environment: Production
179+
# Deploy Path: \\FileServer\Builds\Prod
180+
```
181+
182+
✅ Parameter provides input for computation, property overrides the final value.
183+
184+
## Comparison Table
185+
186+
| Aspect | Parameters | Properties |
187+
|--------|-----------|-----------|
188+
| **Timing** | Set before properties blocks run | Applied after all properties blocks complete |
189+
| **Purpose** | Provide inputs for computation | Override final computed values |
190+
| **Derived Values** | Can be used to compute other properties | Do NOT trigger recomputation |
191+
| **Best For** | Configuration inputs, build settings | Environment-specific overrides |
192+
| **Use Case** | "I need this value to calculate others" | "I need to replace this final value" |
193+
194+
## Best Practices
195+
196+
### 1. Prefer Parameters for Inputs
197+
198+
When you need to pass values that affect multiple properties, use parameters:
199+
200+
```powershell
201+
# ✅ Good: Use parameters for inputs
202+
Invoke-psake .\build.ps1 -parameters @{
203+
"configuration" = "Release"
204+
"platform" = "x64"
205+
}
206+
207+
# Properties block can use these to compute paths
208+
properties {
209+
$config = $configuration
210+
$plat = $platform
211+
$outputPath = "bin\$plat\$config"
212+
}
213+
```
214+
215+
### 2. Use Properties for Environment-Specific Overrides
216+
217+
When deploying to different environments, use properties to override specific values:
218+
219+
```powershell
220+
# ✅ Good: Override environment-specific values
221+
Invoke-psake .\deploy.ps1 `
222+
-parameters @{"environment"="Production"} `
223+
-properties @{
224+
"serverName" = "prod-web-01"
225+
"connectionString" = "Server=prod-sql-01;..."
226+
}
227+
```
228+
229+
### 3. Document Which Approach Your Script Expects
230+
231+
In your build script, document which values should be passed as parameters vs properties:
232+
233+
```powershell
234+
<#
235+
.SYNOPSIS
236+
Build automation script
237+
238+
.PARAMETER parameters
239+
Expected parameters:
240+
- configuration: Build configuration (Debug/Release)
241+
- version: Version number for the build
242+
243+
.PARAMETER properties
244+
Optional property overrides:
245+
- outputPath: Override computed output path
246+
- deployServer: Override target deployment server
247+
#>
248+
73249
properties {
74-
$x = $null
75-
$y = $null
76-
$z = $null
250+
$config = $configuration # From -parameters
251+
$ver = $version # From -parameters
252+
$outputPath = "bin\$config\$ver" # Computed
253+
$deployServer = "localhost" # Default (can override with -properties)
77254
}
255+
```
78256

79-
task default -depends TestProperties
257+
### 4. Avoid Overriding Properties That Other Properties Depend On
80258

81-
task TestProperties {
82-
Assert ($x -ne $null) "x should not be null"
83-
Assert ($y -ne $null) "y should not be null"
84-
Assert ($z -eq $null) "z should be null"
259+
If you override a property that other properties are computed from, the dependent properties won't update:
260+
261+
```powershell
262+
# ❌ Bad: Overriding $buildRoot won't affect $outputPath
263+
properties {
264+
$buildRoot = "C:\Builds"
265+
$outputPath = "$buildRoot\Output"
85266
}
267+
268+
Invoke-psake .\build.ps1 -properties @{"buildRoot"="D:\Builds"}
269+
# Result: $buildRoot = "D:\Builds", but $outputPath = "C:\Builds\Output"
270+
271+
# ✅ Good: Pass as parameter so computation uses the right value
272+
Invoke-psake .\build.ps1 -parameters @{"buildRoot"="D:\Builds"}
273+
# Result: $buildRoot = "D:\Builds" AND $outputPath = "D:\Builds\Output"
86274
```
87275

88-
The value of `$x` should be `1` and `$y` should be `2` by the time the
89-
`TestProperties` task is executed. The value of `$z` was not overridden so it
90-
should still be `$null`.
276+
## Summary
277+
278+
- **Parameters** = inputs set **before** properties blocks run → use for values that affect computations
279+
- **Properties** = overrides applied **after** properties blocks run → use to replace final values
280+
- Both can be used together for maximum flexibility
281+
- When in doubt, prefer parameters for inputs and reserve properties for environment-specific overrides
282+
283+
:::tip Rule of Thumb
284+
285+
Ask yourself: "Does this value need to be used to calculate other properties?"
286+
- **Yes** → Use `-parameters`
287+
- **No** → Use `-properties`
288+
289+
:::

0 commit comments

Comments
 (0)