Windows-powershell / PowerShell-master /.github /instructions /powershell-module-organization.instructions.md
metadata
applyTo:
- tools/ci.psm1
- build.psm1
- tools/packaging/**/*.psm1
- .github/**/*.yml
- .github/**/*.yaml
Guidelines for PowerShell Code Organization
When to Move Code from YAML to PowerShell Modules
PowerShell code in GitHub Actions YAML files should be kept minimal. Move code to a module when:
Size Threshold
- More than ~30 lines of PowerShell in a YAML file step
- Any use of .NET types like
[regex],[System.IO.Path], etc. - Complex logic requiring multiple nested loops or conditionals
- Reusable functionality that might be needed elsewhere
Indicators to Move Code
- Using .NET type accelerators (
[regex],[PSCustomObject], etc.) - Complex string manipulation or parsing
- File system operations beyond basic reads/writes
- Logic that would benefit from unit testing
- Code that's difficult to read/maintain in YAML format
Which Module to Use
ci.psm1 (tools/ci.psm1)
Purpose: CI/CD-specific operations and workflows
Use for:
- Build orchestration (invoking builds, tests, packaging)
- CI environment setup and configuration
- Test execution and result processing
- Artifact handling and publishing
- CI-specific validations and checks
- Environment variable management for CI
Examples:
Invoke-CIBuild- Orchestrates build processInvoke-CITest- Runs Pester testsTest-MergeConflictMarker- Validates files for conflictsSet-BuildVariable- Manages CI variables
When NOT to use:
- Core build operations (use build.psm1)
- Package creation logic (use packaging.psm1)
- Platform-specific build steps
build.psm1 (build.psm1)
Purpose: Core build operations and utilities
Use for:
- Compiling source code
- Resource generation
- Build configuration management
- Core build utilities (New-PSOptions, Get-PSOutput, etc.)
- Bootstrap operations
- Cross-platform build helpers
Examples:
Start-PSBuild- Main build functionStart-PSBootstrap- Bootstrap dependenciesNew-PSOptions- Create build configurationStart-ResGen- Generate resources
When NOT to use:
- CI workflow orchestration (use ci.psm1)
- Package creation (use packaging.psm1)
- Test execution
packaging.psm1 (tools/packaging/packaging.psm1)
Purpose: Package creation and distribution
Use for:
- Creating distribution packages (MSI, RPM, DEB, etc.)
- Package-specific metadata generation
- Package signing operations
- Platform-specific packaging logic
Examples:
Start-PSPackage- Create packagesNew-MSIXPackage- Create Windows MSIXNew-DotnetSdkContainerFxdPackage- Create container packages
When NOT to use:
- Building binaries (use build.psm1)
- Running tests (use ci.psm1)
- General utilities
Best Practices
Keep YAML Minimal
# β Bad - too much logic in YAML
- name: Check files
shell: pwsh
run: |
$files = Get-ChildItem -Recurse
foreach ($file in $files) {
$content = Get-Content $file -Raw
if ($content -match $pattern) {
# ... complex processing ...
}
}
# β
Good - call function from module
- name: Check files
shell: pwsh
run: |
Import-Module ./tools/ci.psm1
Test-SomeCondition -Path ${{ github.workspace }}
Document Functions
Always include comment-based help for functions:
function Test-MyFunction
{
<#
.SYNOPSIS
Brief description
.DESCRIPTION
Detailed description
.PARAMETER ParameterName
Parameter description
.EXAMPLE
Test-MyFunction -ParameterName Value
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $ParameterName
)
# Implementation
}
Error Handling
Use proper error handling in modules:
try {
# Operation
}
catch {
Write-Error "Detailed error message: $_"
throw
}
Verbose Output
Use Write-Verbose for debugging information:
Write-Verbose "Processing file: $filePath"
Module Dependencies
- ci.psm1 imports both
build.psm1andpackaging.psm1 - build.psm1 is standalone (minimal dependencies)
- packaging.psm1 imports
build.psm1
When adding new functions, consider these import relationships to avoid circular dependencies.
Testing Modules
Functions in modules should be testable:
# Test locally
Import-Module ./tools/ci.psm1 -Force
Test-MyFunction -Parameter Value
# Can be unit tested with Pester
Describe "Test-MyFunction" {
It "Should return expected result" {
# Test implementation
}
}
Migration Checklist
When moving code from YAML to a module:
- β Determine which module is appropriate (ci, build, or packaging)
- β Create function with proper parameter validation
- β Add comment-based help documentation
- β
Use
[CmdletBinding()]for advanced function features - β Include error handling
- β Add verbose output for debugging
- β Test the function independently
- β Update YAML to call the new function
- β Verify the workflow still works end-to-end