16
16
17
17
(def ^:dynamic *cljs-files*)
18
18
19
- (defn cljs- files-in
20
- " Return a sequence of all .cljs files in the given directory."
21
- [dir]
19
+ (defn files-in
20
+ " Return a sequence of all files with given extension in the given directory."
21
+ [ext dir]
22
22
(filter #(let [name (.getName ^java.io.File %)]
23
- (and (.endsWith name " .cljs " )
23
+ (and (.endsWith name ( str " . " ext) )
24
24
(not= \. (first name))))
25
25
(file-seq dir)))
26
26
27
+ (def cljs-files-in
28
+ (partial files-in " cljs" ))
29
+
30
+ (defn lib-ns [lib-file]
31
+ (let [first-line (binding [*in* (clojure.java.io/reader lib-file)]
32
+ (read-line ))]
33
+ (if (.startsWith first-line " -- CLJS/LUA" )
34
+ (symbol (nth (.split first-line " " ) 3 )))))
35
+
36
+ (def lib-files-map
37
+ (apply hash-map
38
+ (mapcat (fn [f] [(lib-ns f) f])
39
+ (files-in " cljlib" (io/file (com/common-libs-path ))))))
40
+
27
41
(defn ns-decl [file]
28
42
(let [first-form
29
43
(->> file
41
55
(doseq [form seq]
42
56
(comp/emit (ana/analyze (ana/empty-env ) form))))
43
57
44
- (defn compile-file [file]
45
- (compile-seq (cloader/make-forms-seq file)))
58
+ (defn compile-file [file optmap ]
59
+ (compile-seq (cloader/make-forms-seq file)))
46
60
47
61
(defn get-parent [file]
48
62
(.getParentFile (io/file (.getCanonicalPath file))))
49
63
50
- (defn compile-with-deps [file]
64
+ (defn compile-with-deps [file optmap ]
51
65
(let [nsdecl (ns-decl file)
52
66
requires (nsdecl :requires )]
53
67
(doseq [[ns-alias ns-name] requires]
54
- (compile-with-deps (*cljs-files* ns-name)))
55
- (compile-file file)))
68
+ (if (*cljs-files* ns-name)
69
+ (compile-with-deps (*cljs-files* ns-name) optmap)
70
+ (if (lib-files-map ns-name)
71
+ (println (slurp (lib-files-map ns-name)))
72
+ (throw (Exception. (str " Dependency not found : " ns-name " !" ))))))
73
+ (compile-file file optmap)))
56
74
57
- (defn compile-root-file [file]
75
+ (defn compile-root-file [file { :keys [no-deps] :as optmap} ]
58
76
(binding [*cljs-files* (make-files-map (get-parent file))]
59
- (compile-with-deps file)))
77
+ (compile-with-deps file optmap )))
60
78
61
- (defn -compile [file args ]
79
+ (defn -compile [file { :keys [no-deps as-lib] :as optmap} ]
62
80
(let [nsd (ns-decl file)]
63
81
; ; Adding builtins
64
- (println (slurp (io/resource " builtins.lua" )))
82
+ (if-not no-deps ( println (slurp (io/resource " builtins.lua" ) )))
65
83
; ; Adding core.cljs
66
- (compile-seq com/core-forms-seq)
84
+ (if-not no-deps (compile-seq com/core-forms-seq))
85
+ (if as-lib (println " -- CLJS/LUA " (second (nsd :form ))))
67
86
; ; Compile main file and deps
68
- ((if nsd compile-root-file compile-file) file)))
87
+ ((if (and nsd (not no-deps)) compile-root-file compile-file) file optmap)))
88
+
89
+ (defn remove-dots [s]
90
+ (.replaceAll (str s) " \\ ." " _" ))
91
+
92
+ (defn lib-file-name [src-file {:keys [as-lib]}]
93
+ (let [nsd (ns-decl src-file)
94
+ fname (if nsd
95
+ (remove-dots (comp/munge (second (nsd :form ))))
96
+ (throw (Exception. " No ns decl" )))]
97
+ (str (com/common-libs-path ) com/file-sep fname " .cljlib" )))
69
98
70
- (defn -main [args]
99
+ (defn mk-out-file [src-file {:keys [out-file as-lib] :as optmap}]
100
+ (let [o
101
+ (if out-file out-file
102
+ (if as-lib (lib-file-name src-file optmap) *out*))]
103
+ (println o)
104
+ (io/writer o)))
105
+
106
+ (defn -main [src-file & {:keys [out-file as-lib] :as optmap}]
71
107
(ana/with-core-macros " /cljs/lua/core"
72
108
(binding [ana/*cljs-ns* 'cljs.user
73
109
ana/*cljs-static-fns* true
74
110
comp/*ns-emit-require* false ]
75
- (let [src-file (io/file ( first args) )]
111
+ (let [src-file (io/file src-file )]
76
112
(if (.isDirectory src-file)
77
113
(println " Input must be a cljsc file !" )
78
- (binding [*out* (if ( second args) ( io/writer ( second args)) *out* )]
79
- (-compile src-file args )))))))
114
+ (binding [*out* (mk-out-file src-file optmap )]
115
+ (-compile src-file ( if as-lib ( assoc optmap :no-deps true ) optmap) )))))))
0 commit comments