Skip to content

Commit 3fbf30c

Browse files
committed
Add build target to generate TypeScript bindings
1 parent 1e9aef1 commit 3fbf30c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ Test/bin/
3434
packages
3535
Test/Script.fsx
3636
Test/Script.fsx
37+
typings/FunScript.TypeScript/Output
38+
typings/FunScript.TypeScript/Types.zip
39+
typings/FunScript.TypeScript/*.txt
40+
41+
.DS_Store

build.fsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,68 @@ let translateModules() =
9797
|> String.concat "\n"
9898
System.IO.File.WriteAllText(__SOURCE_DIRECTORY__ @@ fileName, moduleJS)
9999

100+
101+
// --------------------------------------------------------------------------------------
102+
// Generate FunScript bindings from the *.d.ts files in the 'typings' folder
103+
// --------------------------------------------------------------------------------------
104+
105+
open System.IO
106+
open Fake.ProcessHelper
107+
open Fake.ZipHelper
108+
109+
let typings = __SOURCE_DIRECTORY__ @@ "typings"
110+
let typesZip = typings @@ "FunScript.TypeScript" @@ "Types.zip"
111+
let noInline = set [ "jquery.d.ts"; "node.d.ts"; "atom.d.ts" ]
112+
113+
let fsharpBin =
114+
[ "/Library/Frameworks/Mono.framework/Versions/3.10.0/lib/mono/4.0"
115+
(* TODO: This needs to list many more locations where fsc.exe can be? *) ]
116+
|> List.tryFind (fun p -> File.Exists(p @@ "fsc.exe"))
117+
118+
119+
Target "GenerateBindings" (fun () ->
120+
// Clean the 'temp' filder and 'Types.zip' file for FunScript
121+
DeleteFile typesZip
122+
CleanDir (typings @@ "temp")
123+
124+
// Get all definitions and their dependencies (and source code without <reference> tags)
125+
let tsDefinitions =
126+
let reg = System.Text.RegularExpressions.Regex("reference.*path=\"([^\"]*)\"") // "
127+
let defs = Directory.EnumerateFiles(typings, "*.d.ts", SearchOption.AllDirectories)
128+
[ for def in defs ->
129+
let lines = File.ReadAllLines(def)
130+
let deps, lines =
131+
[ for line in lines do
132+
let mtch = reg.Match(line)
133+
if mtch.Success then
134+
let depName = Path.GetFileName(mtch.Groups.[1].Value)
135+
if not (noInline.Contains depName) then yield true, depName
136+
else yield false, line ] |> List.partition fst
137+
Path.GetFileName(def), List.map snd lines :> seq<_>, List.map snd deps ]
138+
139+
// Inline all dependencies, except for those that are in 'noInline' set
140+
let sources = dict [ for fn, src, _ in tsDefinitions -> fn, src ]
141+
for fn, src, deps in tsDefinitions do
142+
if noInline.Contains fn then
143+
let lines =
144+
[| yield! src
145+
for dep in deps do yield! sources.[dep] |]
146+
File.WriteAllLines(typings @@ "temp" @@ fn, lines)
147+
148+
// ZIP everything in the temp folder (inlined definitions)
149+
Directory.EnumerateFiles(typings @@ "temp", "*.d.ts")
150+
|> CreateZip (typings @@ "temp") (typings @@ "FunScript.TypeScript" @@ "Types.zip") "" 1 false
151+
CleanDir (typings @@ "temp")
152+
153+
// Set the FSHARP_BIN variable and run FunScript codegen!
154+
fsharpBin |> Option.iter (fun fsharpBin ->
155+
System.Environment.SetEnvironmentVariable("FSHARP_BIN", fsharpBin))
156+
ProcessHelper.StartProcess (fun p ->
157+
p.FileName <- typings @@ "FunScript.TypeScript" @@ "FunScript.TypeScript.exe"
158+
p.WorkingDirectory <- typings @@ "FunScript.TypeScript"
159+
)
160+
)
161+
100162
// --------------------------------------------------------------------------------------
101163
// Run all targets by default. Invoke 'build <Target>' to override
102164

typings/t.zip

-52.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)