Creating Helix Solutions and Modules with PowerShell
Helix is a set of overall design principles and conventions for Sitecore development, put forth by Sitecore in hopes of providing the community a path toward standardized solution development. As noted in the official documentation:
Helix provide a set of guidelines for your Sitecore projects. The Habitat example provides you with a pre-built and tested set of common modules that you can use as an inspiration to your project. Both improve the efficiency of your projects, reduce costs and time to market. As more and more people and organisations adopt the Helix conventions and principles, it will become a Sitecore standard. This means that people who are familiar with the conventions or the Habitat example will be able to work more easily on other convention-based projects with minimal training. It will be easier for Sitecore Product Support to understand projects built using the conventions, enabling them to resolve issues more quickly. Sitecore will test its software using the conventions so any compatible project that has been implemented for a customer will be more reliable. And since Sitecore will test its software using the conventions, Sitecore will be able to provide better guidance on how to update and upgrade existing Sitecore projects when new versions and new products are released.
(“Why be interested at all in Helix or Habitat?“, http://helix.sitecore.net/introduction/what-is-helix.html#why-be-interested-at-all-in-helix-or-habitat, April 16, 2018)
Some Basic Principles
Before we can dig into the script and creating Helix based solutions and modules, I want to reference some basic principles from the Helix documentation.
A Module is a conceptual grouping of assets which relates to a business requirement. For example, when the company asks that their Sitecore solution contains website search, all assets, business logic and configuration relating to search belongs to the Search module.
In technical terms, project often refers to Visual Studio project, but conceptually can also to the process of implementing the business requirements into an implementation.
The layer concept in Helix supports the architecture by making the dependency flow completely clear everywhere in the solution, in Sitecore, in Visual Studio and even in the file system. Furthermore, the layers provide a structure that is extremely suitable for creating and maintaining solutions of any size and steers both new and experienced developers to producing more maintenance-friendly and clean code. Layers helps control the direction of dependencies the importance of which is described by the Stable Dependencies Principle or SDP, which is one of the cornerstone principles in Modular Architecture:
File System and Solution Layout
In the Helix documentation, (and a review of the sample Habitat site) one will find a very specific recommended (if not expected) layout and naming of folders both in the Visual Studio Solution as well as the file system. The uniqueness of this ‘recommended’ layout makes the initial setup of a solution very time consuming and increasingly error prone when you start to deal with adding modules at the different layers. The following two images, taken from the Helix documentation show what the Visual Studio Solution and corresponding file system begin to look like.
Helpful but difficult
My team enjoys the flexibility as well as cleanliness of a solution built on the Helix principles. What we find most difficult is the extra leg work required in making the file system as well as the Visual Studio Solution appropriate to meet the principles. Trying to solve this problem has led us to try several solutions available in the Sitecore Community, many function very nicely, but didn’t always meet the needs of my teams. So I setout to solve some of these issues, including :
- No use of custom templates and files to ease setup across machines
- Option for serialization project creation
- Pre-loading basic NuGet packages for a module
- New projects added into the solution by default
Helix Project Creator PowerShell Module
Enough with the intro stuff and into the meat of what you’ve come looking for…a script to ease your Helix headaches and focus on the fun of implementing solutions. TheCodeAttic.Helix.ProjectCreator can be pulled from GitHub at https://github.com/gillissm/TheCodeAttic.Helix.PowerShellProjectCreator.
Setup and Use the Simple Manual Process
The first thing you need to do is retrieve the script from the GitHub repository. As this is hosted on GitHub there are numerous ways you can go about getting things ready to use. In an attempt to make adaption as easy as possible I’ve simplified setup to the following four steps for you:
- Open PowerShell command prompt as Admin.
- Change the directory to a working/temporary location
- Enter the following command
(Invoke-WebRequest -UseBasicParsing -Uri https://raw.githubusercontent.com/gillissm/TheCodeAttic.Helix.PowerShellProjectCreator/master/ProjectCreatorEasyInstall.ps1).Content | Out-File "ProjectCreatorEasyInstall.ps1"
- Then enter
- Now go implement some awesome Sitecore solutions!
Setup and Configure the Manual Way
As an alternate to the above, you can pull the script from the GitHub repository and load it into Package Manager Console each time you need it with the following steps:
- Download from the GitHub repository to your local system in the manner that suits your workflow the best.
- Each time you wish to leverage the module in Visual Studio you will need to enter the following in the Package Manager Console
Manual Process – Full Setup
OR you could download and configure the module manually (this is what ProjectCreatorEasyInstall.ps1 does for you.)
- Go to C:\Users\\Documents\WindowsPowerShell\Modules
- Create a new folder called “TheCodeAttic.Helix.PowerShellProjectCreator”
- Download the GitHub repository into the above directory
- Go up a level in the file system, should be at C:\Users\\Documents\WindowsPowerShell\
- Open (or create) NuGet_profile.ps1
- Add the following
- Each time you run Visual Studio the module will be available to use in the Package Console Manager
Using Helix Project Creation
Create a new Helix Solution
- Open Visual Studio as Admin
- Open the Package Manager Console
- Create a new Helix based solution by running Invoke-VisualStudioSolution
Invoke-VisualStudioSolution -SolutionPath 'C:\Code\Coffeehouse.Demo.SXA' -SolutionName 'Coffeehouse.Demo.SXA'
Add a new module to a solution
- Open Visual Studio as Admin
- Open your Solution
- Open the Package Manager Console
- ‘Wake-up’ the $dte object by running
- Add new module by running Invoke-NewModule
Invoke-NewModule -ModuleName 'Coffeehouse.Demo.SXA.Coupon' -Layer 'Feature' -UseGlass -UseTDS
- Go write some Sitecore code!!!!
Proof is in the pudding, they say
After running the Invoke-NewModule command your solution explorer should now look like the following:
And your file system will have the this:
The Sitecore Community members are always out to help each other, and some other Helix project creation solutions that may fit your work stream better are:
- Hands-on with Sitecore Helix: Using PowerShell to add a new module, Marc Duiker, https://blog.marcduiker.nl/2016/12/28/hands-on-with-sitecore-helix-using-powershell-add-module.html
- Sitecore Helix with TDS: create a new module with PowerShell, Una Verhoeven, https://unaverhoeven.ghost.io/2018/02/02/sitecore-helix-with-tds-create-a-new-module-with-powershell/
- Sitecore Helix Visual Studio Templates, Anders Laub, https://marketplace.visualstudio.com/items?itemName=AndersLaublaubplusco.SitecoreHelixVisualStudioTemplates
- Habi what-a-tat ? Create a Sitecore Helix solution from scratch using PowerShell, Akshay Sura, https://www.akshaysura.com/2017/01/21/habi-what-a-tat-create-a-sitecore-helix-solution-from-scratch-using-powershell/
- Yeoman Generator for Sitecore Habitat, Kam Figy, https://github.com/kamsar/generator-habitat