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"]
namespace System
namespace System.IO
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
property Environment.CurrentDirectory: string
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.Exists(path: string) : bool
val url : string
val wc : Net.WebClient
namespace System.Net
Multiple items
type WebClient =
  inherit Component
  new : unit -> WebClient
  member BaseAddress : string with get, set
  member CachePolicy : RequestCachePolicy with get, set
  member CancelAsync : unit -> unit
  member Credentials : ICredentials with get, set
  member DownloadData : address:string -> byte[] + 1 overload
  member DownloadDataAsync : address:Uri -> unit + 1 overload
  member DownloadFile : address:string * fileName:string -> unit + 1 overload
  member DownloadFileAsync : address:Uri * fileName:string -> unit + 1 overload
  member DownloadString : address:string -> string + 1 overload
  ...

Full name: System.Net.WebClient

--------------------
Net.WebClient() : unit
val tmp : string
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
Path.GetTempFileName() : string
val stable : string
Net.WebClient.DownloadString(address: Uri) : string
Net.WebClient.DownloadString(address: string) : string
Net.WebClient.DownloadFile(address: Uri, fileName: string) : unit
Net.WebClient.DownloadFile(address: string, fileName: string) : unit
File.Move(sourceFileName: string, destFileName: string) : unit
Path.GetFileName(path: string) : string
val dependencies : obj

Full name: Reference-from-repl.dependencies
val paketDependencies : obj

Full name: Reference-from-repl.paketDependencies
Path.Combine([<ParamArray>] paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
Fork me on GitHub