Paket


Using Paket from F# Interactive

This page demonstrates how to use Paket from the F# Interactive.

Download latest paket.exe (optional)

As first step we need to download and reference the latest paket tool. This boilerplate code allows F# scripts to work self-contained without an installed paket.exe. Alternativly you can just reference any paket.exe that you have on your system.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
open System
open System.IO
 
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
 
if not (File.Exists "paket.exe") then
    let url = "http://fsprojects.github.io/Paket/stable"
    use wc = new Net.WebClient() 
    let tmp = Path.GetTempFileName() 
    let stable = wc.DownloadString(url) 
    wc.DownloadFile(stable, tmp)
    File.Move(tmp,Path.GetFileName stable)

#r "paket.exe"

Configure which paket.dependencies file to use

Now we need open the Paket namespace and to tell Paket which paket.dependencies file to use.

1: 
2: 
3: 
4: 
5: 
open Paket

// locate the paket.dependencies file
let dependencies = Dependencies.Locate(__SOURCE_DIRECTORY__)
found: paket.dependencies

Adding and removing NuGet packages

Paket allows to install and uninstall NuGet packages programmatically:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
// install a package
dependencies.Add "FSharp.Data"

// check which version is installed
dependencies.GetInstalledVersion "FSharp.Data"
val it : string option = Some "2.1.0"

// uninstall a package
dependencies.Remove "FSharp.Data"

// check again which version is installed
dependencies.GetInstalledVersion "FSharp.Data"
val it : string option = None

Query the install model

It's possible to do queries against the installed NuGet packages:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
// install some packages
dependencies.Add "FSharp.Data"
dependencies.Add "FSharp.Formatting"
dependencies.Add "FsUnit"

// list all installed packages
dependencies.GetInstalledPackages()
val it : (string * string * string) list =
  [("Main", "FSharp.Compiler.Service", "0.0.67"); ("Main", "FSharp.Data", "2.1.0");
   ("Main", "FSharp.Formatting", "2.4.36"); ("Main", "FsUnit", "1.3.0.1");
   ("Main", "Microsoft.AspNet.Razor", "2.0.30506.0"); ("Main", "NUnit", "2.6.3");
   ("Main", "RazorEngine", "3.3.0"); ("Main", "Zlib.Portable", "1.10.0")]

// list only the direct dependencies
dependencies.GetDirectDependencies()
val it : (string * string * string) list =
  [("Main", "FSharp.Data", "2.1.0"); ("Main", "FSharp.Formatting", "2.4.36");
   ("Main", "FsUnit", "1.3.0.1")]

// list direct dependencies for the given package
dependencies.GetDirectDependenciesForPackage("Main", "FSharp.Compiler.Service")
val it : (string * string * string) list =
  ...

// list all usages of a package in Paket
let paketDependencies = Dependencies.Locate(System.IO.Path.Combine(__SOURCE_DIRECTORY__,".."))
found: D:\code\Paket\paket.dependencies
paketDependencies.FindReferencesFor "UnionArgParser"
val it : string list = ["D:\code\Paket\src\Paket\Paket.fsproj"]
Fork me on GitHub