Skip to content

Instantly share code, notes, and snippets.

@silky
Last active January 4, 2020 10:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save silky/bf304785a46db32b5cc8 to your computer and use it in GitHub Desktop.
Save silky/bf304785a46db32b5cc8 to your computer and use it in GitHub Desktop.
FAKE build file example with multiple targets specified in many files.
// "bar_build.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load @"common_targets.fsx" // the order is important.
#load @"foo_targets.fsx"
#load @"bar_targets.fsx"
open Fake
setEnvironVar "PROJECT_NAME" "BarProj"
RunTargetOrDefault "Bar"
// "bar_targets.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load @"CommonFuncs.fsx"
open Fake
open CommonFuncs
module Bar =
Description "Bars"
Target "Bar" (fun _ -> trace "And you have me building software ..." )
"Env" ==> "Bar"
"Foo" ==> "Bar"
// "common.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load "CommonFuncs.fsx"
open Fake
open System.Collections.Generic
open System
open System.Threading
open CommonFuncs
open Fuchu
module Common =
Target "Env" (fun _ -> trace "Env")
Target "Tested" (fun _ ->
let k = CommonFuncs.tests |> run
if k = 1 then
failwith "Tests failed!"
)
// Horrifyingly offensive but "Fuchu" defines its own "==>" operator,
// so we can't use that here to express this dependency. We really ask
// that before anyone does anything, they check that the build script
// itself works.
Dependencies "Env" ["Tested"]
// "CommonFuncs.fsx"
#r "../../tools/FAKE/FakeLib.dll"
#r "../../tools/Fuchu/lib/net40-client/Fuchu.dll"
open Fake
open System.Collections.Generic
open System
open System.IO
open System.Threading
open Fuchu
module CommonFuncs =
let projectName =
fun _ -> environVar "PROJECT_NAME"
let FBoo () =
2
/// Runs a command and, if it doesn't return 0, fails the build.
let ExecAndCheck (cmd : string) (dir :string) (args_list : List<string>) : unit =
let args = String.concat " " args_list
let result = execProcess (fun info ->
info.FileName <- cmd
info.WorkingDirectory <- dir
info.Arguments <- args) System.TimeSpan.MaxValue
if not result then
let msg = sprintf "Failed running '%s' with args '%s'." cmd args
raise <| new Exception(msg)
[<Tests>]
let ``tests`` =
TestList [
testCase "Want FBoo () = 2" <| fun _ -> Assert.Equal("Foo () == 2", FBoo (), 2)
// testCase "Want FBoo () = 3" <| fun _ -> Assert.Equal("Foo () == 3", FBoo (), 3)
]
// "foo_build.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load @"common_targets.fsx" // the order is important
#load @"foo_targets.fsx"
open Fake
setEnvironVar "PROJECT_NAME" "FooProj"
RunTargetOrDefault "Foo"
// "foo_targets.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load @"CommonFuncs.fsx"
open Fake
open CommonFuncs
module Foo =
Description "Foos"
Target "Foo" (fun _ ->
trace <| sprintf "Here I am, foo the size of a bar, in %s" (CommonFuncs.projectName ())
)
"Env" ==> "Foo"
// "marvin_build.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load @"common_targets.fsx" // order is important
#load @"foo_targets.fsx"
#load @"bar_targets.fsx"
#load @"marvin_targets.fsx"
open Fake
setEnvironVar "PROJECT_NAME" "MarvinProj"
RunTargetOrDefault "Marvin"
// "marvin_targets.fsx"
#r @"../../tools/FAKE/FakeLib.dll"
#load @"CommonFuncs.fsx"
open Fake
open CommonFuncs
module Marvin =
Target "Marvin" (fun _ -> ())
"Bar" ==> "Marvin"
"Foo" ==> "Marvin"
"Foo" ==> "Bar"
@MarkRobertJohnson
Copy link

MarkRobertJohnson commented May 29, 2016

I like your solution for the most part. I am wondering though, what is the best way to get variables defined in the primary build script into the included targets (The scripts are #load'd at the top of the main build.fsx, before the variables are even defined)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment