NuGet package restore
INFO
This documentation is for FAKE.exe before version 5 (or the non-netcore version). The documentation needs te be updated, please help!
If you are using a source control system like git you probably don't want to store all binary dependencies in it. With FAKE you can use NuGet to download all dependent packages during the build.
Setting the stage for NuGet
In order to download the packages during the build we need to add NuGet.exe to our repository. You can download the "NuGet.exe Command Line Tool" from the release page.
Restore packages from the build script
Modify your build script and add RestorePackages() near the beginning of the script. This will use the default parameters to retrieve all NuGet packages specified in "./**/packages.config" files.
If you need to use different parameters please use the RestorePackage task directly.
Download latest version of FAKE via NuGet
If you don't want to store FAKE.exe and its components in your repository, you can use a batch file which downloads it before the build:
@echo off
cls
"tools\nuget\nuget.exe" "install" "FAKE" "-OutputDirectory" "tools" "-ExcludeVersion"
"tools\FAKE\tools\Fake.exe" build.fsx
pause
Creating NuGet packages
Creating a .nuspec template
The basic idea to create nuget packages is to create a .nuspec template and let FAKE fill out the missing parts. The following code shows such .nuspec file from the OctoKit project.
The .nuspec template contains some placeholders like @build.number@
which can be replaced later by the build script.
It also contains some specific information like the copyright which is not handled by FAKE.
The following table gives the correspondence between the placeholders and the fields of the record type used by the NuGet task.
Placeholder |
replaced by ( |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a combination of |
|
a combination of |
|
a list of contentFiles to be included in the nuget package |
|
a list of source, target, and exclude strings for files to be included in the nuget package |
Setting up the build script
In the build script you need to create a target which executes the NuGet task:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: |
|
There are a couple of interesting things happening here. In this sample FAKE created:
- a copy of the .nuspec file
- filled in all the specified parameters
- created the NuGet package
- pushed it to nuget.org using the given
myAccessKey
.
Handling package dependencies
If your project depends on other projects it is possible to specify these dependencies in the .nuspec definition (see also Nuget docs). Here is a small sample which sets up dependencies for different framework versions:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: |
|
Explicit assembly references
If you want to have auxiliary assemblies next to the ones that get referenced by the target project, you can place all the needed files in the lib
directory and explicitly specify which of them should be referenced (see Nuget docs) via the References
and ReferencesByFramework
fields.
Here is a code snippet showing how to use these:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: |
|
Explicit file specifications
If you want to specify exactly what files are packaged and where they are placed in the resulting NuGet package you can specify the Files property directly. This is exactly like having the Files element of a nuspec filled out ahead of time. Here is a code snippet showing how to use this:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: |
|
NuGet ContentFiles
ContentFiles in NuGet are static files that the NuGet client will make available to a project for inclusion in the project. For more information see this blog post explaining the difference and relationship to the Files element.
The ContentFiles param supports all the documented nuspec contentFiles attributes.
It takes a value of type list<string*string option*string option*bool option*bool option>
where each tuple part maps respectively to the following attributes:
- include
- exclude
- buildAction
- copyToOutput
- flatten
Here is a code snippet showing how to use ContentFiles:
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|