patternMinor
Generic build script using Fake
Viewed 0 times
scriptgenericusingbuildfake
Problem
I have something like a generic build script for a group of projects. I am using Fake but my F# skills are pretty bad. Would you please take a look and point where I can improve it?
ORIGINAL
```
#I @"./bin/tools/FAKE/tools/"
#r @"./bin/tools/FAKE/tools/FakeLib.dll"
open System
open System.IO
open Fake
open Fake.Git
open Fake.FSharpFormatting
open Fake.AssemblyInfoFile
open Fake.ReleaseNotesHelper
type System.String with member x.contains (comp:System.StringComparison) str = x.IndexOf(str,comp) >= 0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BEGIN EDIT
let appName = getBuildParamOrDefault "appName" ""
let appType = getBuildParamOrDefault "appType" ""
let appSummary = getBuildParamOrDefault "appSummary" ""
let appDescription = getBuildParamOrDefault "appDescription" ""
let appAuthors = ["YourNameHere";]
// END EDIT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
let buildDir = @"./bin/Release" @@ appName
let releaseNotes = @"./src/" @@ appName @@ @"RELEASE_NOTES.md"
let release = LoadReleaseNotes releaseNotes
let nuget = environVar "NUGET"
let nugetOutDir = "./bin/nuget" @@ appName
Target "Clean" (fun _ -> CleanDirs [buildDir; nugetOutDir;])
Target "RestorePackages" (fun _ ->
let packagesDir = @"./src/packages"
!! "./**/packages.config"
|> Seq.iter (RestorePackage (fun p ->
{ p with
ToolPath = nuget
OutputPath = packagesDir }))
)
Target "Build" (fun _ ->
let appProjectFile = match appType with
| t when t = "msi" -> @"./src/" @@ appName + ".sln"
| _ -> @"./src/" @@ appName @@ appName + ".csproj"
!! appProjectFile
|> MSBuildRelease buildDir "Build"
|> Log "Build-Output: "
)
Target "CreateNuGet" (fun _ ->
let packages = [appName, appType]
for appName,appType in packages do
ORIGINAL
```
#I @"./bin/tools/FAKE/tools/"
#r @"./bin/tools/FAKE/tools/FakeLib.dll"
open System
open System.IO
open Fake
open Fake.Git
open Fake.FSharpFormatting
open Fake.AssemblyInfoFile
open Fake.ReleaseNotesHelper
type System.String with member x.contains (comp:System.StringComparison) str = x.IndexOf(str,comp) >= 0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BEGIN EDIT
let appName = getBuildParamOrDefault "appName" ""
let appType = getBuildParamOrDefault "appType" ""
let appSummary = getBuildParamOrDefault "appSummary" ""
let appDescription = getBuildParamOrDefault "appDescription" ""
let appAuthors = ["YourNameHere";]
// END EDIT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
let buildDir = @"./bin/Release" @@ appName
let releaseNotes = @"./src/" @@ appName @@ @"RELEASE_NOTES.md"
let release = LoadReleaseNotes releaseNotes
let nuget = environVar "NUGET"
let nugetOutDir = "./bin/nuget" @@ appName
Target "Clean" (fun _ -> CleanDirs [buildDir; nugetOutDir;])
Target "RestorePackages" (fun _ ->
let packagesDir = @"./src/packages"
!! "./**/packages.config"
|> Seq.iter (RestorePackage (fun p ->
{ p with
ToolPath = nuget
OutputPath = packagesDir }))
)
Target "Build" (fun _ ->
let appProjectFile = match appType with
| t when t = "msi" -> @"./src/" @@ appName + ".sln"
| _ -> @"./src/" @@ appName @@ appName + ".csproj"
!! appProjectFile
|> MSBuildRelease buildDir "Build"
|> Log "Build-Output: "
)
Target "CreateNuGet" (fun _ ->
let packages = [appName, appType]
for appName,appType in packages do
Solution
All of the match expressions can be simplified. For example,
Can be
Similarly,
Can be
In the first example above,
It looks like
While you're at it, change
match fileExists nugetPackagesFile with
| t when t = true -> ...
| _ -> ...Can be
match fileExists nugetPackagesFile with
| true -> ...
| _ -> ...Similarly,
match appType with
| t when t = "web" -> ...
| t when t = "nuget" -> ...
| _ -> ...Can be
match appType with
| "web" -> ...
| "nuget" -> ...
| _ -> ...In the first example above,
fun(A,B) -> A can be written as fst.It looks like
nugetDependenciesFlat is only used by exclude, where you're already appending "CodeContracts". So you can change the second match in nugetDependenciesFlat from _ -> ["CodeContracts"] to _ -> [].While you're at it, change
nugetDependenciesFlat @ ["CodeContracts"] to "CodeContracts" :: nugetDependenciesFlat. The reason being, prepending an element to a list is a constant-time operation, while appending will take time proportional to to the length of nugetDependenciesFlat. It won't make a noticeable difference since I assume nugetDependenciesFlat will be very short, but it's a good practice to get into.Code Snippets
match fileExists nugetPackagesFile with
| t when t = true -> ...
| _ -> ...match fileExists nugetPackagesFile with
| true -> ...
| _ -> ...match appType with
| t when t = "web" -> ...
| t when t = "nuget" -> ...
| _ -> ...match appType with
| "web" -> ...
| "nuget" -> ...
| _ -> ...Context
StackExchange Code Review Q#82517, answer score: 6
Revisions (0)
No revisions yet.