Skip to content

Commit 81a0e60

Browse files
committed
Added option to compile file as lib
1 parent 52f6b3d commit 81a0e60

File tree

5 files changed

+75
-24
lines changed

5 files changed

+75
-24
lines changed

bin/cljslua.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
(def commands {"compile" comp/-main
66
"repl" repl/-main})
77

8-
(let [args *command-line-args*
8+
(defn keywordize-args [args]
9+
(for [arg args]
10+
(if (.startsWith arg ":") (keyword (subs arg 1)) arg)))
11+
12+
(let [args (keywordize-args *command-line-args*)
913
cmd-func (commands (first args))]
1014
(conf/load-config)
1115
(if cmd-func
12-
(cmd-func (rest args))
16+
(apply cmd-func (rest args))
1317
(println "Unknown command : " (first args))))

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[org.clojure/data.json "0.1.2"]]
44
:plugins [[lein-swank "1.4.0"]
55
[lein-pprint "1.1.1"]]
6-
:extra-classpath-dirs ["libclojurescript.jar" "test/cljs"]
6+
:extra-classpath-dirs ["libclojurescript.jar"]
77
:resources-path "cljs"
88
:source-path "src"
99
:main cljs.lua.compile

src/cljs/lua/common.clj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,15 @@
2929

3030
(defn new-env
3131
([context] {:ns (@ana/namespaces ana/*cljs-ns*) :context context :locals {}})
32-
([] (new-env :return)))
32+
([] (new-env :return)))
33+
34+
(def file-sep java.io.File/separator)
35+
36+
(defn get-cljs-dir []
37+
(str (System/getProperty "user.home") file-sep ".cljslua"))
38+
39+
(defn common-libs-path []
40+
(str (get-cljs-dir) file-sep "libs"))
41+
42+
(defn init-dirs []
43+
(.mkdirs (io/file (common-libs-path))))

src/cljs/lua/compile.clj

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,28 @@
1616

1717
(def ^:dynamic *cljs-files*)
1818

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]
2222
(filter #(let [name (.getName ^java.io.File %)]
23-
(and (.endsWith name ".cljs")
23+
(and (.endsWith name (str "." ext))
2424
(not= \. (first name))))
2525
(file-seq dir)))
2626

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+
2741
(defn ns-decl [file]
2842
(let [first-form
2943
(->> file
@@ -41,39 +55,61 @@
4155
(doseq [form seq]
4256
(comp/emit (ana/analyze (ana/empty-env) form))))
4357

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)))
4660

4761
(defn get-parent [file]
4862
(.getParentFile (io/file (.getCanonicalPath file))))
4963

50-
(defn compile-with-deps [file]
64+
(defn compile-with-deps [file optmap]
5165
(let [nsdecl (ns-decl file)
5266
requires (nsdecl :requires)]
5367
(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)))
5674

57-
(defn compile-root-file [file]
75+
(defn compile-root-file [file {:keys [no-deps] :as optmap}]
5876
(binding [*cljs-files* (make-files-map (get-parent file))]
59-
(compile-with-deps file)))
77+
(compile-with-deps file optmap)))
6078

61-
(defn -compile [file args]
79+
(defn -compile [file {:keys [no-deps as-lib] :as optmap}]
6280
(let [nsd (ns-decl file)]
6381
;; Adding builtins
64-
(println (slurp (io/resource "builtins.lua")))
82+
(if-not no-deps (println (slurp (io/resource "builtins.lua"))))
6583
;; 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))))
6786
;; 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")))
6998

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}]
71107
(ana/with-core-macros "/cljs/lua/core"
72108
(binding [ana/*cljs-ns* 'cljs.user
73109
ana/*cljs-static-fns* true
74110
comp/*ns-emit-require* false]
75-
(let [src-file (io/file (first args))]
111+
(let [src-file (io/file src-file)]
76112
(if (.isDirectory src-file)
77113
(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))))))))

test/core_test.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,4 +1531,4 @@
15311531
(test-stuff)
15321532
(macro-test/test-macros)
15331533

1534-
(defn a [b c] (+ b c))
1534+
(defn a [b c] (+ b c))

0 commit comments

Comments
 (0)