Skip to content

Instantly share code, notes, and snippets.

@kflu
Created November 16, 2016 06:40
Show Gist options
  • Save kflu/0e1f401457993239094ee5f591e23e06 to your computer and use it in GitHub Desktop.
Save kflu/0e1f401457993239094ee5f591e23e06 to your computer and use it in GitHub Desktop.
a drop in replacement for the core Fake functionality - dependency resolution and run tasks
(* It's a drop in replacement for the core Fake functionality - dependency resolution and run tasks
*)
open System
open System.Text
open System.Collections.Generic
open System.IO
let private args = Environment.GetCommandLineArgs() |> List.ofArray |> List.skip 2
printfn "All args: %A" args
let private conf (args : string list) : Map<string,string> =
// find all --<key>=<val>
args
|> List.map (fun x ->
let m = RegularExpressions.Regex.Match(x, "^--(\w+)=(\S+)$")
if m.Success then (m.Groups.[1].Value, m.Groups.[2].Value) |> Some else None)
|> List.filter (fun x -> x.IsSome)
|> List.map (fun x -> x.Value)
|> Map.ofSeq
let private parameters = args |> conf
printfn "Parameters are: %A" parameters
let getBuildParamOrDefault param def =
let res =
match parameters |> Map.tryFind param with
| Some res -> res
| _ -> def
printfn "getBuildParamOrDefault %s %s. Result: %s" param def res
res
let private tasks = new Dictionary<string, unit -> unit>()
let private deps = new Dictionary<string, string list>()
let Target (task : string) (body : unit -> unit) = tasks.Add(task, body)
let (==>) (x:string) (y:string) =
// y depends on x
match deps.TryGetValue y with
| false, _ -> deps.Item(y) <- [x]
| true, ls -> deps.Item(y) <- x :: ls
let rec private RunTask (task:string) (visited:string list) =
printfn "running task %s" task
if visited |> List.contains task then failwithf "Loop detected: %s" task
match deps.TryGetValue task with
| false, _ -> ()
| true, dep ->
dep |> List.iter (fun dep -> RunTask dep (task :: visited))
match tasks.TryGetValue task with
| false, _ -> printfn "Nothing to be executed for %s" task
| true, body ->
printfn "Executing %s" task
body ()
let RunTargetOrDefault (def : string) =
let task = if args.Length > 0 then args.[0] else def
RunTask task []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment