FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

PSScriptBuilder/examples/09-module-build at main · PSScriptBuilder/PSScriptBuilder · GitHub

Latest commit

 

History

History

README.md

Example 09 - Module Build

This example shows how PSScriptBuilder builds a PowerShell module instead of a standalone script. The output is a .psm1 file that is loaded via using module in a separate demo script, giving callers access to the module's types (ConfigEntry, AppConfig) as strongly-typed objects.

New in this example

  • Module output - the template produces a .psm1 file, not a .ps1 script
  • Demo-Module.ps1 - a separate script that loads the built module with using module
  • Run-Example.ps1 calls Demo-Module.ps1 via & because using module must appear at the top of a file
  • AppConfig.psd1 module manifest is pre-existing; only the .psm1 body is generated by the build

Key concepts

using module makes the types defined in a module available as first-class PowerShell types in the consuming script. Without it, objects returned by module functions have the correct data but their type names are not accessible for things like [ConfigEntry] parameter types or $obj.GetType().Name checks.

Why a separate demo script? The using module statement must appear before any executable code in a script file. Because Run-Example.ps1 has already executed code (the build), it cannot issue using module itself. Calling a child script with & starts a clean parsing scope where using module is valid.

The module manifest (AppConfig.psd1) is committed to source control and points to AppConfig.psm1 as the root module. PSScriptBuilder only generates the .psm1 body - the manifest stays unchanged across builds.

Project structure

09-module-build/
+-- Run-Example.ps1                 Entry point - builds module, then calls Demo-Module.ps1
+-- Demo-Module.ps1                 Loads built module via 'using module', demonstrates types
+-- README.md                       This file
+-- psscriptbuilder.config.json     Build configuration
+-- src/
|   +-- Classes/
|   |   +-- ConfigEntry.ps1         Class: Key, Value, Description; ToString()
|   |   +-- AppConfig.ps1           Class: Name, Entries; Add(), Get(), Contains(), Count()
|   +-- Functions/
|       +-- New-ConfigEntry.ps1     Factory function, returns [ConfigEntry]
|       +-- New-AppConfig.ps1       Factory function, returns [AppConfig]
|       +-- Get-ConfigValue.ps1     Gets a value by key, supports -Default parameter
+-- build/
    +-- Templates/
    |   +-- AppConfig.psm1.template Template with {{CLASS_DEFINITIONS}} and {{FUNCTION_DEFINITIONS}}
    +-- Output/
        +-- AppConfig.psd1          Module manifest (pre-existing, not generated)

How to run

Warning: Run in a fresh PowerShell session
The using module statement loads types into the current session.
Reloading or switching between examples in the same session can cause type conflicts.

.\Run-Example.ps1

For detailed build output:

.\Run-Example.ps1 -Verbose

How it works

  1. Set-PSScriptBuilderProjectRoot reads psscriptbuilder.config.json to resolve template and output paths
  2. A ContentCollector is created with a ClassCollector for src\Classes\ and a FunctionCollector for src\Functions\
  3. Invoke-PSScriptBuilderBuild analyzes type dependencies, sorts classes and functions, fills AppConfig.psm1.template, and writes build\Output\AppConfig.psm1
  4. Run-Example.ps1 calls Demo-Module.ps1 as a child script via &
  5. Demo-Module.ps1 loads the built module with using module .\build\Output\AppConfig.psd1 and works with typed ConfigEntry and AppConfig objects

Expected output

Build Summary
  Output: ...\build\Output\AppConfig.psm1
  Size  : 2.02 KB
  Time  : 1.03 s

Components
  Classes  : 2
  Functions: 3
  Total    : 5

--- Running Demo-Module.ps1 ---
Config   : AppSettings
Entries  : 4

  Environment     = Production  # Deployment environment
  LogLevel        = Warning  # Minimum log level
  MaxRetries      = 3  # Maximum retry attempts
  Timeout         = 30

Environment : Production
ApiKey      : (not set)

Type of entry     : ConfigEntry
Type of config    : AppConfig

Back | FazBrowse Home | New Git URL