-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.fsx
192 lines (161 loc) · 6.06 KB
/
build.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
192
#load ".fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.DotNet
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.Core.TargetOperators
open System.Text.RegularExpressions
open System.Xml.Linq
/// given a project name, generate a path to write the concatenated
/// source code found in the project.
let concatFileNamePath projName =
sprintf "./%s.fs" projName
/// this will be inserted to the top of the concatenated file.
let concatHeader = """(*
The MIT License
prelude.fs - my prelude
Copyright(c) 2018 cannorin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)
// this file is automatically generated by build.fsx.
// do not edit this directly.
"""
/// given the common namespace among the source files, generate module/namespace
/// definition that will be placed at the top of the concatenated file.
let fileLevelGrouping name =
sprintf "[<AutoOpen>]\nmodule internal %s" name
let regex pattern input =
let m = Regex.Match(input, pattern)
if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
else None
let inline xname s = XName.Get s
let inline logfailwithf format =
Printf.kprintf (fun s -> Trace.traceError s; failwith s) format
let concatSources path =
let proj = MSBuild.loadProject path
let sources =
proj.Descendants(xname "Compile")
|> Seq.map (fun x ->
let fileName = x.Attribute(xname "Include").Value
let path = Path.getDirectory path @@ fileName
fileName, File.read path)
|> Seq.filter (fun (fileName, _) -> fileName <> "AssemblyInfo.fs")
|> Seq.cache
if sources |> Seq.isEmpty then
logfailwithf "no source files to concat in the project '%s'." path
let commonNamespace =
let nss =
sources
|> Seq.map (fun (_, source) ->
source |> Seq.choose (regex @"^namespace +([a-zA-Z_']+).*$") |> Seq.tryHead
)
if nss |> Seq.forall Option.isSome
&& (nss |> Seq.distinct |> Seq.length) = 1 then
nss |> Seq.head |> Option.get |> List.head
else
logfailwithf "the source files does not have the same namespace definition"
let inline isNamespaceLine str =
let rgx = regex <| sprintf "^namespace +%s.*$" commonNamespace
rgx str |> Option.isSome
let source = seq {
yield concatHeader
yield fileLevelGrouping commonNamespace
yield ""
for (fileName, codeLines) in sources do
yield sprintf "// from: %s" fileName
yield!
codeLines |> Seq.skipWhile (not << isNamespaceLine)
|> Seq.skipWhile isNamespaceLine
|> Seq.skipWhile String.isNullOrWhiteSpace
yield ""
}
let path =
path |> String.replace (Path.getDirectory path) ""
|> Path.changeExtension ""
|> String.trimEndChars [|'.'|]
|> String.trimChars [|'/'|]
|> concatFileNamePath
Trace.tracefn "generating '%s'..." path
File.write false path source
type Date = System.DateTimeOffset
let (|Regex|_|) pattern input =
let m = Regex.Match(input, pattern)
if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
else None
let loadReleaseNoteWithAutoIncrement releaseNote =
let y2k = Date.Parse "1/1/2010+0000"
let today = Date.UtcNow
let dayssincey2k = (today - y2k).Days
let secs = today.TimeOfDay.TotalSeconds |> int |> (/) <| 2
let lines = File.read releaseNote
let topLine =
match (lines |> Seq.tryHead) with
| Some (Regex @"^(\*|\#\# New in) ([0-9]+)\.([0-9]+)\.([0-9]+)(?:\.\*) (.+)$" [head; maj; min; rev; tail]) ->
sprintf "%s %s.%s.%s.%i %s" head maj min rev (today.DayOfYear * 10000 + secs) tail
| Some (Regex @"^(\*|\#\# New in) ([0-9]+)\.([0-9]+)(?:\.\*) (.+)$" [head; maj; min; tail]) ->
sprintf "%s %s.%s.%i.%i %s" head maj min dayssincey2k secs tail
| Some x -> x
| None ->
sprintf "0.%i.%i.%i" (today.Year - 2000) today.DayOfYear secs
lines |> Seq.mapi (fun i x -> if i = 0 then topLine else x)
|> ReleaseNotes.parse
// Read additional information from the release notes document
let release = loadReleaseNoteWithAutoIncrement "RELEASE_NOTES.md"
release.AssemblyVersion |> Trace.tracefn "Auto version: %s"
Target.create "WriteVersion" (fun _ ->
AssemblyInfoFile.createFSharp "./src/AssemblyInfo.fs" [
AssemblyInfo.Version release.AssemblyVersion
AssemblyInfo.FileVersion release.AssemblyVersion
]
)
Target.create "Clean" (fun _ ->
!! "src/**/bin"
++ "src/**/obj"
|> Shell.cleanDirs
)
Target.create "Build" (fun _ ->
!! "src/**/*.*proj"
|> Seq.iter (DotNet.build id)
)
Target.create "Concat" (fun _ ->
!! "src/**/*.*proj"
|> Seq.iter concatSources
)
Target.create "Pack" (fun _ ->
!! "src/**/*.*proj"
|> Seq.iter (DotNet.pack (fun opt ->
{ opt with
Configuration = DotNet.BuildConfiguration.Release
OutputPath = Some "../bin/packages/"
MSBuildParams = {
opt.MSBuildParams with
Properties = [
"PackageVersion", release.NugetVersion
] @ opt.MSBuildParams.Properties
}
}
))
)
Target.create "All" ignore
"Clean"
==> "WriteVersion"
==> "Build"
==> "Concat"
==> "Pack"
==> "All"
Target.runOrDefault "All"