You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This project is an attempt by a group of PowerShell MVPs and module authors to:
3
+
This module makes it easier to break up your module source into several files for organization, even though you need to ship it as one big psm1 file.
4
4
5
-
1. Build a common set of [tools for module authoring](#whats-in-the-module-so-far)
6
-
2. Encourage a common pattern for [organizing PowerShell module projects](#organizing-your-module)
7
-
3. Promote best practices for authoring functions and modules
5
+
There are still some issues in Visual Studio Code and PSScriptAnalyzer when authoring modules as multiple files, but if you want to break up your module into multiple files for organization and maintainability, and still need to ship it as one big file for performance and compatibility reasons, this module is for you.
8
6
9
-
In short, we want to make it easier for people to write great code and produce great modules.
7
+
## You should ship your module as one big file!
10
8
11
-
In service of this goal, we intend to produce:
9
+
PowerShell expects script modules to be all in one file. A module in a single `.psm1` script file results in the best performance, natural "script" scope, and full support for classes and "using" statements.
12
10
13
-
1. Guidance on using the best of the existing tools: dotnet, Pester, PSDepends, etc.
14
-
2. Module templates demonstrating best practices for organization
15
-
3. Function templates demonstrating best practices for common parameters and error handling
16
-
4. ModuleBuilder module - a set of tools for building modules following these best practices
11
+
The single file option is particularly important for performance if you are signing your module (or your end users want to be able to code-sign it), because each file's signature must be checked, and each certificate must be checked against CRLs. It's also critical if you are using PowerShell classes (the `using` statement only supports classes defined in the root psm1 file). It's basically required if you want to use module-scope variables to share state between functions in your module.
17
12
18
-
## The ModuleBuilder module
13
+
## What's in the ModuleBuilder module so far?
19
14
20
-
This module is the main output of the project, consisting of one primary command: `Build-Module` and a few helpers to translate input and output line numbers. It represents the collaboration of several module authors who had each written their own version of these tools for themselves, and have now decided to collaborate on creating a shared tool set. We are each using the patterns and tools that are represented here, and are committed to helping others to succeed at doing so.
15
+
This module is the main output of the project, consisting of one primary command: `Build-Module` and a few helpers to translate input and output line numbers so you can trouble-shoot error messages from your module against the source files.
21
16
22
-
### What's in the module so far
23
-
24
-
#### Build-Module
17
+
### Build-Module
25
18
26
19
Builds a script module from a source project containing one file per function in `Public` and `Private` folders.
27
20
28
21
The `Build-Module` command is a build task for PowerShell script modules that supports [incremental builds](https://docs.microsoft.com/en-us/visualstudio/msbuild/incremental-builds).
29
22
30
-
####Convert-CodeCoverage
23
+
### Convert-CodeCoverage
31
24
32
25
Takes the output from `Invoke-Pester -Passthru` run against the build output, and converts the code coverage report to the source lines.
33
26
34
-
####ConvertFrom-SourceLineNumber
27
+
### ConvertFrom-SourceLineNumber
35
28
36
29
Converts a line number from a source file to the corresponding line number in the built output.
37
30
38
-
####ConvertTo-SourceLineNumber
31
+
### ConvertTo-SourceLineNumber
39
32
40
33
Converts a line number from the built output to the corresponding file and line number in the source.
41
34
42
-
####Convert-Breakpoint
35
+
### Convert-Breakpoint
43
36
44
37
Convert any breakpoints on source files to module files _and vice-versa_.
45
38
46
39
## Organizing Your Module
47
40
48
-
For best results, you need to organize your module project similarly to how this project is organized. It doesn't have to be exact, because nearly all of our conventions can be overriden, but the module _is_ opinionated, so if you follow the conventions, it should feel wonderfully automatic.
41
+
For best results, you need to organize your module project similarly to how this project is organized. It doesn't have to be exact, because you can override nearly all of our conventions, but the module _is_ opinionated, so if you follow the conventions, it should feel wonderfully automatic.
49
42
50
-
1. Create a `source` folder with a `build.psd1` file and your module manifest in it
43
+
1. Create a `source`(or `src`) folder with a `build.psd1` file and your module manifest in it
51
44
2. In the `build.psd1` specify the relative **Path** to your module's manifest, e.g. `@{ Path = "ModuleBuilder.psd1" }`
52
45
3. In your manifest, make sure a few values are not commented out. You can leave them empty, because they'll be overwritten:
53
46
-`FunctionsToExport` will be updated with the _file names_ that match the `PublicFilter`
54
47
-`AliasesToExport` will be updated with the values from `[Alias()]` attributes on commands
55
48
-`Prerelease` and `ReleaseNotes` in the `PSData` hash table in `PrivateData`
56
49
57
-
Once you start working on the module, you'll create sub-folders in source, and put script files in them with only **one** function in each file. You should name the files with _the same name_ as the function that's in them -- especially in the public folder, where we use the file name (without the extension) to determine the exported functions.
50
+
Once you start working on the module, you'll create sub-folders in source, and put script files in them with only **one** function in each file. You should name the files with _the same name_ as the function that's in them -- especially in the `source\public` folder, where we use the file names to determine the exported functions.
58
51
59
-
1. By convention, use folders named "Classes" (and/or "Enum"), "Private", and "Public"
60
-
2. By convention, the functions in "Public" will be exported from the module (you can override the `PublicFilter`)
52
+
1. By convention, use SourceDirectories named "Classes" (and/or "Enum"), "Private", and "Public"
53
+
2. By convention, the PublicFilter is all of the functions in the "Public" directory.
61
54
3. To force classes to be in a certain order, you can prefix their file names with numbers, like `01-User.ps1`
62
55
63
-
There are a _lot_ of conventions in `Build-Module`, expressed as default values for its parameters. These defaults are documented in the help for Build-Module. You can override any parameter defaults by adding keys to the `build.psd1` file with your preferences, or by passing the values to the `Build-Module` command directly.
56
+
There are a _lot_ of conventions in `Build-Module`, expressed as default values for its parameters. These defaults are documented in the help for Build-Module, and you can override any parameter defaults by adding keys to the `build.psd1` file with your preferences, or by passing the values to the `Build-Module` command directly. So in other words, you can override the default `SourceDirectories` and `PublicFilters` (and any others) by adding them to the `build.psd1` file.
Once you've cloned both, the `Build.build.ps1` script will use the shared [Tasks\_Bootstrap.ps1](https://github.com/PoshCode/Tasks/blob/main/_Bootstrap.ps1) to install the other dependencies (see [RequiredModules.psd1](https://github.com/PoshCode/ModuleBuilder/blob/main/RequiredModules.psd1)), including [dotnet](https://dot.net), and will use [Invoke-Build](https://github.com/nightroman/Invoke-Build) and [Pester](https://github.com/Pester/Pester) to build and test the module.
89
+
Once you've cloned both, the `Build.build.ps1` script will use the shared [Tasks\_Bootstrap.ps1](https://github.com/PoshCode/Tasks/blob/main/_Bootstrap.ps1) to install the other dependencies (see [build.requires.psd1](https://github.com/PoshCode/ModuleBuilder/blob/main/build.requires.psd1)), including [dotnet](https://dot.net), and will use [Invoke-Build](https://github.com/nightroman/Invoke-Build) and [Pester](https://github.com/Pester/Pester) to build and test the module.
97
90
98
91
```powershell
99
92
cd ModuleBuilder
@@ -102,23 +95,18 @@ cd ModuleBuilder
102
95
103
96
This _should_ work on Windows, Linux, or MacOS. I test the build process on Windows, and in CI we run it in the Linux containers via earthly, and we run the full Pester test suit on all three platforms.
104
97
105
-
#### The old-fashioned way
98
+
##Most recent releases
106
99
107
-
You _can_ build the module without any additional tools (and without running tests), by using the old `build.ps1` bootstrap script. You'll need to pass a version number in, and if you have [Pester](https://github.com/Pester/Pester) and [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer), you can run the 'test.ps1' script to run the tests.
Script Generators let developers modify their module's source code as it is being built. A generator can create new script functions on the fly, such that whole functions are added to the built module. A generator can also inject boilerplate code like error handling, logging, tracing and timing at build-time, so this code can be maintained once, and be automatically added (and updated) in all the places where it's needed when the module is built. The generators run during the build and can inspect existing functions, data files, or even data from an API, and produce code that is output into the module (and clearly marked as generated).
103
+
104
+
### 3.1.0 - Supports help outside the top of script commands
113
105
114
-
## Changelog
106
+
Starting with this release, ModuleBuilder adds an empty line between the `#REGION filename` comment lines it injects, and the content of the files. This allows PowerShell to recognize help comments that are at the top of each file (outside the function block).
115
107
116
-
### 3.0.0 - Now with better alias support
108
+
### 3.0.0 - Better alias support
117
109
118
110
Starting with this release, ModuleBuilder will automatically export aliases from `New-Alias` and `Set-Alias` as well as the `[Alias()]` attributes on commands. This is (probably not) a breaking change, but because it can change the aliases exported by existing modules that use ModuleBuilder, I've bumped the major version number as a precaution (if you're reading this, mission accomplished).
119
111
120
112
Additionally, the `Build-Module` command now _explicitly sorts_ the source files into alphabetical order, to ensure consistent behavior regardless of the native order of the underlying file system. This is technically also a breaking change, but it's unlikely to affect anyone except the people whose builds didn't work on non-Windows systems because of the previous behavior.
121
-
122
-
### 3.1.0 - Now allows help outside the top of script commands
123
-
124
-
Starting with this release, ModuleBuilder adds an empty line between the `#REGION filename` comment lines it injects, and the content of the files. This allows PowerShell to recognize help comments that are at the top of each file (outside the function block).
0 commit comments