Getting started with Paket

For a while now I’ve been meaning to have a play with Paket and I’ve finally found some time to try it out.

What is Paket?

Paket is a package management tool along the lines of NuGet. In many ways you think of it as NuGet plus some extra goodies.

Like NuGet, you can download/update/install NuGet packages, but on top of that you have the ability to download/update/install files from GitHub or from the Gist repositories or single files from an HTTP resource. This is so useful, especially when you have files that maybe you don’t want to create assemblies around to reuse, but instead just want to include the files in your project in their minimal state, i.e. just as source code (or any other file type you like).

Jumping straight in

Okay, enough chat, let’s get started.

I’m going to create a solution in Visual Studio named PaketTest, mine’s a console application but it really doesn’t matter as we’re just going to walk through the quickest and simplest way to get up and running with Paket. We’re using this solution solely as a place holder so we can see our dependencies referenced and files loaded – to get a feel for how things work and look.

When the solution’s loaded we’re going to use NuGet to grab the paket.exe. The Paket website’s tutorial mentioned getting Paket EXE and/or the bootstrapper from GitHub, but in some scenarios you might be locked down by your companies download policy to not be able to download EXE’s. So either grab the bootstrapper and use that to download paket.exe or use NuGet to get it.

So follow these steps and we’ll be up and running in now time

  • From Package Manage Console in your Visual Studio solution, run Install-Package Paket
  • From a command prompt, navigate to the root folder of your solution
  • Create the directory .paket
  • You can either copy paket.exe to this location or run it from the packages folder, I’ve copied mine here as it’s an easier path to type each time
  • In the solution root folder create the file paket.dependencies. Next, add the following to that file
    source https://nuget.org/api/v2
    
    nuget Ninject
    
    github putridparrot/MusicTheory Theory.xml
    
    gist putridparrot/b3968f23bdabc86a6777 MemoryInformation.cs
    

    Obviously feel free to replace NInject and the putridparrot GitHub and Gist repositories/files with your own, but as I want to demo these three options, I know that there are files in those locations that we can download.

    The dependencies file is used to get the dependencies from the various locations but does not automatically add these to your solution, that’s covered in the next step.

  • Now we need to create another new file (again in the root folder of the solution) named paket.references. In here place the following code
    NInject
    
    File:Theory.xml
    File:MemoryInformation.cs
    

    This file is what Paket uses to actually add references and files to your solution. As you can see we’re basically saying we want to add the references NInject to our solution and the files listed (with the File: prefix) will be added to a paket-files solution folder. If you’d prefer the files are placed in the root folder or your solution, follow the file name with a . for example

    NInject
    
    File:Theory.xml .
    File:MemoryInformation.cs .
    

    Note: We can also specify a folder in our solution for any files, simply replace the . in the above examples with a folder name, for example

    NInject
    
    File:Theory.xml Data
    File:MemoryInformation.cs Source
    
  • Now we’ve got the configuration in place, run (from the command line in the root of the solution) .paket\paket.exe install.

    This will run the Paket application, which will read the .dependencies file and download the NuGet package NInject and the file Theory.xml from GitHub and the Gist file MemoryInformation.cs. It will then read the .references file (if one exists) to add the references for NInject and will add the two files Theory.xml and MemoryInformation.cs. Depending upon whether you used the . or not, the files will either be in the root of the solution or the paket-files solution folder or a folder you’ve specified.

What’s this paket.lock file

When you run the paket.exe install command you’ll notice a new file added to the root folder, named paket.lock. Mine looks like this

NUGET
  remote: https://nuget.org/api/v2
  specs:
    Ninject (3.2.2)
GITHUB
  remote: putridparrot/MusicTheory
  specs:
    Theory.xml (3383d5e239cf64fa8e65ada252fe08ae5764e36c)
GIST
  remote: putridparrot/b3968f23bdabc86a6777
  specs:
    MemoryInformation.cs (4368713d0b2c0c969be01e9309db818f67a65d53)

To quote “The paket.lock file”, this file “records the concrete dependency resolution of all direct and transitive dependencies of your project”. This file should be checked into your source repos. (along with the other two files) for other developers to use as it will ensure that the same versions of packages are loaded. i.e. if NInject (for example moves forward to version 4.0 this file ensures we’re all still on the same version).

Not covered in the above steps (mainly as I haven’t had need to use it so far), but you can also add a file via an HTTP resource to the dependencies using

http http://someurl/Myfile.cs

and then reference the file in the normal way in the references file, i.e.

File:MyFile.cs

Core paket commands

I’m going to cover some core paket commands here

paket.exe help

As you’d expect this will list all the currently available commands for paket and can be invoked using help or ? or a few other ways which you can find yourself by typing paket help.

paket.exe install

We’ve already covered this, but to document this, the install command is used to download the dependencies specified within the paket.dependencies file or the paket.lock file (remember if the paket.lock file is not supplied it will be generated via the install command).

paket.exe outdated

What if a dependency has a new version available ? We can run the outdated command to ask paket to report on whether any packages have been updated, i.e. are there any newer versions. This command does not actually update anything, it’s there to tell us if we might need to update.

packet.exe update

Unlike outdated, the update command will not only check for newer versions of packages but will download them and update any that are declared in the paket.references file.

paket.exe convert-from-nuget

If you’re working on a solution that is already using NuGet and you want to convert to paket you can use the convert-from-nuget command. See paket convert-from-nuget for a full list of steps/information on this command.

paket.exe restore

Previously I mentioned checking the paket.lock file into the source repos. Now if I’ve just joined a project and I need to restore the packages, I can run the restore command which works in a similar way to NuGet restore in that it will download the relevant packages and files based upon the information in the lock file and set my solution up accordingly. This command will use the .lock file to ensure it gets the same versions so I would now be in sync with those who checked this file in.

References

Paket on GitHub
The Paket website