Using Stucco to Create Your PowerShell Module
Table of Contents
It all started one summer day… just kidding! You can skip to the “recipe” here…
It’s funny that I haven’t written this post before. I’ve done talks and have been a huge proponent for Stucco for a few years. I think I discovered it during one of Brandon Olin’s (aka devblackops) talk.
Some reasons I’m a huge fan
- Each function/cmdlet is it’s own file.
- Includes several high quality tests out of the box.
- It auto generates your docs via PlayPS.
- Ready for deploying to the CI tool of your choice.
- Include VSCode configs that make developing your code easy!
Before using Stucco I had hand crafted my modules but bespoke code is prone to trivial errors. This becomes even more of an issue when you work on the code as a larger team.
psake and PowerShellBuild #
One of the things that makes Stucco extremely powerful is it’s use of psake and PowerShellBuild. psake is a build system in PowerShell and PowerShellBuild is a set of pre-defined tasks for psake. Both of these are Brandon’s projects as well!
psake allows for easy extensibility and PowerShellBuild lays down a strong set of foundational tasks.
Your Entry Point #
After you create your first Stucco module you’ll be using the .\build.ps1
script for essentially all your work. The tasks are called with the -Task
parameter. So as you write your code and you want to test, you would just call
the same build script with the Test task and everything happens.
What’s everything? It cleans up previous builds, stages your files, builds your documentation, runs script analyzer, and tests your code. If you run the Publish task you’ll get all the same steps but then you’ll find your modules in the PSGallery! It’s that easy.
VSCode Tasks #
With the build script being the pivotal point for you to do your work, it makes sense that exposing those commands is critical. This was another huge feature for me. VSCode has a concept called tasks that you can call through the Command Palette or via Shortcuts. Stucco includes all the tasks that come from PowerShellBuild as VSCode tasks.
What’s that mean for you the developer? Write code. Hit shortcut run test. Rinse and repeat. It makes taking on Test Driven Development (TDD) very easy. See Run Your First Test to see an example.
Enough talk! Let do! #
At a high level we’ll be doing the following
- Install Stucco
- Create your GitHub repo and check it out
- Create your Stucco module
- Run your Test
- Commit your code
- Prep for PSGallery
- Publish your module
There are some tweaks for other repositories/CI systems but if you’re goal is to write public modules, I found this to be pretty easy.
Install Stucco #
Honestly that could be the whole thing. Under the hood Stucco depends on all the necessary modules so when you install Stucco it’ll pull down it’s requirements.
GitHub Repository #
If you don’t already have a GitHub account go ahead and sign up. I’ll skip on the details since there are several articles online to explain how and some best practices.
You’ll want to create a new repository for your module. So click on New.
Once you have your repository you’ll want to give a name and a description. Then you will check it out on your machine where you have Stucco installed.
Create Your Modules #
Now you can create you module in your repo folder. It’s as simple… New-StuccoModule
. Here’s an Example.
If you follow the prompts you’ll get the right files. Easy! Next you’ll want to check out the files and layout.
Some key ones are…
Build.ps1
: I mentioned this above. This is the work horse of the whole process.Requirements.ps1
: PSDepends (which the build file loads) uses this to setup your environment and setup any needed files/modules.psakeFile.ps1
: The psake configuration.
Run Your First Test #
You now have the “Hello World” version of the module. It can actually compile and give you a Hello World cmdlet. That should be good enough for testing.
Let’s run the Test task. You can find it under Terminal -> Run Task -> Test.
At this point you’ll see the build output. You can see how all the dependant tasks are executed before testing.
You can see all the steps and how long they took. Nice!
Commit Your Code #
Now let’s publish our code. I prefer to check in the initial version and add each feature in separate commits but that’s just me.
Setup PSGallery #
Once you have some versions checked into your repository on GitHub, you will eventually want to publish. First let’s create an API key on the PowerShell Gallery.
- Login to PSGallery
- Create API keys
- Copy them to your password vault.
With your key handy you’ll want to add it to GitHub.
- Go to your new repo and click the Settings pane
- You want to go to Secrets and then Actions
- Name it
PS_GALLERY_KEY
and paste
Publish Your Module! #
With you API key in your GitHub secrets you’re not ready to publish from GitHub. I would recommend creating a publish workflow on GitHub. You can use mine as an example: publish.yaml.
My workflow does a few convenient things
- Uses Chrissy LeMaire’s psmodulecache to speed up the builds
- Checks if the version is already published
- If it’s newer, it attempts to Publish with the Publish task
Wrap Up #
And that’s it! You now have a module that’s testable, open source, and automatically published to the PowerShell Gallery!
There are so many more cool details I could go over but I don’t want this to run longer then it already has. If there was anything unclear, please let me know by adding a comment or messaging me on the various linked social medias. I hope this was helpful and I would love to see what you publish!
References #
- My Stucco Presentation - Doc
- My Stucco Presentation - Slides
- Stucco on GithHb
- Plaster on GitHub
- PowerShell Module Cache GitHub Action
Cover Photo by Avel Chuklanov on Unsplash