Repository navigation
Expand file tree
/
Copy pathSFD.ScriptTools.ScriptGenerator.fsx
More file actions
executable file
·412 lines (340 loc) · 15.9 KB
/
Copy pathSFD.ScriptTools.ScriptGenerator.fsx
File metadata and controls
executable file
·412 lines (340 loc) · 15.9 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env -S dotnet fsi --
#r "nuget: Microsoft.CodeAnalysis.CSharp.Workspaces, 4.9.2"
open System
open System.IO
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.CSharp
open Microsoft.CodeAnalysis.CSharp.Syntax
open Microsoft.CodeAnalysis.Formatting
// Constants
[<Literal>]
let GameScriptClassName = "GameScript"
// Name of the throwaway wrapper class used purely so Roslyn has something
// syntactically valid to parse and format around the welded members.
let wrapperClassName = "__SFDScriptGeneratorFormattingWrapper__"
// Argument parsing
let printUsage () =
printfn "Usage: SFD.ScriptTools.ScriptGenerator.fsx <file1.cs> [file2.cs ...] [-o|--output <path>] [-m|--minified] [-h|--help]"
printfn ""
printfn " <file.cs>... One or more C# source files to weld together"
printfn " -o, --output Path to write the resulting welded .csx file to (required)"
printfn " -m, --minified Additionally write a minified copy (<name>.min.txt next to the output)"
printfn " -h, --help Show this help message and exit"
type ParsedArgs =
{ Files: string list
Output: string option
Minified: bool
Help: bool }
let rec parseArgs (args: string list) (acc: ParsedArgs) =
match args with
| [] -> { acc with Files = List.rev acc.Files }
| ("-o" | "--output") :: value :: rest -> parseArgs rest { acc with Output = Some value }
| ("-m" | "--minified") :: rest -> parseArgs rest { acc with Minified = true }
| ("-h" | "--help") :: rest -> parseArgs rest { acc with Help = true }
| token :: rest when token.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) ->
parseArgs rest { acc with Files = token :: acc.Files }
| token :: rest ->
eprintfn "Warning: ignoring unrecognized argument '%s'" token
parseArgs rest acc
let scriptArgs = fsi.CommandLineArgs |> List.ofArray
let parsed =
parseArgs
scriptArgs
{ Files = []
Output = None
Minified = false
Help = false }
if parsed.Help then
printUsage ()
exit 0
if parsed.Files.IsEmpty then
eprintfn "No input .cs files were provided."
printUsage ()
exit 1
let outputPath =
match parsed.Output with
| Some path -> path
| None ->
eprintfn "Missing required -o/--output argument."
printUsage ()
exit 1
// Using-directive validation: the welded script can't contain using
// directives (except SFDGameScriptInterface which is already implicit).
let collectForbiddenUsings (filePath: string) : (string * string) list =
if not (File.Exists filePath) then
[]
else
let tree = CSharpSyntaxTree.ParseText(File.ReadAllText filePath)
let root = tree.GetRoot()
// Only check usings in files that contribute to the welded output.
let hasGameScript =
root.DescendantNodes()
|> Seq.exists (fun n ->
match n with
| :? ClassDeclarationSyntax as c when c.Identifier.ValueText = GameScriptClassName -> true
| _ -> false)
if not hasGameScript then [] else
root.DescendantNodes()
|> Seq.choose (fun n ->
match n with
| :? UsingDirectiveSyntax as u ->
let name = u.Name.ToFullString().Trim()
let label = u.ToString().TrimEnd(';', '\n', '\r').Trim()
let isGlobal = u.GlobalKeyword.IsKind SyntaxKind.GlobalKeyword
let isStatic = u.StaticKeyword.IsKind SyntaxKind.StaticKeyword
let hasAlias = not (isNull u.Alias)
let isAllowed = not isGlobal && not isStatic && not hasAlias && name = "SFDGameScriptInterface"
if isAllowed then None else Some(label, filePath)
| _ -> None)
|> Seq.toList
let forbiddenUsings = parsed.Files |> List.collect collectForbiddenUsings
if not forbiddenUsings.IsEmpty then
eprintfn "The following 'using' directives are not allowed in welded scripts;"
eprintfn "replace them with fully-qualified references (e.g. System.Collections.Generic.Dictionary)."
for label, file in forbiddenUsings do
eprintfn " %s: %s;" (Path.GetFileName file) label
eprintfn ""
eprintfn "'using SFDGameScriptInterface;' is the only allowed using (implicit in the welded output)."
exit 1
// Formatting helper: dedent by one indentation level. After formatting a
// member block that Roslyn believes lives one level inside a class, every
// line carries that class's indentation. Since the final welded output is
// no longer nested in any class, we strip that common leading whitespace
// back off so the code sits flush with our own header comments.
let dedentOneLevel (text: string) : string =
let lines = text.Replace("\r\n", "\n").Split '\n'
let nonBlankLines = lines |> Array.filter (fun l -> l.Trim() <> "")
if nonBlankLines.Length = 0 then
text
else
let minIndent =
nonBlankLines
|> Array.map (fun l -> l.Length - l.TrimStart(' ').Length)
|> Array.min
if minIndent = 0 then
lines |> String.concat "\n"
else
lines
|> Array.map (fun l ->
if l.Trim() = "" then ""
elif l.Length >= minIndent then l.Substring minIndent
else l.TrimStart ' ')
|> String.concat "\n"
let workspace = new AdhocWorkspace()
/// Wraps a raw member block in a throwaway class purely so Roslyn has valid
/// syntax to parse, runs the Formatter over it to fix up whitespace, unwraps
/// the formatted members back out, then dedents them by the one level of
/// indentation the wrapper class introduced.
let formatMemberBlock (rawBody: string) : string =
let wrapperSource = sprintf "class %s\n{\n%s\n}" wrapperClassName rawBody
let wrapperTree = CSharpSyntaxTree.ParseText wrapperSource
let wrapperRoot = wrapperTree.GetRoot()
let formattedRoot = Formatter.Format(wrapperRoot, workspace)
let formattedWrapperClass =
formattedRoot.DescendantNodes()
|> Seq.tryPick (fun n ->
match n with
| :? ClassDeclarationSyntax as c when c.Identifier.ValueText = wrapperClassName -> Some c
| _ -> None)
match formattedWrapperClass with
| None ->
eprintfn "Warning: formatting step failed unexpectedly for one file; using unformatted output."
rawBody
| Some c ->
c.Members
|> Seq.map (fun m -> m.ToFullString())
|> String.concat ""
|> dedentOneLevel
/// Either an individual token or an interpolated string kept byte-for-byte.
type private MinSegment =
| MinTok of SyntaxToken
| MinChunk of string
/// Re-emits a member block from its raw token stream with all trivia
/// (comments, whitespace) removed. Every candidate is verified by re-parsing
/// and comparing token texts, so the result is guaranteed to lex identically
/// to the input. Interpolated strings ($"...") are kept as verbatim chunks,
/// since Roslyn splits them into several tokens and inserting even a single
/// space between those would alter the produced string value.
let minifyMemberBlock (rawBody: string) : string option =
let wrapped s = sprintf "class %s\n{\n%s\n}" wrapperClassName s
try
let root = CSharpSyntaxTree.ParseText(wrapped rawBody).GetRoot()
let wrapperClass =
root.DescendantNodes()
|> Seq.tryPick (fun n ->
match n with
| :? ClassDeclarationSyntax as c when c.Identifier.ValueText = wrapperClassName -> Some c
| _ -> None)
match wrapperClass with
| None ->
eprintfn "Warning: minification step failed unexpectedly."
None
| Some c ->
// Token texts of just the members (the wrapper class scaffolding
// is excluded); used to build candidates and to verify them.
let memberTokenTexts (sourceRoot: SyntaxNode) : string array option =
sourceRoot.DescendantNodes()
|> Seq.tryPick (fun n ->
match n with
| :? ClassDeclarationSyntax as cc when cc.Identifier.ValueText = wrapperClassName -> Some cc
| _ -> None)
|> Option.map (fun cc ->
cc.Members
|> Seq.collect (fun m -> m.DescendantTokens())
|> Seq.filter (fun t -> not (t.IsKind SyntaxKind.EndOfFileToken))
|> Seq.map (fun t -> t.Text)
|> Array.ofSeq)
let originalTexts =
match memberTokenTexts root with
| Some texts -> texts
| None -> [||]
let lexesIdentically (candidate: string) =
match memberTokenTexts (CSharpSyntaxTree.ParseText(wrapped candidate).GetRoot()) with
| Some re ->
Array.length re = Array.length originalTexts && Array.forall2 (=) re originalTexts
| None -> false
let segments = ResizeArray<MinSegment>()
for m in c.Members do
let nodeText = m.ToString()
// Outermost interpolated strings only (nested ones are part
// of their parent's chunk).
let outermost =
m.DescendantNodes()
|> Seq.choose (fun n ->
match n with
| :? InterpolatedStringExpressionSyntax as i -> Some i.Span
| _ -> None)
|> fun spans ->
let parents = System.Collections.Generic.List<Text.TextSpan>()
spans
|> Seq.sortByDescending (fun s -> s.Start, s.Length)
|> Seq.iter (fun s ->
if parents |> Seq.exists (fun p -> p.Contains s) |> not then
parents.Add s)
parents.ToArray()
let mutable skipUntil = -1
for t in m.DescendantTokens() do
if t.IsKind SyntaxKind.EndOfFileToken then ()
elif skipUntil >= 0 && t.Span.End <= skipUntil then ()
else
match outermost |> Array.tryFind (fun s -> s.Contains t.SpanStart) with
| Some s ->
segments.Add(MinChunk(nodeText.Substring(s.Start - m.SpanStart, s.Length)))
skipUntil <- s.End
| None -> segments.Add(MinTok t)
if segments.Count = 0 then
Some ""
else
let render (seg: MinSegment) =
match seg with
| MinTok t -> t.Text
| MinChunk s -> s
// Zero-width tokens (e.g. the omitted array size in 'T[]')
// carry empty text; they are grammar-implied and regenerate on
// reparse, so they must not take part in spacing decisions.
let rendered =
segments
|> Seq.map render
|> Array.ofSeq
|> Array.filter (fun s -> s.Length > 0)
if rendered.Length = 0 then
Some ""
else
let lastChar (s: string) = s.[s.Length - 1]
let firstChar (s: string) = s.[0]
// Attempt 1: jam every segment together.
let tight = String.concat "" rendered
if lexesIdentically tight then
Some tight
else
// Attempt 2: single space only where gluing could merge
// tokens (identifier/keyword boundaries or operator pairs).
let isIdentChar (ch: char) = Char.IsLetterOrDigit ch || ch = '_' || ch = '@'
let isOpChar (ch: char) = "+-<>%&|^!=?:.*/~".IndexOf ch >= 0
let spaced =
rendered
|> Seq.mapi (fun i s ->
if i = 0 then
s
else
let pl = lastChar rendered.[i - 1]
let fc = firstChar s
if isIdentChar pl && isIdentChar fc
|| isOpChar pl && isOpChar fc then
" " + s
else
s)
|> String.concat ""
if lexesIdentically spaced then
Some spaced
else
// Last resort: a space between every segment. Cannot
// corrupt interpolated strings since those stay whole.
Some(String.concat " " rendered)
with ex ->
eprintfn "Warning: minification failed (%s)." ex.Message
None
// Extraction: pull everything inside the GameScript class out of each file,
// then format and dedent it, and finally prepend a flush-left header comment
// naming the source file.
let tryExtractGameScriptBody (filePath: string) : string option =
if not (File.Exists filePath) then
eprintfn "Warning: file not found, skipping: %s" filePath
None
else
let source = File.ReadAllText filePath
let tree = CSharpSyntaxTree.ParseText source
let root = tree.GetRoot()
let classDecl =
root.DescendantNodes()
|> Seq.tryPick (fun n ->
match n with
| :? ClassDeclarationSyntax as c when c.Identifier.ValueText = GameScriptClassName -> Some c
| _ -> None)
match classDecl with
| None ->
eprintfn "Warning: no '%s' class found in %s, skipping." GameScriptClassName filePath
None
| Some c ->
let body = c.Members |> Seq.map (fun m -> m.ToFullString()) |> String.concat ""
Some body
let weldedSections =
parsed.Files
|> List.choose (fun f ->
tryExtractGameScriptBody f
|> Option.map (fun rawBody ->
let formattedBody = (formatMemberBlock rawBody).Trim('\n', '\r')
sprintf "// %s\n%s" (Path.GetFileName f) formattedBody))
if weldedSections.IsEmpty then
eprintfn "None of the provided files contained a '%s' class. Nothing to generate." GameScriptClassName
exit 1
workspace.Dispose()
let autoGeneratedHeader =
[ "// <auto-generated>"
"// This file was generated by SFD.ScriptTools.ScriptGenerator.fsx."
"// Do not modify it by hand as your changes will be lost on regeneration."
"// </auto-generated>" ]
|> String.concat "\n"
let finalCode =
autoGeneratedHeader + "\n\n" + (weldedSections |> String.concat "\n\n")
// Write output
let fullOutputPath = Path.GetFullPath outputPath
let outputDir = Path.GetDirectoryName fullOutputPath
if not (String.IsNullOrEmpty outputDir) && not (Directory.Exists outputDir) then
Directory.CreateDirectory outputDir |> ignore
File.WriteAllText(fullOutputPath, finalCode.Trim() + Environment.NewLine)
printfn "Wrote welded script (%d file(s)) to: %s" weldedSections.Length fullOutputPath
if parsed.Minified then
let body = weldedSections |> String.concat "\n\n"
match minifyMemberBlock body with
| None ->
eprintfn "Failed to produce minified output."
exit 1
| Some minified ->
let minifiedPath =
if Path.HasExtension fullOutputPath then
Path.ChangeExtension(fullOutputPath, ".min.txt")
else
fullOutputPath + ".min.txt"
File.WriteAllText(minifiedPath, minified.Trim() + Environment.NewLine)
printfn "Wrote minified script (%d -> %d chars) to: %s" body.Length minified.Length minifiedPath