Skip to content

Instantly share code, notes, and snippets.

@timReynolds
Created December 10, 2015 16:25
Show Gist options
  • Save timReynolds/62eacb53debc1690caca to your computer and use it in GitHub Desktop.
Save timReynolds/62eacb53debc1690caca to your computer and use it in GitHub Desktop.
FAKE Build Process to create NuGet Packages
// include Fake lib
#r @"packages/FAKE/tools/FakeLib.dll"
#r @"System.Xml.Linq"
open Fake
open System
open System.IO
open System.Xml.Linq
open Fake.DotCover
open Fake.NuGet
let buildDir = currentDirectory @@ "build"
let artifactsDir = currentDirectory @@ "artifacts"
let toolsDir = currentDirectory @@ "tools"
let packagingDir = artifactsDir @@ "packages"
let nuGetPath = toolsDir @@ "nuget" @@ "nuget.exe"
let buildVersion = if isLocalBuild then "0.0.0.0" else buildVersion
let buildChangeset = environVarOrDefault "BUILD_VCS_NUMBER" "Unknown"
let solutionFile = environVarOrDefault "SOLUTION_FILE" "./ASIInfrastructure.sln"
let buildMode = getBuildParamOrDefault "buildMode" "Release"
let unitTestsFiles = (buildDir @@ "/*.UnitTests.dll")
let nuspecTemplate = (currentDirectory @@ "template.nuspec")
let nunitOptions p =
{ p with
TimeOut = TimeSpan.MaxValue
DisableShadowCopy = true
OutputFile = artifactsDir @@ "/TestResults.xml" }
let ensureToolIsDownloaded toolName downloadUrl =
CreateDir toolsDir
if not (TestDir (toolsDir @@ toolName)) then
let downloadFileName = Path.GetFileName(downloadUrl)
trace ("Downloading " + downloadFileName + " from " + downloadUrl)
let webClient = new System.Net.WebClient()
webClient.DownloadFile(downloadUrl, toolsDir @@ downloadFileName)
trace ("Unzip " + downloadFileName)
Unzip (toolsDir @@ toolName) (toolsDir @@ downloadFileName)
let Exec command =
let result = Shell.Exec(command)
if result <> 0 then failwithf "%s exited with error %d" command result
Target "Clean" (fun _ -> CleanDirs [buildDir; artifactsDir;] )
Target "EnsureCoverageDependencies" (fun _ ->
ensureToolIsDownloaded "dotCover" "http://download-ln.jetbrains.com/dotcover/dotCoverConsoleRunner.2.6.608.466.zip"
)
Target "SetVersion" (fun _ ->
tracefn "This build is version %s. Changeset %s" buildVersion buildChangeset
File.WriteAllText(artifactsDir @@ "/version.txt", buildVersion + Environment.NewLine + buildChangeset)
)
Target "BuildUnitTests" (fun _ ->
!! "./**/*UnitTests.csproj"
|> MSBuildDebug buildDir "Build"
|> Log "BuildUnitTests-Output: "
)
Target "RunUnitTests" (fun _ ->
!! unitTestsFiles
|> NUnitParallel nunitOptions
)
Target "TestCoverage" (fun _ ->
let filters = ""
!! unitTestsFiles
|> DotCoverNUnit (fun p -> { p with
Output = artifactsDir @@ "NUnitDotCover.snapshot"
Filters = filters }) nunitOptions
)
Target "BuildSolution" (fun _ ->
MSBuildRelease buildDir "Build" ["./ASIInfrastructure.sln"]
|> Log "BuildSolution-Output: "
)
Target "CreatePackages" (fun _ ->
CreateDir packagingDir
// Creates a nuget package for every project file in the current dir
// Determines
!! "./**/*.csproj"
-- "./**/*Tests*"
|> Seq.iter ( fun projectFilePath ->
let projectName = Path.GetFileNameWithoutExtension(projectFilePath)
let projectDir = Path.GetDirectoryName(projectFilePath)
let nuspecFileLocation = buildDir @@ projectName + ".nuspec"
let nuspecDependencies =
if fileExists (projectDir @@ "packages.config") then
let projectPackagesConfig = System.Xml.Linq.XDocument.Load(projectDir @@ "packages.config")
let projectDependencies = projectPackagesConfig.Descendants(XName.Get("package", projectPackagesConfig.Root.Name.NamespaceName))
[for dep in projectDependencies -> (dep.Attribute(XName.Get("id")).Value, dep.Attribute(XName.Get("version")).Value)]
else
[]
let nuspecFiles = [
(buildDir @@ projectName + ".dll", Some "lib", None)
(buildDir @@ projectName + ".pdb", Some "lib", None)
]
printfn "Creating NuGet Package for %s" projectName
printfn "Nuspec file location %s" nuspecFileLocation
// create nuspec file from the template
CopyFile nuspecFileLocation nuspecTemplate
NuGet (fun p ->
{p with
OutputPath = packagingDir
WorkingDir = buildDir
Project = projectName
Description = projectName
Summary = projectName
Version = "1.0." + buildVersion
Dependencies = nuspecDependencies
Files = nuspecFiles })
nuspecFileLocation
)
)
Target "Default" DoNothing
"Clean"
==> "SetVersion"
==> "BuildSolution"
==> "RunUnitTests"
==> "EnsureCoverageDependencies"
==> "TestCoverage"
==> "CreatePackages"
==> "Default"
RunTargetOrDefault "Default"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment