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
, 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:
The source command will set the default feed used to pull NuGet packages.
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.
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:
chew 'Moq' '4.0.10827'
chew 'Rx-Main' '1.0.10621'
chew 'NLog' '184.108.40.2060'
chew 'xunit' '220.127.116.119'
chew 'Pester' '1.0.1'
chew 'psake' '18.104.22.168'
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 22.214.171.1240'.
Successfully installed 'xunit 126.96.36.1999'.
Successfully installed 'Pester 1.0.1'.
Successfully installed 'psake 188.8.131.52'.
Successfully installed 'Giles 0.1.1.3'.
And my file system now has the packages installed:
PS C:\dev\PowerStudio> ls .\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.
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.