Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget - Writing tools for Package Manager Console (custom cmdlets)

I've installed nuget package manager and I really love mvc-scaffold extension. I would like to write similar tool for my projects.

Is there any API reference or some documentation for nuget I can learn from ? TIA for any suggestions.

Edit: Question is already 'answered' (thx one more time), here are some links that can be helpful:

  • http://nuget.codeplex.com/releases/view/59864 - package explorer (download + see what it's inside the package - thank God for comments in mvc scaffolding ps scripts :))
  • http://channel9.msdn.com/Series/mvcConf/mvcConf-2-Steve-Sanderson-MvcScaffolding - video from mvcConf2
  • and of course Steve Sanderson's blog : http://blog.stevensanderson.com/
like image 266
Jarek Avatar asked Feb 22 '11 11:02

Jarek


People also ask

What is the NuGet package manager console?

The NuGet Package Manager Console lets you use NuGet PowerShell commands to find, install, uninstall, and update NuGet packages. Using the console is necessary in cases where the Package Manager UI does not provide a way to perform an operation. To use nuget.exe CLI commands in the console, see Using the nuget.exe CLI in the console.

How do I use Nuget in Visual Studio?

You can use NuGet PowerShell commands to find, install, uninstall, and update NuGet packages. To open the console in Visual Studio, go to the main menu and select Tools > NuGet Package Manager > Package Manager Console command.

How do I manage package sources in NuGet?

To manage package sources, select the gear icon. This is a shortcut to the Tools > Options > NuGet Package Manager > Package Sources dialog box as described on the Package Manager UI page. Also, the control to the right of the project selector clears the console's contents:

How do I use the Package Manager Console in Visual Studio?

The Package Manager Console is available within Visual Studio by going to Tools » Nuget Package Manager. If you want to use the Package Manager Console to execute migrations command, you need to ensure that the latest version of Microsoft.EntityFrameworkCore.Tools is added to your project.json file.


1 Answers

I wrote the current version of MvcScaffolding that you mention in your question. Here's how it adds PowerShell cmdlets to the Package Manager Console:

  • Cmdlets are written in C# and compiled into a .NET assembly (see http://msdn.microsoft.com/en-us/magazine/cc163293.aspx)
  • The .NET assembly is included in MvcScaffolding's "tools" folder (see http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#From_a_convention_based_working_directory)
  • MvcScaffolding also contains an init.ps1 file that NuGet runs each time you open a solution containing it (also described at http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Automatically_Running_PowerShell_Scripts_During_Package_Installation_and_Removal). This script uses the PowerShell "Import-Module" command to import the cmdlets from the .NET assembly, making them available in the console.

Note that it's not actually necessary to write your cmdlets in C# and call Import-Module. A simpler alternative is to write them in PowerShell (see http://technet.microsoft.com/en-us/magazine/ff677563.aspx) and define them inline in your NuGet package's init.ps1 file.

Or, if your question is about how to add custom scaffolders to MvcScaffolding (e.g., so you can say "Scaffold MyCustomThing -somecustomparams"), then use the command "Scaffold CustomScaffolder MyCustomThing", and then edit the PS1/T4 files that appear in your CodeTemplates/Scaffolders folder. I'll blog more details about this soon.

like image 60
Steven Sanderson Avatar answered Sep 21 '22 23:09

Steven Sanderson