|
| 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 | +let flash = createProcess "uflash" |
| 76 | + |
| 77 | +let npm = |
| 78 | + let npmPath = |
| 79 | + match ProcessUtils.tryFindFileOnPath "npm" with |
| 80 | + | Some path -> path |
| 81 | + | None -> |
| 82 | + "npm was not found in path. Please install it and make sure it's available from your path. " + |
| 83 | + "See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info" |
| 84 | + |> failwith |
| 85 | + |
| 86 | + createProcess npmPath |
| 87 | + |
| 88 | +let run proc arg dir = |
| 89 | + proc arg dir |
| 90 | + |> Proc.run |
| 91 | + |> ignore |
| 92 | + |
| 93 | +let runParallel processes = |
| 94 | + processes |
| 95 | + |> Proc.Parallel.run |
| 96 | + |> ignore |
| 97 | + |
| 98 | +let runOrDefault args = |
| 99 | + try |
| 100 | + match args with |
| 101 | + | [| target |] -> Target.runOrDefault target |
| 102 | + | _ -> Target.runOrDefault "Run" |
| 103 | + 0 |
| 104 | + with e -> |
| 105 | + printfn "%A" e |
| 106 | + 1 |
0 commit comments