1- namespace Giraffe
2-
3- [<AutoOpen>]
4- module Helpers =
5- open System
6- open System.IO
7-
8- /// <summary>
9- /// Checks if an object is not null.
10- /// </summary>
11- /// <param name="x">The object to validate against `null`.</param>
12- /// <returns>Returns true if the object is not null otherwise false.</returns>
13- let inline isNotNull x = not ( isNull x)
14-
15- /// <summary>
16- /// Converts a string into a string option where null or an empty string will be converted to None and everything else to Some string.
17- /// </summary>
18- /// <param name="str">The string value to be converted into an option of string.</param>
19- /// <returns>Returns None if the string was null or empty otherwise Some string.</returns>
20- let inline strOption ( str : string ) =
21- if String.IsNullOrEmpty str then None else Some str
22-
23- /// <summary>
24- /// Reads a file asynchronously from the file system.
25- /// </summary>
26- /// <param name="filePath">The absolute path of the file.</param>
27- /// <returns>Returns the string contents of the file wrapped in a Task.</returns>
28- // let readFileAsStringAsync (filePath : string) =
29- // task {
30- // use reader = new StreamReader(filePath)
31- // return! reader.ReadToEndAsync()
32- // }
1+ module Helpers
2+
3+ open Fake.Core
4+
5+ let initializeContext () =
6+ let execContext = Context.FakeExecutionContext.Create false " build.fsx" [ ]
7+ Context.setExecutionContext ( Context.RuntimeContext.Fake execContext)
8+
9+ module Proc =
10+ module Parallel =
11+ open System
12+
13+ let locker = obj()
14+
15+ let colors =
16+ [| ConsoleColor.Blue
17+ ConsoleColor.Yellow
18+ ConsoleColor.Magenta
19+ ConsoleColor.Cyan
20+ ConsoleColor.DarkBlue
21+ ConsoleColor.DarkYellow
22+ ConsoleColor.DarkMagenta
23+ ConsoleColor.DarkCyan |]
24+
25+ let print color ( colored : string ) ( line : string ) =
26+ lock locker
27+ ( fun () ->
28+ let currentColor = Console.ForegroundColor
29+ Console.ForegroundColor <- color
30+ Console.Write colored
31+ Console.ForegroundColor <- currentColor
32+ Console.WriteLine line)
33+
34+ let onStdout index name ( line : string ) =
35+ let color = colors.[ index % colors.Length]
36+ if isNull line then
37+ print color $" {name}: --- END ---" " "
38+ else if String.isNotNullOrEmpty line then
39+ print color $" {name}: " line
40+
41+ let onStderr name ( line : string ) =
42+ let color = ConsoleColor.Red
43+ if isNull line |> not then
44+ print color $" {name}: " line
45+
46+ let redirect ( index , ( name , createProcess )) =
47+ createProcess
48+ |> CreateProcess.redirectOutputIfNotRedirected
49+ |> CreateProcess.withOutputEvents ( onStdout index name) ( onStderr name)
50+
51+ let printStarting indexed =
52+ for ( index, ( name, c: CreateProcess<_>)) in indexed do
53+ let color = colors.[ index % colors.Length]
54+ let wd =
55+ c.WorkingDirectory
56+ |> Option.defaultValue " "
57+ let exe = c.Command.Executable
58+ let args = c.Command.Arguments.ToStartInfo
59+ print color $" {name}: {wd}> {exe} {args}" " "
60+
61+ let run cs =
62+ cs
63+ |> Seq.toArray
64+ |> Array.indexed
65+ |> fun x -> printStarting x; x
66+ |> Array.map redirect
67+ |> Array.Parallel.map Proc.run
68+
69+ let createProcess exe arg dir =
70+ CreateProcess.fromRawCommandLine exe arg
71+ |> CreateProcess.withWorkingDirectory dir
72+ |> CreateProcess.ensureExitCode
73+
74+ let dotnet = createProcess " dotnet"
75+
76+
77+ let pytest = createProcess " pytest"
78+ let poetry = createProcess " poetry"
79+
80+ let npm =
81+ let npmPath =
82+ match ProcessUtils.tryFindFileOnPath " npm" with
83+ | Some path -> path
84+ | None ->
85+ " npm was not found in path. Please install it and make sure it's available from your path. " +
86+ " See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
87+ |> failwith
88+
89+ createProcess npmPath
90+
91+ let run proc arg dir =
92+ proc arg dir
93+ |> Proc.run
94+ |> ignore
95+
96+ let runParallel processes =
97+ processes
98+ |> Proc.Parallel.run
99+ |> ignore
100+
101+ let runOrDefault args =
102+ try
103+ match args with
104+ | [| target |] -> Target.runOrDefault target
105+ | _ -> Target.runOrDefault " Run"
106+ 0
107+ with e ->
108+ printfn " %A " e
109+ 1
0 commit comments