Sitecore CLI: Extend the Command Line Interface with NuGet
Sitecore Command Line Interface (CLI) allows console communication with a Sitecore instance. Sitecore CLI allows adding commands by writing custom plugins. Sitecore provides some plugins like Publishing and Serialization.
The purpose of this blog is to create a simple Sitecore CLI plugin which will print any message which you pass as parameter or print a default message.
Sitecore CLI Document
Prerequisites
Code Repository
Usages
dotnet sitecore plugin [subcommand] [options]
Below are some subcommands:
Create a custom CLI plugin command
- Create a blank new Solution “CustomCLI” in Visual Studio, then add a class library with .Net core 3.1 with the name “CLI.DevEX.Extensibility.Custom”.
- PackageSources node contains all package sources. Plugin sources need to be specified in Nuget.Config, Local package source will take the package from “Build/nugets” folder which we have generated in the previous step.
- Add NuGet references to Sitecore.Devex.Client.Cli.Extensibility and Spectre.Console in the class library.
- Create a class named RegisterExtension and inherit it from ISitecoreCliExtension class. AddCommands method allows the plugin to add a command to the RootCommand of the System.CommandLine processor. AddConfiguration method will allow the plug-in to add configuration to a config builder. AddServices method will allow the plug-in to service to the IOC container.
- Create a subfolder named Commands and add a class CustomCommand inside the Commands folder which will inherit Command and ISubcommand class.
- Now create a class named PrintCommand inside the Commands folder. SubcommandBase class will handle exceptions for Sitecore CLI subcommands.
- Add a class named PrintArgOptions inside the Tasks folder.
- Add a new subfolder named Tasks and create a class PrintTask inside this folder.
- Create PrintTaskOptions which inherit TaskOptionsBase. This class will validate the utility of the task option class.
- Open CLI.DevEX.Extensibility.Custom.csproj file and update the below settings:
PackageOutputPath
Determines the output path in which the packed package will be dropped. Default is $(OutputPath). To make Sitecore CLI able to find our Nuget without publishing it to the public feed we instead use a local Nuget folder. “$(SolutionDir)_Build/nugets”. In the csproj file we can set the ‘PackageOutputPath’ property to output our package to this folder.
CopyLocalLockFileAssemblies
The CopyLocalLockFileAssemblies property is useful for plugin projects that have dependencies on other libraries. If you set this property to true, any NuGet package dependencies are copied to the output directory. That means you can use the output of dotnet build to run your plugin on any machine.
AppendTargetFrameworkToOutputPath
Setting AppendTargetFrameworkToOutputPath to false prevents the target framework moniker from being appended to the output path.
PackageReference
Package references, using the PackageReference node, manage NuGet dependencies directly within project files.
TargetsForTfmSpecificContentInPackage
TargetsForTfmSpecificContentInPackage write a custom target. PackagePath where the file should be output in the package.
Plugin assembly needs to be placed inside a plugin folder so that’s why PackagePath is mentioned as a plugin.
How to Run Custom CLI Plugin
- Open terminal, Run as Administrator.
- Run the below command to restore:
- dotnet tool restore
- Run below command to add your custom plugin:
- Run “dotnet sitecore -h” command to check available commands:
- To check the custom command run “dotnet sitecore Custom -h”.
- Now finally when you will run “dotnet sitecore Custom Print” command, it will print the default name.
- You can pass some message like this “dotnet sitecore Custom print -m Sitecore”.
Note: Extensions/customizations to the CLI and Sitecore Management Services module are unsupported by Sitecore.
Comments