-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkresource.sml
57 lines (54 loc) · 1.44 KB
/
mkresource.sml
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
fun readEscapeAndWrite (ins, outs) =
let
val chunk = BinIO.input ins
val escape = String.toString o Byte.bytesToString
in
if Word8Vector.length chunk = 0 then ()
else
(TextIO.output (outs, escape chunk);
readEscapeAndWrite (ins, outs))
end
fun writeResourceEntry (path, outs) =
let
fun p s = TextIO.output (outs, s)
val ins = BinIO.openIn path
in
p " (\"";
p (String.toString path);
p "\", \"";
readEscapeAndWrite (ins, outs);
p "\"),\n";
BinIO.closeIn ins
end
fun writeStructure (str, paths, outs) =
let
fun p s = TextIO.output (outs, s)
in
p "structure ";
p str;
p " = struct\n";
p " val resources = [\n";
List.app (fn path => writeResourceEntry (path, outs)) paths;
p " (\"\", \"\") ]\n";
p " fun get path =\n";
p " let fun get' [] = raise Empty\n";
p " | get' ((path', datum)::resources) =\n";
p " if path = path' then datum\n";
p " else get' resources\n";
p " in\n";
p " get' resources\n";
p " end\n";
p "end"
end
fun main () =
let
fun toAbs path = OS.Path.mkAbsolute {path=path, relativeTo=OS.FileSys.getDir ()}
(* 1st argument is structure name *)
(* 2nd argument changes working directory *)
val (str::cwd::paths) = CommandLine.arguments ()
val cwd' = toAbs cwd
val paths' = map (fn path => OS.Path.mkRelative {path=toAbs path, relativeTo=cwd'}) paths
in
OS.FileSys.chDir cwd;
writeStructure (str, paths', TextIO.stdOut)
end