-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild-utils.fsx
191 lines (167 loc) · 6.69 KB
/
build-utils.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#load ".fake/build.fsx/intellisense.fsx"
open System
open System.Text
open System.Text.RegularExpressions
open System.IO
open System.Collections.Generic
open Fake.Core
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
module Util =
open System.Net
let retryIfFails maxRetries f =
let rec loop retriesRemaining =
try
f ()
with _ when retriesRemaining > 0 ->
loop (retriesRemaining - 1)
loop maxRetries
let (|RegexReplace|_|) =
let cache = new Dictionary<string, Regex>()
fun pattern (replacement: string) input ->
let regex =
match cache.TryGetValue(pattern) with
| true, regex -> regex
| false, _ ->
let regex = Regex pattern
cache.Add(pattern, regex)
regex
let m = regex.Match(input)
if m.Success
then regex.Replace(input, replacement) |> Some
else None
let join pathParts =
Path.Combine(Array.ofSeq pathParts)
let run workingDir fileName args =
printfn "CWD: %s" workingDir
let fileName, args =
if Environment.isUnix
then fileName, args else "cmd", ("/C " + fileName + " " + args)
let exitCode =
Process.execSimple (fun info ->
{ info with
FileName = fileName
WorkingDirectory = workingDir
Arguments = args }) TimeSpan.MaxValue
if exitCode <> 0 then failwith (sprintf "'%s> %s %s' task failed with code %d" workingDir fileName args exitCode)
let start workingDir fileName args =
let p = new System.Diagnostics.Process()
p.StartInfo.FileName <- fileName
p.StartInfo.WorkingDirectory <- workingDir
p.StartInfo.Arguments <- args
p.Start() |> ignore
p
let runAndReturn workingDir fileName args =
printfn "CWD: %s" workingDir
let fileName, args =
if Environment.isUnix
then fileName, args else "cmd", ("/C " + fileName + " " + args)
let result =
Process.execWithResult (fun info ->
{ info with
FileName = fileName
WorkingDirectory = workingDir
Arguments = args}) TimeSpan.MaxValue
result//.Messages |> String.concat "\n"
let rmdir dir =
if Environment.isUnix
then Shell.rm_rf dir
// Use this in Windows to prevent conflicts with paths too long
else run "." "cmd" ("/C rmdir /s /q " + Path.GetFullPath dir)
let visitFile (visitor: string->string) (fileName : string) =
File.ReadAllLines(fileName)
|> Array.map (visitor)
|> fun lines -> File.WriteAllLines(fileName, lines)
let replaceLines (replacer: string->Match->string option) (reg: Regex) (fileName: string) =
fileName |> visitFile (fun line ->
let m = reg.Match(line)
if not m.Success
then line
else
match replacer line m with
| None -> line
| Some newLine -> newLine)
let normalizeVersion (version: string) =
let i = version.IndexOf("-")
if i > 0 then version.Substring(0, i) else version
type ComparisonResult = Smaller | Same | Bigger
let foldi f init (xs: 'T seq) =
let mutable i = -1
(init, xs) ||> Seq.fold (fun state x ->
i <- i + 1
f i state x)
let compareVersions (expected: string) (actual: string) =
if actual = "*" // Wildcard for custom fable-core builds
then Same
else
let expected = expected.Split('.', '-')
let actual = actual.Split('.', '-')
(Same, expected) ||> foldi (fun i comp expectedPart ->
match comp with
| Bigger -> Bigger
| Same when actual.Length <= i -> Smaller
| Same ->
let actualPart = actual.[i]
match Int32.TryParse(expectedPart), Int32.TryParse(actualPart) with
// TODO: Don't allow bigger for major version?
| (true, expectedPart), (true, actualPart) ->
if actualPart > expectedPart
then Bigger
elif actualPart = expectedPart
then Same
else Smaller
| _ ->
if actualPart = expectedPart
then Same
else Smaller
| Smaller -> Smaller)
module Npm =
let script workingDir script args =
sprintf "run %s -- %s" script (String.concat " " args)
|> Util.run workingDir "npm"
let scriptAndReturn workingDir script args =
sprintf "run %s -- %s" script (String.concat " " args)
|> Util.runAndReturn workingDir "npm"
let install workingDir modules =
sprintf "install %s" (String.concat " " modules)
|> Util.run workingDir "npm"
let ci workingDir modules =
sprintf "ci %s" (String.concat " " modules)
|> Util.run workingDir "npm"
let prune workingDir isProd =
// https://docs.npmjs.com/cli/prune
sprintf "prune %s" (if isProd then "--production" else "--no-production")
|> Util.run workingDir "npm"
let command workingDir command args =
sprintf "%s %s" command (String.concat " " args)
|> Util.run workingDir "npm"
let commandAndReturn workingDir command args =
sprintf "%s %s" command (String.concat " " args)
|> Util.runAndReturn workingDir "npm"
let getLatestVersion package tag =
let package =
match tag with
| Some tag -> package + "@" + tag
| None -> package
commandAndReturn "." "show" [package; "version"]
let updatePackageKeyValue f pkgDir keys =
let pkgJson = Path.Combine(pkgDir, "package.json")
let reg =
String.concat "|" keys
|> sprintf "\"(%s)\"\\s*:\\s*\"(.*?)\""
|> Regex
let lines =
File.ReadAllLines pkgJson
|> Array.map (fun line ->
let m = reg.Match(line)
if m.Success then
match f(m.Groups.[1].Value, m.Groups.[2].Value) with
| Some(k,v) -> reg.Replace(line, sprintf "\"%s\": \"%s\"" k v)
| None -> line
else line)
File.WriteAllLines(pkgJson, lines)
module Node =
let run workingDir script args =
let args = sprintf "%s %s" script (String.concat " " args)
Util.run workingDir "node" args