Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for contentFiles to Fake.DotNet.NuGet packaging #2165

Merged
merged 5 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions help/markdown/dotnet-nuget.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Placeholder | replaced by (`NuGetParams` record field)
`@copyright@` | `Copyright`
`@dependencies@` | a combination of `Dependencies` and `DependenciesByFramework`
`@references@` | a combination of `References` and `ReferencesByFramework`
`@contentFiles@` | a list of [contentFiles](https://docs.microsoft.com/en-us/nuget/reference/nuspec#using-the-contentfiles-element-for-content-files) to be included in the nuget package
`@files@` | a list of source, target, and exclude strings for files to be included in the nuget package

## Setting up the build script
Expand Down Expand Up @@ -157,3 +158,31 @@ NuGet (fun p ->
})
"template.nuspec"
```

## 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](https://blog.nuget.org/20160126/nuget-contentFiles-demystified.html) explaining the difference and relationship to the Files element.

The ContentFiles param supports all the documented nuspec contentFiles [attributes](https://docs.microsoft.com/en-us/nuget/reference/nuspec#using-the-contentfiles-element-for-content-files).

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]((https://docs.microsoft.com/en-us/nuget/reference/nuspec#using-the-contentfiles-element-for-content-files)):

* include
* exclude
* buildAction
* copyToOutput
* flatten

Here is a code snippet showing how to use ContentFiles:

```fsharp
NuGet (fun p ->
{p with
// ...
ContentFiles = [
(@"**/*.md", Some @"**/Exclude/*.md", Some @"Content", None, None)
]
// ...
})
"template.nuspec"
```
28 changes: 28 additions & 0 deletions src/app/Fake.DotNet.NuGet/NuGet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type NuGetParams =
SymbolPackage : NugetSymbolPackage
Properties : list<string * string>
Files : list<string*string option*string option>
ContentFiles : list<string*string option*string option*bool option*bool option>
Language : string}

/// NuGet default parameters
Expand Down Expand Up @@ -111,6 +112,7 @@ let NuGetDefaults() =
SymbolPackage = NugetSymbolPackage.ProjectFile
Properties = []
Files = []
ContentFiles = []
Language = null }

/// Creates a string which tells NuGet that you require exactly this package version.
Expand Down Expand Up @@ -214,6 +216,31 @@ let private createNuSpecFromTemplate parameters (templateNuSpec:FileInfo) =

let dependenciesXml = sprintf "<dependencies>%s</dependencies>" (dependencies + dependenciesByFramework)

let contentFilesTags =
parameters.ContentFiles
|> Seq.map (fun (incl, exclArg, buildActionArg, copyToOutputArg, flattenArg) ->
let excl =
match exclArg with
| Some x -> sprintf " exclude=\"%s\"" x
| _ -> String.Empty
let buildAction =
match buildActionArg with
| Some x -> sprintf " buildAction=\"%s\"" x
| _ -> String.Empty
let copyToOutput =
match copyToOutputArg with
| Some x -> sprintf " copyToOutput=\"%b\"" x
| _ -> String.Empty
let flatten =
match flattenArg with
| Some x -> sprintf " flatten=\"%b\"" x
| _ -> String.Empty

sprintf "<files include=\"%s\"%s%s%s%s />" incl excl buildAction copyToOutput flatten)
|> String.toLines

let contentFilesXml = sprintf "<contentFiles>%s</contentFiles>" contentFilesTags

let filesTags =
parameters.Files
|> Seq.map (fun (source, target, exclude) ->
Expand Down Expand Up @@ -253,6 +280,7 @@ let private createNuSpecFromTemplate parameters (templateNuSpec:FileInfo) =
|> List.append [ "@dependencies@", dependenciesXml
"@references@", referencesXml
"@frameworkAssemblies@", frameworkAssembliesXml
"@contentFiles@", contentFilesXml
"@files@", filesXml ]

Templates.replaceInFiles replacements [ specFile ]
Expand Down