forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneratedFiles.ml
52 lines (47 loc) · 1.55 KB
/
GeneratedFiles.ml
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
type fileAction =
| NoMatch (* No @genType annotation found. *)
| Replace (* Replace existing file on disk with new contents. *)
| Identical (* File already on disk with identical contents. Skip. *)
| TypeError
(* The cmt file was produced after a type error -- don't delete generated files. *)
| Write (* File not present on disk. *)
let logFileAction fileAction fileName =
if !Debug.basic then
Log_.item "%s %s\n"
(match fileAction with
| NoMatch -> "NoMatch"
| Replace -> "Replace"
| Identical -> "Identical"
| TypeError -> "TypeError"
| Write -> "Write")
fileName
let readLines (file : string) : string list =
let lines = ref [] in
let chan = open_in file in
let finished_lines =
try
while true do
lines := input_line chan :: !lines
done;
[]
with End_of_file ->
close_in chan;
!lines |> List.rev
in
finished_lines
let readFile (file : string) : string = String.concat "\n" (readLines file)
let writeFile (filePath : string) (contents : string) =
let outFile = open_out filePath in
output_string outFile contents;
close_out outFile
let writeFileIfRequired ~outputFile ~fileContents =
if Sys.file_exists outputFile then
let oldContents = readFile outputFile in
let identical = oldContents = fileContents in
if identical then outputFile |> logFileAction Identical
else (
outputFile |> logFileAction Replace;
writeFile outputFile fileContents)
else (
outputFile |> logFileAction Write;
writeFile outputFile fileContents)