forked from sergey-tihon/OpenNLP.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.fsx
300 lines (255 loc) · 9.59 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#r @"paket:
source https://nuget.org/api/v2
framework netstandard2.0
nuget FSharp.Core 5.0.0
nuget Mono.Cecil
nuget System.IO.Compression.ZipFile
nuget Graphviz.DotLanguage
nuget Fake.Core.Target
nuget Fake.Core.Process
nuget Fake.Core.ReleaseNotes
nuget Fake.IO.FileSystem
nuget Fake.DotNet.Cli
nuget Fake.DotNet.MSBuild
nuget Fake.DotNet.AssemblyInfoFile
nuget Fake.DotNet.Paket
nuget Fake.Tools.Git
nuget Fake.Api.GitHub //"
#if !FAKE
#load "./.fake/build.fsx/intellisense.fsx"
#r "netstandard" // Temp fix for https://github.com/fsharp/FAKE/issues/1985
#endif
open System
open System.IO
open System.IO.Compression
open System.Collections.Generic
open System.Text.RegularExpressions
open Microsoft.FSharp.Core
open Mono.Cecil
open Fake
open Fake.Core.TargetOperators
open Fake.Core
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.DotNet
open Fake.Tools
open DotLang.CodeAnalysis.Syntax
Target.initEnvironment()
// --------------------------------------------------------------------------------------
// Provide project-specific details
let [<Literal>]root = __SOURCE_DIRECTORY__
// Read additional information from the release notes document
let release = ReleaseNotes.load "RELEASE_NOTES.md"
// --------------------------------------------------------------------------------------
// IKVM.NET compilation helpers
let fixFileNames path =
use file = File.Open(path, FileMode.Open, FileAccess.ReadWrite)
use archive = new ZipArchive(file, ZipArchiveMode.Update)
archive.Entries
|> Seq.toList
|> List.filter(fun x -> x.FullName.Contains(":"))
|> List.iter (fun entry ->
printfn "%s " entry.FullName
let newName = entry.FullName.Replace(":","_")
let newEntry = archive.CreateEntry(newName)
begin
use a = entry.Open()
use b = newEntry.Open()
a.CopyTo(b)
end
entry.Delete()
)
let unZipTo toDir file =
Trace.trace "Renaming files inside zip archive ..."
fixFileNames file
Trace.tracefn "Unzipping file '%s' to '%s'" file toDir
Compression.ZipFile.ExtractToDirectory(file, toDir)
let restoreFolderFromFile folder zipFile =
if not <| Directory.Exists folder then
zipFile |> unZipTo folder
// Location of IKVM Compiler & ildasm / ilasm
let ikvmcExe = root </> "paket-files/sergeytihon.files.wordpress.com/ikvm-8.1.5717.0/bin/ikvmc.exe"
type IKVMcTask(jar:string, version:string) =
member __.JarFile = jar
member __.Version = version
member __.DllFile = Path.ChangeExtension(Path.GetFileName(jar), ".dll")
member val Dependencies = List.empty<IKVMcTask> with get, set
member this.GetDllReferences() =
seq {
for t in this.Dependencies do
yield! t.GetDllReferences()
yield this.DllFile
}
let timeOut = TimeSpan.FromSeconds(120.0)
let IKVMCompile workingDirectory keyFile tasks =
let ikvmc args =
(if Environment.isWindows
then CreateProcess.fromRawCommandLine ikvmcExe args
else CreateProcess.fromRawCommandLine "mono" (ikvmcExe + " " + args))
|> CreateProcess.withWorkingDirectory (DirectoryInfo(workingDirectory).FullName)
|> CreateProcess.withTimeout timeOut
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
let newKeyFile =
if (File.Exists keyFile) then
let file = workingDirectory @@ (Path.GetFileName(keyFile))
File.Copy(keyFile, file, true)
Path.GetFileName(file)
else keyFile
let bprintf = Microsoft.FSharp.Core.Printf.bprintf
let cache = HashSet<_>()
let rec compile (task:IKVMcTask) =
let getIKVMCommandLineArgs() =
let sb = Text.StringBuilder()
task.Dependencies
|> Seq.collect (fun x ->
compile x
x.GetDllReferences()
)
|> Seq.distinct
|> Seq.iter (bprintf sb " -r:%s")
if not <| String.IsNullOrEmpty(task.Version)
then task.Version |> bprintf sb " -version:%s"
bprintf sb " %s -out:%s" task.JarFile task.DllFile
sb.ToString()
if cache.Contains task.JarFile
then
Trace.tracefn "Task '%s' already compiled" task.JarFile
else
//File.Copy(task.JarFile, workingDirectory @@ (Path.GetFileName(task.JarFile)) ,true)
ikvmc <| getIKVMCommandLineArgs()
if (File.Exists(newKeyFile)) then
let key = FileInfo(newKeyFile).FullName
|> File.ReadAllBytes
ModuleDefinition
.ReadModule(task.DllFile, ReaderParameters(InMemory=true))
.Write(task.DllFile, WriterParameters(StrongNameKeyBlob=key))
cache.Add(task.JarFile) |> ignore
tasks |> Seq.iter compile
/// Infer dependencies between *.jar files
let buildJDepsGraph dotFile jarFolder (jars:string seq)=
"-filter:package -dotoutput dot " + String.Join(" ", jars)
|> CreateProcess.fromRawCommandLine "jdeps"
|> CreateProcess.withWorkingDirectory jarFolder
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
jarFolder </> "dot/summary.dot"
|> Shell.copyFile dotFile
let loadJDepsGraph dotFile (jars:Set<string>) =
let parser = dotFile |> File.ReadAllText |> Parser
parser.Parse().Graphs.[0].Statements
|> Seq.cast<EdgeStatementSyntax>
|> Seq.map (fun x->
(x.Left :?> NodeStatementSyntax).Identifier.IdentifierToken.StringValue,
(x.Right :?> NodeStatementSyntax).Identifier.IdentifierToken.StringValue)
|> Seq.filter (fun (x,y) -> jars.Contains x && jars.Contains y)
|> Seq.groupBy fst
|> Seq.map (fun (x,xs)-> x, xs |> Seq.map snd |> Seq.toList)
|> Map.ofSeq
/// Build IKVm task with proper dependencies order (topological sort)
let createCompilationGraph dotFile jarFolder (jars:Set<string>) =
let deps = loadJDepsGraph dotFile jars
let cache = Dictionary<_,_>()
let pattern = Regex("-(?<version>[0-9.]*).jar$", RegexOptions.Compiled)
let getVersion str =
let m = pattern.Match(str)
if m.Success
then m.Groups.["version"].Value |> Some
else None
let rec getTask name =
match cache.TryGetValue name with
| true, task -> task
| _ ->
let deps =
Map.tryFind name deps
|> Option.defaultValue []
|> List.map getTask
let ver = getVersion name
|> Option.defaultValue release.AssemblyVersion
let task = IKVMcTask(jarFolder </> name, ver, Dependencies = deps)
cache.Add(name, task)
task
seq {
for jar in jars do
if not <| cache.ContainsKey jar
then yield getTask jar
} |> Seq.toList
let copyPackages fromDir toDir =
if (not <| Directory.Exists(toDir))
then Directory.CreateDirectory(toDir) |> ignore
Directory.GetFiles(fromDir)
|> Seq.filter (fun x -> Path.GetExtension(x) = ".nupkg")
|> Seq.iter (fun x -> File.Copy(x, Path.Combine(toDir, Path.GetFileName(x)), true))
let removeNotAssembliesFrom dir =
!! (dir + @"/*.*")
-- (dir + @"/*.dll")
|> Seq.iter (System.IO.File.Delete)
let createNuGetPackage template =
Paket.pack(fun p ->
{ p with
ToolType = Fake.DotNet.ToolType.CreateLocalTool()
TemplateFile = template
OutputPath = "bin"
Version = release.NugetVersion
ReleaseNotes = String.toLines release.Notes})
let keyFile = @"./nuget/OpenNLP.snk"
// --------------------------------------------------------------------------------------
// Clean build results
Target.create "Clean" (fun _ ->
Shell.cleanDirs ["bin"; "temp"]
)
// --------------------------------------------------------------------------------------
// Compile Stanford.NLP.CoreNLP and build NuGet package
let openNLPDir = root </> "paket-files/dlcdn.apache.org/apache-opennlp-1.9.4/lib"
Target.create "Compile" (fun _ ->
// Get *.jar file for compilation
let jars =
Directory.GetFiles(openNLPDir, "*.jar")
|> Array.map (Path.GetFileName)
|> Set.ofArray
if (jars.IsEmpty)
then failwith "Found 0 *.jar files"
let ikvmDir = @"bin/lib"
Shell.mkdir ikvmDir
let dotFile = "nuget/OpenNLP.dot"
if not <| File.Exists dotFile then
buildJDepsGraph dotFile openNLPDir jars
createCompilationGraph dotFile openNLPDir jars
|> IKVMCompile ikvmDir keyFile
)
Target.create "NuGet" (fun _ ->
root </> "nuget/OpenNLP.template"
|> createNuGetPackage
)
// --------------------------------------------------------------------------------------
// Build and run test projects
Target.create "BuildTests" (fun _ ->
let result = DotNet.exec id "build" "OpenNLP.NET.sln -c Release"
if not result.OK
then failwithf "Build failed: %A" result.Errors
)
Target.create "RunTests" (fun _ ->
let libs = !! "tests/**/bin/Release/net45/*.Tests.dll"
let args = String.Join(" ", libs)
let runner = "packages/NUnit.ConsoleRunner/tools/nunit3-console.exe"
(if Environment.isWindows
then CreateProcess.fromRawCommandLine runner args
else CreateProcess.fromRawCommandLine "mono" (runner + " " + args))
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
)
// --------------------------------------------------------------------------------------
// Run all targets by default. Invoke 'build <Target>' to override
Target.create "All" ignore
Target.create "Release" ignore
"Clean"
==> "Compile"
==> "NuGet"
==> "BuildTests"
==> "RunTests"
==> "All"
Target.runOrDefault "All"