From 531f6cb72aa17eea2f8add050f21ed88e2bb1b32 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 25 Oct 2016 19:45:47 -0400 Subject: - Improved the way resources and source code are handled. --- src/lux.clj | 23 +++++++++++++----- src/lux/compiler.clj | 25 ++++++++++++-------- src/lux/packager/program.clj | 55 ++++++++++++++++++++++++-------------------- 3 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/lux.clj b/src/lux.clj index 73e8e9126..a8efa19f7 100644 --- a/src/lux.clj +++ b/src/lux.clj @@ -9,19 +9,30 @@ [lux.compiler.base :as &compiler-base] [lux.compiler :as &compiler] [lux.repl :as &repl] + [clojure.string :as string] :reload-all) (:import (java.io File))) +(def unit-separator (str (char 31))) + +(defn ^:private process-dirs + "(-> Text (List Text))" + [resources-dirs] + (-> resources-dirs + (string/replace unit-separator "\n") + string/split-lines + &/->list)) + (defn -main [& args] (|case (&/->list args) - (&/$Cons "release" (&/$Cons program-module source-dirs)) - (time (&compiler/compile-program &/$Release program-module source-dirs)) + (&/$Cons "release" (&/$Cons program-module (&/$Cons resources-dirs (&/$Cons source-dirs (&/$Nil))))) + (time (&compiler/compile-program &/$Release program-module (process-dirs resources-dirs) (process-dirs source-dirs))) - (&/$Cons "debug" (&/$Cons program-module source-dirs)) - (time (&compiler/compile-program &/$Debug program-module source-dirs)) + (&/$Cons "debug" (&/$Cons program-module (&/$Cons resources-dirs (&/$Cons source-dirs (&/$Nil))))) + (time (&compiler/compile-program &/$Debug program-module (process-dirs resources-dirs) (process-dirs source-dirs))) - (&/$Cons "repl" source-dirs) - (&repl/repl source-dirs) + (&/$Cons "repl" (&/$Cons source-dirs (&/$Nil))) + (&repl/repl (process-dirs source-dirs)) _ (println "Can't understand command."))) diff --git a/src/lux/compiler.clj b/src/lux/compiler.clj index 584cc88ac..56574f8b1 100644 --- a/src/lux/compiler.clj +++ b/src/lux/compiler.clj @@ -121,13 +121,18 @@ )) )) -(defn init! [] - (&¶llel/setup!) - (reset! !source->last-line {}) - (.mkdirs (java.io.File. &&/output-dir)) - (doto (.getDeclaredMethod java.net.URLClassLoader "addURL" (into-array [java.net.URL])) - (.setAccessible true) - (.invoke (ClassLoader/getSystemClassLoader) (to-array [(-> (new java.io.File "./resources") .toURI .toURL)])))) +(defn init! + "(-> (List Text) Null)" + [resources-dirs] + (do (&¶llel/setup!) + (reset! !source->last-line {}) + (.mkdirs (java.io.File. &&/output-dir)) + (let [class-loader (ClassLoader/getSystemClassLoader) + addURL (doto (.getDeclaredMethod java.net.URLClassLoader "addURL" (into-array [java.net.URL])) + (.setAccessible true))] + (doseq [resources-dir (&/->seq resources-dirs)] + (.invoke addURL class-loader + (to-array [(->> resources-dir (new java.io.File) .toURI .toURL)])))))) (defn eval! [expr] (&/with-eval @@ -257,15 +262,15 @@ )) ))) -(defn compile-program [mode program-module source-dirs] - (do (init!) +(defn compile-program [mode program-module resources-dir source-dirs] + (do (init! resources-dir) (let [m-action (|do [_ (compile-module source-dirs "lux")] (compile-module source-dirs program-module))] (|case (m-action (&/init-state mode)) (&/$Right ?state _) (do (println "Compilation complete!") (&&cache/clean ?state) - (&packager-program/package program-module)) + (&packager-program/package program-module resources-dir)) (&/$Left ?message) (assert false ?message))))) diff --git a/src/lux/packager/program.clj b/src/lux/packager/program.clj index 2afda2674..bbd7da665 100644 --- a/src/lux/packager/program.clj +++ b/src/lux/packager/program.clj @@ -26,15 +26,19 @@ (def ^:private kilobyte 1024) (def ^:private buffer-size (* 10 kilobyte)) -(defn ^:private manifest [^String module] +(def ^:private jar-resources "__LUX_PROGRAM_RESOURCES__/") + +(defn ^:private manifest "(-> Text Manifest)" + [^String module] (doto (new Manifest) (-> .getMainAttributes (doto (.put Attributes$Name/MAIN_CLASS (str (&host/->module-class module) "._")) (.put Attributes$Name/MANIFEST_VERSION "1.0") - (.put Attributes$Name/CLASS_PATH "resources/"))))) + (.put Attributes$Name/CLASS_PATH jar-resources))))) -(defn ^:private write-class! [^String path ^File file ^JarOutputStream out] +(defn ^:private write-class! "(-> Text File JarOutputStream Null)" + [^String path ^File file ^JarOutputStream out] (with-open [in (new BufferedInputStream (new FileInputStream file))] (let [buffer (byte-array buffer-size)] (doto out @@ -48,8 +52,9 @@ )) (let [output-dir-size (inc (.length &&/output-dir))] - (defn ^:private write-module! [^File file ^JarOutputStream out] + (defn ^:private write-module! "(-> File JarOutputStream Null)" + [^File file ^JarOutputStream out] (let [module-name (.substring (.getPath file) output-dir-size) inner-files (.listFiles file) inner-modules (filter #(.isDirectory ^File %) inner-files) @@ -59,24 +64,23 @@ (doseq [$module inner-modules] (write-module! $module out))))) -(let [resources-path "resources"] - (defn ^:private write-resources! [^JarOutputStream out] - "(-> JarOutputStream Null)" - (let [resources-dir (new File resources-path)] - (if (.exists resources-dir) - (doseq [^File res (.listFiles resources-dir)] - (with-open [in (->> res (new FileInputStream) (new BufferedInputStream))] - (let [buffer (byte-array buffer-size)] - (doto out - (.putNextEntry (new JarEntry (str resources-path "/" (.getName res)))) - (-> (.write buffer 0 bytes-read) - (->> (when (not= -1 bytes-read)) - (loop [bytes-read (.read in buffer)]))) - (.flush) - (.closeEntry) - )) - )) - nil)))) +(defn ^:private write-resources! + "(-> JarOutputStream (List Text) Null)" + [^JarOutputStream out resources-dirs] + (doseq [resources-dir (&/->seq resources-dirs) + :let [resources-dir (new File resources-dir)] + :when (.exists resources-dir) + ^File res (.listFiles resources-dir) + :let [buffer (byte-array buffer-size)]] + (with-open [in (->> res (new FileInputStream) (new BufferedInputStream))] + (doto out + (.putNextEntry (new JarEntry (str jar-resources (.getName res)))) + (-> (.write buffer 0 bytes-read) + (->> (when (not= -1 bytes-read)) + (loop [bytes-read (.read in buffer)]))) + (.flush) + (.closeEntry)) + ))) (defn ^:private fetch-available-jars [] (->> ^java.net.URLClassLoader (ClassLoader/getSystemClassLoader) @@ -122,12 +126,13 @@ )))) ;; [Resources] -(defn package [module] - "(-> Text Null)" +(defn package + "(-> Text (List Text) Null)" + [module resources-dirs] (with-open [out (new JarOutputStream (->> &&/output-package (new File) (new FileOutputStream)) (manifest module))] (do (doseq [$group (.listFiles (new File &&/output-dir))] (write-module! $group out)) - (write-resources! out) + (write-resources! out resources-dirs) (->> (fetch-available-jars) (filter #(and (not (.endsWith ^String % "luxc.jar")) (not (.endsWith ^String % "tools.nrepl-0.2.3.jar")) -- cgit v1.2.3