In the post about how to make a module with Azure DevOps, I mention a file, build.ps1. Basically, I tell you to copy what I got. Maybe you want to understand what I’m trying to do in that file. Here’s that explanation:
- Initialization:
- It sets
$buildVersion
to the value of theBUILDVER
environment variable. - It determines the path of the current script’s directory and assigns it to
$projectRoot
. - The
projectRoot
variable is then saved as a pipeline variable usingWrite-Host
. This and a few more to come are used in other steps of the process. Saving as pipeline variable allows me to find the value here and use it later.
- It sets
- Reading Configuration:
- The script reads a JSON configuration file named
module-config.json
located in the project root directory.
- The script reads a JSON configuration file named
- Checking for Public Folder:
- The script checks if there’s a Public folder in the project root directory, throwing an error if not found. This folder is critical to the module and should contain all your PowerShell functions.
- Checking for Private Folder:
- Similarly, it checks for a Private folder in the project root directory.
- If this folder exists, it sets the
$hasPrivateFunctions
variable to$true
and saves it as a pipeline variable.
- Checking for Tests Folder:
- The script looks for a Tests folder in the project root directory.
- If it finds one, it sets the
$hasPesterTests
variable to$true
and saves it as a pipeline variable.
- Assembling the Manifest Data:
- The script then enters the Manifest region, which is responsible for building the module manifest file.
- It sets the
$manifestPath
variable to the path of the module manifest file, which is located in the project root directory and has the same name as the module, followed by the.psd1
extension. - The
$copyrightDate
variable is set based on theCopyrightStartYear
property in the configuration file. If the start year is the current year, only the current year is used. Otherwise, a range of years is used.
- Creating the Module Manifest:
- Finally, the script creates a new module manifest file (
*.psd1
) with the specified metadata.
- Finally, the script creates a new module manifest file (
- Loads the Functions:
- Updates the manifest file with the public functions found in the Public folder.
- Create Nuspec File:
- The next region is for finding the values and saving Nuspec file.
- Builds a NuGet package (*.nuspec) for the module, including the module files and any private functions or Pester tests.
- Create Module File:
- In a simple module, this file might contain all the functions for the module. In this case, we have them in separate files.
- This exports PowerShell code into a module file (*.psm1) that will dynamically load all the functions in the public and private folders into the current session. The list of functions in the public folder will be exported as part of the module.
Comments
One response to “Building a Module”
[…] If you want an explanation of what’s going on inside this file, see this post. […]