ModuleBuilder Documentation
repository·main·Indexed 19 days ago
https://github.com/poshcode/modulebuilderA PowerShell tool for authoring modules using a multi-file structure that compiles into a single, high-performance .psm1 file. It simplifies the development lifecycle by providing tools for module creation, version management, signing, packaging, and publishing. Key features include the Build-Module command, script generators for Aspect-Oriented Programming (AOP), and utilities for translating line numbers and breakpoints between source files and the compiled output.
What's inside ModuleBuilder
- ModuleBuilder is a PowerShell module designed to simplify the entire lifecycle of PowerShell script module development. It provides tools to help scripters write, version, sign, package, and publish their modules with increased ease and consistency. The module aims to provide a structured project format that facilitates both individual development and community contributions.
Overview of Module Builder
mainModule Builder is a tool designed to simplify the authoring of PowerShell script modules. It allows developers to organize their source code into multiple files (one per function) for better maintainability, while ultimately compiling them into a single
.psm1file.Shipping as a single file is recommended for:
- Performance: Faster loading and execution.
- Compatibility: Full support for PowerShell classes and
usingstatements. - Security: Easier code-signing, as only one file's signature needs to be checked.
- State Management: Enables the use of module-scope variables to share state between functions.
How Merge-ScriptBlock generator works
mainThe
Merge-ScriptBlockgenerator is used for Aspect-Oriented Programming (AOP). It wraps the content of thebegin,process, orendscript blocks of functions with providedboilerplatecode.You can use wildcard patterns to specify which functions should have this boilerplate injected. This is ideal for adding cross-cutting concerns like logging, timing, or error handling to all functions in a module without duplicating the code in every function.
ModuleBuilder Feature Capabilities
mainModuleBuilder provides a suite of features categorized into the following lifecycle stages:
Module Creation
- Generates build folders,
.psd1(module manifests), and.psm1(script module) files. - Facilitates GitHub repository creation and linking.
- Automates GitHub setup, including building Wiki pages, GitHub Pages sites, and generating
README.mdfiles with guided checklists. - Provides a wizard-driven UI to run cmdlets.
Module Management
- Handles version management.
- Manages help file creation and publication (e.g., converting comment-based help to XML/language-based help).
- Automatically adds functions and files to
.psd1and.psm1files based on naming conventions.
File Tools
- Cleans files (expanding aliases, formatting, etc.).
- Runs files against
PSAnalyzer. - Includes a GUI-based Function Builder to create function shells, parameters, and XML-based help files.
Testing, Signing, and Packaging
- Testing: Helps build test suites, inserts test folders, and provides AppVeyor setup (
.yml). - Signing: Automatically signs scripts during the release process.
- Packaging: Creates packages for OneGet, NuGet, or other formats.
- Release: Publishes modules to GitHub Releases, Chocolatey, or the PowerShell Gallery.
- Generates build folders,
How ConvertTo-Script generator works
mainTheConvertTo-Scriptgenerator is a packaging tool. It takes a complete module (including its assemblies) and the name of a specific function. It then outputs a standalone.ps1script file named after that function. This script contains the entire module embedded within it, allowing the single script to be distributed and run (e.g., from a network share) without requiring the module to be formally installed.How Move-UsingStatement generator works
mainTheMove-UsingStatementgenerator managesusingstatements. It comments outusingstatements found within individual source files, sorts them, and moves a single unique copy of each statement to the very top of the final, built module file. This allows you to keepusingstatements near the functions they serve in your source files while maintaining a clean, consolidated header in the output.How Add-Parameter generator works
mainTheAdd-Parametergenerator works in conjunction withMerge-ScriptBlock. It allows you to copy parameter sets from one script function to another. This enables developers to define common parameter sets once and apply them across multiple functions, while encapsulating implementation details within the injected script blocks.How Update-AliasesToExport generator works
mainTheUpdate-AliasesToExportgenerator modifies the module manifest rather than the source code. It scans for[Alias()]attributes,New-Alias,Set-Alias, andRemove-Aliascommands to automatically update the list of exported aliases in the final module manifest, ensuring all declared aliases are properly exported.Organize your PowerShell module repository for ModuleBuilder
mainModuleBuilder is an opinionated tool that expects a specific directory structure to automate compiling your module into a single
.psm1file, running Pester tests, and reporting accurate code coverage.To use ModuleBuilder, organize your repository following these conventions:
- Source Folder: Create a
Sourcefolder at the root of your repository. - Build Configuration: Place a
build.psd1file inside theSourcefolder. - Module Manifest: Place your module manifest (e.g.,
MyModule.psd1) inside theSourcefolder. - Function Organization:
- Create a
Privatefolder insideSourcefor internal functions. - Create a
Publicfolder insideSourcefor exported functions. - File Naming: Create one
.ps1file per function. Files in thePublicfolder must use theVerb-Nounformat (e.g.,Get-Data.ps1) as these will be exported without the extension.
- Create a
- Tests: Create a
Testsfolder at the root to house your Pester tests (organized intoPrivateandPublicsubfolders to match your source).
Recommended Directory Structure
MyModule ├───Source │ │ build.psd1 │ │ MyModule.psd1 │ │ ... │ ├───Private │ │ Internal-Function.ps1 │ └───Public │ │ Public-Function.ps1 └───Tests ├───Private └───Public- Source Folder: Create a
Create a custom dotnet template alias for ModuleBuilder
mainTo speed up your workflow, you can create a custom alias for the
PSModuleBuildertemplate. This allows you to pre-define common values like--authorand--companyso you only need to provide unique details (like the module name or description) when generating new modules.# Create an alias named 'psmo' with default author and company dotnet new -a psmo psmodulebuilder --author Jaykul --company PoshCode.org # Use the alias to quickly create a new module dotnet new psmo -o MyNewModule --description "My Brand New Module"Build a PowerShell module using Build-Module
mainTo compile your script files into a single module file, follow these steps:
- Run
Install-RequiredModuleto ensure dependencies are met. - Place all your
.ps1script files into a folder namedSource. - Execute
Build-Modulepointing to your source directory. - Retrieve the compiled module from the
Outputfolder.
Build-Module .\Source- Run
Build ModuleBuilder from source
mainYou can build the ModuleBuilder project itself using one of two methods:
Method 1: Using Earthbuild (Recommended)
This is the fastest method and uses containers to manage dependencies and parallelize steps. Requires WSL2, Docker Desktop, and the Earthbuild CLI.
git clone https://github.com/PoshCode/ModuleBuilder.git cd ModuleBuilder earth +testMethod 2: Manual Build (Without Earthbuild)
Requires the
.NET SDKto be installed manually. You must also clone theTasksrepository as a sibling to theModuleBuilderfolder.git clone https://github.com/PoshCode/ModuleBuilder.git git clone https://github.com/PoshCode/Tasks.git cd ModuleBuilder ./build.build.ps1