Sitecore 10.1 New Feature - Extend the Command Line Interface with NuGet (Step-by-Step)

Recently tried this new Sitecore 10.1 feature that extends nuget configuration with Sitecore packages and installs plugins from CLI. Faced a few roadblocks but got some precise tips from Nick Wesselman. So, decided to do a step-by-step tutorial since this seems a vital extension!

1. Open PowerShell in admin mode within the directory where you want to install CLI 3.0.0. Ideally this should be your Sitecore project directory. Since this is just a test, I created a directory named clitest under c: and decided to install the CLI extension to see what happens thereafter.

2. Within the directory, first of all, I decided to test if any sitecore.cli commands are recognized:


3. I also tried dotnet sitecore -


4. The reason is - our new folder is empty at this point:


5. Now, let's execute the first command - dotnet new tool-manifest  You should get a success message:


6. This is how the folder now looks:


There is only a .config folder added and it has dotnet-tools.json file. 

7. Next, if you execute any of the sitecore commands like, sitecore login, you will get a message as follows:

Couldn't resolve a root configuration file (sitecore.json) in the current or any parent directory. Looks like the command may have been executed outside a Sitecore project?

8. In order to create the sitecore.json, execute the sitecore init command and you should see the following changes to your folder , in my case, clitest:


9. Note that if you execute dotnet, the cmd line recognizes the same but not dotnet sitecore -



10. So, next step is to tell the cmd line to start recognizing Sitecore.cli commands. So, let's issue the following command:

dotnet tool install Sitecore.CLI --add-source https://nuget.sitecore.com/resources/v3/index.json


The fact that the PS command line is environment-friendly by emitting green message means its happy and everyone is happy since nobody likes a cmd line vomiting blood.... hehe... huh... my sense of humour...  


11. Back on track, the proof that CLI is installed successfully can be further cross-checked through dotnet-tools.json within the .config folder:


12. Now, when you execute dotnet sitecore --help in the PS command line, you get some respect:


If you check the commands list above, you should now find the gem we have been after - the plugin command!

13. Also, execute dotnet Sitecore --version and you should see the version info:


Note that if you just execute sitecore --version  you will get a different version info. So, always prefix with dotnet command.



14. But, we are not there yet! Our purpose is to extend nuget and use Sitecore specific packages. So, we need to link these two together.

15. To understand what I mean, if you execute a Sitecore plugin command now - dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.Serialization, you will get the following message - 

Unable to install NuGet package 'Sitecore.DevEx.Extensibility.Serialization'. Ensure package names and versions are valid and that NuGet is installed.
 Sources:https://api.nuget.org/v3/index.json
file:///C:/Program Files (x86)/Microsoft SDKs/NuGetPackages/



16. So, the next step is to update nuget.config with the Sitecore package url. The following command does the same:

dotnet nuget add source https://nuget.sitecore.com/resources/v3/index.json -n sc-packages


Once the command is executed successfully, you can check the nuget.config for the updated packagesources list. Since this folder doesn't have any nuget.config, the above command goes and updates the central nuget.config. In my case, its under - 

C:\Users\conta\AppData\Roaming\NuGet


The above screen shot holds the Sitecore nuget source url as a proof.

17. Now, back in the PS Command prompt, re-execute the dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.Serialization and, no surprises this time -



18. Next, execute dotnet sitecore plugin list and you should find the list of all plugins installed in this folder - 


19. At this point, you should also see a package-cache sub-folder under .sitecore folder that has the new Sitecore package -



20. Just in case if you want to remove the package, you can execute the following command - 

dotnet sitecore plugin remove -n Sitecore.DevEx.Extensibility.Serialization

Note that the above command doesn't delete the contents from the package-cache sub-folder but, if you execute the dotnet sitecore plugin list, the plugin is not listed anymore -



Also, in case if you need to install a new Sitecore package, head to https://sitecore.myget.org/gallery: 


Filter by specific category. For instance, sc-packages in our case and there you have the list of packages with package id used for the name parameter while executing dotnet sitecore plugin add -n <package id>


At this point, I understand that this extension is a valuable addition but, I want to put it to use through a use-case wherein if I build  a Sitecore package, upload to myget, extend through nuget with CLI, I think that would be bliss!

Reference: https://doc.sitecore.com/developers/101/developer-tools/en/extend-the-command-line-interface-with-nuget.html

Other Errors:



Error Details:

Unhandled exception: System.Exception: Unhandled exception
 ---> NuGet.Protocol.Core.Types.FatalProtocolException: An error occurred while retrieving package metadata for 'Sitecore.DevEx.Extensibility.Serialization.3.0.0' from source 'sc-packages'.
 ---> NuGet.Protocol.Core.Types.FatalProtocolException: Failed to fetch results from V2 feed at 'https://sitecore.myget.org/gallery/sc-packages/FindPackagesById()?id='Sitecore.DevEx.Extensibility.Serialization'&semVerLevel=2.0.0' with following message : Response status code does not indicate success: 404 (Not Found).
 ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at NuGet.Protocol.HttpSource.<>c__DisplayClass14_0`1.<<GetAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token)
   at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token)
   at NuGet.Protocol.HttpSource.GetAsync[T](HttpSourceCachedRequest request, Func`2 processAsync, ILogger log, CancellationToken token)
   at NuGet.Protocol.V2FeedParser.LoadXmlAsync(String uri, String cacheKey, Boolean ignoreNotFounds, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   --- End of inner exception stack trace ---
   at NuGet.Protocol.V2FeedParser.LoadXmlAsync(String uri, String cacheKey, Boolean ignoreNotFounds, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   at NuGet.Protocol.V2FeedParser.QueryV2FeedAsync(String relativeUri, String id, Int32 max, Boolean ignoreNotFounds, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   at NuGet.Protocol.V2FeedParser.FindPackagesByIdAsync(String id, Boolean includeUnlisted, Boolean includePrerelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   at NuGet.Protocol.V2FeedParser.GetPackage(PackageIdentity package, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   at NuGet.Protocol.DependencyInfoResourceV2Feed.ResolvePackage(PackageIdentity package, NuGetFramework projectFramework, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   --- End of inner exception stack trace ---
   at NuGet.Protocol.DependencyInfoResourceV2Feed.ResolvePackage(PackageIdentity package, NuGetFramework projectFramework, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
   at Sitecore.DevEx.Configuration.NugetApiClient.NugetApiResolver.InstallPackage(String packageId, String version, String nuGetPackageDirectory) in C:\BA\ca7111d945a16af4\src\Sitecore.DevEx.Configuration\NugetApiClient\NugetApiResolver.cs:line 49
   at Sitecore.DevEx.Configuration.ExternalPackageResolver.InstallNuGetPackages(IReadOnlyList`1 nuGetPackageSpecs, String nuGetPackageDirectory) in C:\BA\ca7111d945a16af4\src\Sitecore.DevEx.Configuration\ExternalPackageResolver.cs:line 93
   at Sitecore.Devex.Client.Cli.Extensibility.Tasks.AddPluginTask.Execute(AddPluginTaskOptions options) in C:\BA\ca7111d945a16af4\src\Sitecore.Devex.Client.Cli.Extensibility\Tasks\AddPluginTask.cs:line 45
   at Sitecore.DevEx.Client.Cli.Subcommands.Plugin.AddPluginCommand.Handle(AddPluginTask task, AddPluginArgs args) in C:\BA\ca7111d945a16af4\src\Sitecore.DevEx.Client.Cli\Subcommands\Plugin\AddPluginCommand.cs:line 28
   at Sitecore.Devex.Client.Cli.Extensibility.Subcommands.SubcommandBase`2.HandleInternal(TArgs args) in C:\BA\ca7111d945a16af4\src\Sitecore.Devex.Client.Cli.Extensibility\Subcommands\SubcommandBase.cs:line 75
   --- End of inner exception stack trace ---
   at Sitecore.Devex.Client.Cli.Extensibility.Subcommands.SubcommandBase`2.HandleExceptionMessageConsoleLogging(Exception exception, ILogger logger) in C:\BA\ca7111d945a16af4\src\Sitecore.Devex.Client.Cli.Extensibility\Subcommands\SubcommandBase.cs:line 118
   at Sitecore.Devex.Client.Cli.Extensibility.Subcommands.SubcommandBase`2.HandleInternal(TArgs args) in C:\BA\ca7111d945a16af4\src\Sitecore.Devex.Client.Cli.Extensibility\Subcommands\SubcommandBase.cs:line 81
   at System.CommandLine.Invocation.CommandHandler.GetResultCodeAsync(Object value, InvocationContext context)
   at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseErrorReporting>b__20_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass24_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__21_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseDirective>b__19_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseDebugDirective>b__11_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass13_0.<<UseExceptionHandler>b__0>d.MoveNext()

Cause or resolution:

Provided a wrong Sitecore url, in this case, I provided https://sitecore.myget.org/gallery/sc-packages  instead of https://sitecore.myget.org/F/sc-packages/api/v3/index.json in the command dotnet tool install Sitecore.CLI --add-source https://sitecore.myget.org/F/sc-packages/api/v3/index.json

Error:


The term sitecore.cli is not recognized as the name of  a cmdlet, function, script file, or operable program.

Resolution:

As the error says, doesn't recognize the cli command so, do the needful  as in step 10.

Comments

Popular Posts