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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube