@@ -2,6 +2,61 @@ let version = Version.version
2
2
3
3
type cliCommand = Add of string | Clean | NoOp | Rm of string list
4
4
5
+ let executeCliCommand ~printUsageAndExit cliCommand =
6
+ match cliCommand with
7
+ | Add s ->
8
+ Log_.Color. forceColor := true ;
9
+ let splitColon = Str. split (Str. regexp " :" ) s in
10
+ let cmt, mlast =
11
+ match splitColon with
12
+ | cmt :: rest ->
13
+ let mlast = rest |> String. concat " " in
14
+ (cmt, mlast)
15
+ | _ -> assert false
16
+ in
17
+ let config = Paths. readConfig ~namespace: (cmt |> Paths. findNameSpace) in
18
+ if ! Debug. basic then Log_. item " Add %s %s\n " cmt mlast;
19
+ cmt |> GenTypeMain. processCmtFile ~config ;
20
+ exit 0
21
+ | Clean ->
22
+ let config = Paths. readConfig ~namespace: None in
23
+ let sourceDirs = ModuleResolver. readSourceDirs ~config in
24
+ if ! Debug. basic then
25
+ Log_. item " Clean %d dirs\n " (sourceDirs.dirs |> List. length);
26
+ let count = ref 0 in
27
+ sourceDirs.dirs
28
+ |> List. iter (fun dir ->
29
+ let files = Sys. readdir dir in
30
+ files
31
+ |> Array. iter (fun file ->
32
+ if
33
+ Filename. check_suffix file " .re"
34
+ || Filename. check_suffix file " .res"
35
+ then
36
+ let extension = EmitType. outputFileSuffix ~config in
37
+ let generated =
38
+ Filename. concat dir
39
+ ((file |> Filename. chop_extension) ^ extension)
40
+ in
41
+ if Sys. file_exists generated then (
42
+ Unix. unlink generated;
43
+ incr count)));
44
+ if ! Debug. basic then Log_. item " Cleaned %d files\n " ! count;
45
+ exit 0
46
+ | NoOp -> printUsageAndExit ()
47
+ | Rm l ->
48
+ let removeOne s =
49
+ let cmtAbsolutePath = s in
50
+ (* somehow the CMT hook is passing an absolute path here *)
51
+ let cmt = cmtAbsolutePath |> Paths. relativePathFromBsLib in
52
+ let config = Paths. readConfig ~namespace: (cmt |> Paths. findNameSpace) in
53
+ let outputFile = cmt |> Paths. getOutputFile ~config in
54
+ if ! Debug. basic then Log_. item " Remove %s\n " cmt;
55
+ if Sys. file_exists outputFile then Unix. unlink outputFile
56
+ in
57
+ l |> List. rev |> List. iter removeOne;
58
+ exit 0
59
+
5
60
let cli () =
6
61
let cliCommand = ref NoOp in
7
62
let usage = " genType version " ^ version in
@@ -29,70 +84,10 @@ let cli () =
29
84
(" --version" , Arg. Unit versionAndExit, " show version information and exit" );
30
85
]
31
86
in
32
- let executeCliCommand cliCommand =
33
- match cliCommand with
34
- | Add s ->
35
- Log_.Color. forceColor := true ;
36
- let splitColon = Str. split (Str. regexp " :" ) s in
37
- let cmt, mlast =
38
- match splitColon with
39
- | cmt :: rest ->
40
- let mlast = rest |> String. concat " " in
41
- (cmt, mlast)
42
- | _ -> assert false
43
- in
44
- let config = Paths. readConfig ~namespace: (cmt |> Paths. findNameSpace) in
45
- if ! Debug. basic then Log_. item " Add %s %s\n " cmt mlast;
46
- cmt |> GenTypeMain. processCmtFile ~config ;
47
- exit 0
48
- | Clean ->
49
- let config = Paths. readConfig ~namespace: None in
50
- let sourceDirs = ModuleResolver. readSourceDirs ~config in
51
- if ! Debug. basic then
52
- Log_. item " Clean %d dirs\n " (sourceDirs.dirs |> List. length);
53
- let count = ref 0 in
54
- sourceDirs.dirs
55
- |> List. iter (fun dir ->
56
- let files = Sys. readdir dir in
57
- files
58
- |> Array. iter (fun file ->
59
- if
60
- Filename. check_suffix file " .re"
61
- || Filename. check_suffix file " .res"
62
- then
63
- let extension = EmitType. outputFileSuffix ~config in
64
- let generated =
65
- Filename. concat dir
66
- ((file |> Filename. chop_extension) ^ extension)
67
- in
68
- if Sys. file_exists generated then (
69
- Unix. unlink generated;
70
- incr count)));
71
- if ! Debug. basic then Log_. item " Cleaned %d files\n " ! count;
72
- exit 0
73
- | NoOp -> printUsageAndExit ()
74
- | Rm l ->
75
- let removeOne s =
76
- let cmtAbsolutePath = s in
77
- (* somehow the CMT hook is passing an absolute path here *)
78
- let cmt = cmtAbsolutePath |> Paths. relativePathFromBsLib in
79
- let config =
80
- Paths. readConfig ~namespace: (cmt |> Paths. findNameSpace)
81
- in
82
- let outputFile = cmt |> Paths. getOutputFile ~config in
83
- if ! Debug. basic then Log_. item " Remove %s\n " cmt;
84
- if Sys. file_exists outputFile then Unix. unlink outputFile
85
- in
86
- l |> List. rev |> List. iter removeOne;
87
- exit 0
88
- in
89
87
let anonArg s =
90
88
match ! cliCommand with
91
89
| Rm l -> cliCommand := Rm (s :: l)
92
90
| _ -> print_endline s
93
91
in
94
92
Arg. parse speclist anonArg usage;
95
- executeCliCommand ! cliCommand
96
- ;;
97
-
98
- cli ()
93
+ executeCliCommand ~print UsageAndExit ! cliCommand
0 commit comments