Introducing Mold - The Missing PowerShell Templating Engine

Mold is PowerShell template & scaffolding engine that lets you effortlessly craft templates for any language (not limited to PowerShell), on any platform. 🚀 Deploy in a flash with interactive prompts or answer files – your choice!

Ready to see Mold work in action? Here’s a sneak peek of what deploying a template looks like:

image-20240712174346768

🌱 What’s Templating & Why Should You Care?

Think of templates as blueprints for your code. They’re pre-formatted files with placeholders for variables, like names, dates, blocks of code. When you use a template, you simply fill in the blanks, and voilà – you’ve got a customized script or project ready to go!

Why is this awesome?

  • Save Time: No more writing the same boilerplate code over and over.
  • Consistency: Templates help ensure your projects follow best practices and maintain a uniform style.
  • Scalability: Easily create multiple projects or files from a single template.
  • Flexibility: Customize your templates to fit your specific needs.

Some Practical Use Cases

  • New modules with consistent boilerplate code.
  • Quickly generate complex configuration files from pre-defined templates.
  • Build project scaffolding with a few simple commands.
  • Standardize documentation across your PowerShell projects.
  • Easy template for Minutes of meeting and personal notes.

Mold is a template engine, a powerful tool that acts as a processor for your templates and data. This Wikipedia article explains the concept of templating in depth.

In short, templating is a superpower for developers, boosting productivity and reducing errors. And with Mold, harnessing that power is easier than ever before!

🚀 Can’t Wait to Get Started?

If you’re itching to give Mold a whirl, you can grab the module from the PowerShell Gallery and explore the source code on GitHub right now!

But wait, there’s more! If you’re ready to dive deeper, stick around and join me as I walk you through the step-by-step process of building a Mold template from the ground up. We’ll cover all the key features and even deploy the template to see it in action. Let’s get started!

The Why?

Mold began as my own little side project to scratch a personal templating itch. But, it quickly blossomed into something bigger – a solution to a longstanding gap in the PowerShell ecosystem. Now, I know what you’re thinking: “We’ve got Plaster, right?” And yes, while Plaster has served the community well, I believe there’s room for a fresh approach. Let’s dive into why I think a new templating engine is not only welcome but necessary. 😉

  • While Plaster’s deep ties to PowerShell offer undeniable advantages, they also come with certain constraints. I aimed to create something more versatile – a tool that treats text as its playground, regardless of language.

  • Let’s face it, Plaster’s PowerShell 7 support is… well, underwhelming. 😞 It’s been gathering dust for years and demands those long, convoluted XML files that nobody enjoys writing.

The How?

🤔 Still Here? Alright, I see you’re intrigued. Perhaps you’re even starting to agree that a fresh templating engine is not that bad idea after all. Let’s unveil what Mold brings to the table, shall we?

You can find a deep dive into Mold’s concepts, installation, usage, and even some pro tips on our GitHub repository. I encourage you to even scour through the actual code to get a feel for how Mold works under the hood.

This blog post will focus on a hands-on walkthrough of crafting and deploying your very own Mold templates. So, buckle up and get ready to mold some code!

Example Project - PowerShell Script Template for new Scripts

I spin up new PowerShell scripts all the time, whether just for quickly experimenting and testing things out, or for projects that I know will be around for a while. There’s a few basic things that I like all my scripts to have. - Daniel Schroeder

Lets tackle this challenge. I wanted a bare-bones script template that delivers the essentials:

  • script.ps1: A file with a name that perfectly matches its purpose, along with a basic script structure.
  • Docs : A dedicated folder to hold extra files related to script.
  • README.md: A markdown file within the Docs folder for documenting the script’s purpose/instruction.

NOTE: This example is intentionally simple (although less practical), designed to illustrate Mold’s core concepts and functionality. In real-world scenarios, your templates can be far more complex and tailored to your specific needs.

Let your imagination run wild! 🌈 Think about the repetitive tasks in your PowerShell projects. Could you automate them with a template? The possibilities are endless!

Step 1: Prep the Template Directory

First things first, let’s get Mold up and running on your system. It’s a quick and easy installation from the PowerShell Gallery:

Install-Module -Name Mold

Now, let’s set the stage for our PowerShell script template. We’ll call our directory PowerScript, all the code in their is boilerplate code that I added manually on my own.

PowerScript
├── Docs
│   └── README.md
└── script.ps1

Here’s the content of script.ps1:

image-20240710163822217

the Readme.md file and its content

image-20240710163949940

Step 2 : Add Placeholders

Time to make our template dynamic! Let’s update script.ps1 and README.md with Mold’s placeholder magic

image-20240710164811003

And this README.md as below

image-20240710165024811

Step 3 : Generate MoldManifest and Update fields

Now that we’ve added placeholders, let’s create the MoldManifest.json file, Just run the following command, either from within your PowerScript directory or by specifying its full path:

# Generate Mold Manifest
New-MoldManifest -Path ./PowerScript

After running the command, you’ll find the following MoldManifest.json file in your PowerScript directory:

This file is the backbone of your template. It defines the questions Mold will ask when the template is invoked. You can customize this file further to tailor your template to your exact needs.

image-20240712002242817

Remember, you can personalize the “Message,” “Prompt,” “Caption,” and “Choices/Description” fields to provide even clearer context for your questions. Make this template truly yours!

Step 3.5 : Supercharge Your Template with MOLD_SCRIPT (Optional, but Oh So Powerful)

What if you want your template to do more than just swap out text? Maybe you need to rename files, tweak content based on user input, or perform some other scripting wizardry. That’s where MOLD_SCRIPT.ps1 steps in! This special file is Mold’s secret weapon for executing custom logic at the end of the template deployment process.

In our example, we’ll use MOLD_SCRIPT.ps1 to rename the generic script.ps1 file to something more meaningful, based on the input we gather from the user.

image-20240712004742971

All user-provided data is consolidated within the $MoldData variable, accessible for use within your custom logic. To maintain system integrity, scripts operate in an isolated environment, able to modify only template content and not the file/content already present in final output directory.

Step 4 : Ready to roll

You’ve just built a reusable template that can effortlessly crank out new PowerShell scripts. Now, let’s explore the different ways you can put this template to work.

  1. Manually point to path

​ If you just need one-off template run or running someone else’s template, you can always manually point to the template directory. This method gives you full control over where Mold looks for your template. Just remember, you’ll need to specify the full path each time.

# Full path to template directory
Invoke-Mold -TemplatePath "/Some/Path/to/Template/PowerScript"
# If you donot provide destination path, template will be deployed to current working directory
  1. Add to MOLDTEMPLATES environment path

    My favorite way to store templates is in the MOLDTEMPLATES environment variable. It’s like a VIP lounge for your templates, offering perks like:

    • Tab Completion: Never fumble for file paths again, you can invoke templates by name
    • Multiple Paths: Add as many template directories as you want, just separate them with semicolons.
    # invoke by name
    Invoke-Mold -Name PowerScript
    

    This trick is especially handy if you’re into Git version control for your templates. It’s the perfect way to keep your templates organized, easily accessible and isolated from PowerShell modules/code.

The Moment of Truth: Invoking Your Template

You’re all set! Now, let’s see the fruits of your labor. Here’s what happens when you run the Invoke-Mold command:

As you can see, Mold seamlessly guides you through the template’s questions, gathers your input, and then generates a fully customized script along with its accompanying documentation. Effortless and efficient! ✨

image-20240712005417595

Answer File

Not a fan of answering prompts? Or perhaps you’re looking to automate template creation within your scripts. No problem! Mold’s got you covered with answer files.

You can easily generate a template for your answer file using the New-MoldAnswerFile command, providing either the template path or its name.

Here’s a simple example of an answer file for our script template, the only required elements here is Key and Answer all other fields are there to provide you better context.

image-20240712010558465

With this file in hand, you can feed it to the Invoke-Mold command and watch it execute without any manual intervention. It’s the perfect way to streamline your templating workflow and achieve hands-off automation!

# invoke with AnswerFile
Invoke-Mold -TemplatePath .\PowerScript -AnswerFile .\Mold_Answer_File.json

And the Results Are In!

Here’s a sneak peek at the final output after running the command:

As you can see, Mold not only replaced placeholders with your answers but also worked its magic behind the scenes. It renamed script.ps1 to SystemUptime.ps1, injected a helpful script header, and even updated the filename within the help content. Now that’s what I call a smart template!

image-20240712005854047

Even Markdown file was updated in Docs folder

image-20240712011014106

📢 Mold Community Call-Out!

Mold is an open-source project, and I’m always eager for your input! Whether you have a cool template idea, a feature request, or you’ve encountered a bug, I’d love to hear from you.

Swing by the Mold GitHub repository and let’s make Mold even more awesome together! Submit your ideas, report issues, or even contribute code. Let’s build a vibrant community of Mold enthusiasts! 💪

🙌 Conclusion

With Mold, you’re not just creating templates; you’re crafting a toolbox of productivity boosters. 🚀

If you encounter any issues or have suggestions, don’t hesitate to open an issue. Your voice matters, Let’s shape the future of PowerShell templating!

Thank you so much, happy scripting!