Open Source.NET

Clean Up NuGet Dependencies With Chewie

Chewie is a domain-specific language that uses PowerShell to tame NuGet installations.

Many developers are tired of dealing with XML. If you're one of them, and don't like the XML configuration files NuGet scatters around during package installation, Chewie might be the tool for you. Created by EricRidgeway and BobbyJohnson, Chewie is a set of PowerShell scripts which define a small DomainSpecificLanguage (DSL) designed to bundle your NuGet dependencies. It gathers your NuGet packages into a single bundle that you can easily manage.

The easiest way to get Chewie is to use its NuGet package. If you install Chewie through Visual Studio, you'll have access to a couple of new commands in the Package Manager Console (PMC).

If you're not in Visual Studio and want to use Chewie, you must make sure that NuGet is in your path. This is needed because Chewie has to use NuGet or the install-package command in the PMC.

If you want to have Chewie available to all your projects through PowerShell, copy the files to a central location and import chewie.psm1 into your PowerShell profile using the Import-Module command. You can edit your profile by entering notepad $profile into the PowerShell command-line.

If you don't want to add Chewie to your profile, just manually import the chewie.psm1 module into your PowerShell session.

Once Chewie's installed, you'll have two new PowerShell commands: Initialize-Chewie and Invoke-Chewie. The first will create a file named .NugetFile and put in some boilerplate configuration. The second will parse the .NugetFile and execute the commands therein. Now that you have Chewie and a simple .NugetFile, there are three basic commands in the grammar to know:

  • source
    The source command will set the default feed used to pull NuGet packages.
  • install_to
    By default, NuGet installs packages to the .\packages\<Package>.<Version> folder. The install_to command is used to specify the root folder for your packages. So if you specified install_to ‘lib', your packages will go to .\lib\<Package> when installed.
  • chew
    The chew command installs the package and allows for an optional version and source parameter. The source parameter overrides the source command so that you can specify an alternate feed for that particular package.

Chewie will keep installed package layout a little cleaner than the default NuGet layout by allowing you to specify which directory to use, and it doesn't use the version number in the folder names.

Taking a real example of a .NugetFile:

  install_to  'Packages'
chew 'Moq' '4.0.10827'
chew 'Rx-Main' '1.0.10621'
chew 'NLog' '2.0.0.2000'
chew 'xunit' '1.8.0.1549'
chew 'Pester' '1.0.1'
chew 'psake' '4.0.1.0'
chew 'Giles' '0.1.1.3'

Then, in the PowerShell Prompt, I can run Chewie:

  PS  C:\dev\PowerStudio> Invoke-Chewie
Successfully installed 'Moq 4.0.10827'.
Successfully installed 'Rx-Main 1.0.10621'.
Successfully installed 'NLog 2.0.0.2000'.
Successfully installed 'xunit 1.8.0.1549'.
Successfully installed 'Pester 1.0.1'.
Successfully installed 'psake 4.0.1.0'.
Successfully installed 'Giles 0.1.1.3'.

And my file system now has the packages installed:

PS C:\dev\PowerStudio>  ls .\Packages

     

Directory: C:\dev\PowerStudio\Packages

Mode              LastWriteTime     Length Name
----              -------------     ------ ----
d----       12/11/2011   9:38 AM          Giles
d----       12/11/2011   9:37 AM          Moq
d----       12/11/2011   9:38 AM          NLog
d----       12/11/2011   9:38 AM          Pester
d----       12/11/2011   9:38 AM          psake
d----       12/11/2011   9:37 AM          Rx-Main
d----       12/11/2011   9:38 AM          xunit

Chewie is a creative example of using PowerShell to create an elegant DSL. I highly recommend you look through the scripts to understand how Chewie processes the .NugetFile. It will be well worth your time.

About the Author

Ian Davis is the Master Code Ninja for software architecture and development consulting firm IntelliTechture. A C# MVP, Davis is an expert on the .NET Tramework and co-organizer of the Spokane .NET User Group who frequently speaks at industry events. He spends most of his free time as an open source author and advocate, publishing and working on many open source projects.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube