-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fnl
executable file
·72 lines (65 loc) · 1.81 KB
/
build.fnl
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
#! env fennel
(local inspect (require :inspect))
(local stringx (require :pl.stringx))
(fn os-name []
(let [binary-format (: package.cpath
:match
"%p[\\|/]?%p(%a+)")]
(case binary-format
:dll :windows
:so :linux
:dylib :macos)))
(fn read-popen [cmd]
(with-open [in (io.popen cmd)]
(icollect [i v (in:lines)] i)))
(fn get-change-time [file]
(-> (read-popen
(case (os-name)
:macos (.. "stat -f '%m' " file)
:linux (.. "stat -c %Y " file)))
(. 1)
tonumber))
(fn get-src-list []
(let [files
(read-popen "find src -type f ")
src (icollect [_ v (ipairs files)]
(if (not (stringx.startswith v :src/macros))
v))]
src))
(fn get-output-file [file]
(-> file
(stringx.replace "src" "lua")
(stringx.replace "fnl" "lua")))
(fn get-dir [path]
(let [index (stringx.rfind path "/")]
(string.sub path 1 index)))
(fn compile [file]
(print (.. "compiling " file))
(let [tgt (get-output-file file)
tgt-dir (get-dir tgt)]
(os.execute (.. "mkdir -p " tgt-dir))
(os.execute (.. :fennel " "
:--add-macro-path " \"./src/macros/?.fnl\" "
:--compile " " file
">"
tgt))))
(fn not-exits [file]
(let [f (io.open file :r)]
(if f
(do
(io.close f)
false)
true)))
(fn test-should-be-compile [file]
(let [tgt (get-output-file file)]
(or
(not-exits tgt)
(> (get-change-time file)
(get-change-time tgt)))))
(fn run []
(let [files (get-src-list)
need-compile (icollect [_ v (ipairs files)]
(if (test-should-be-compile v) v))]
(each [_ v (ipairs need-compile)]
(compile v))))
(run)