diff --git a/src/virgil.clj b/src/virgil.clj index 18a56e1..bfe339d 100644 --- a/src/virgil.clj +++ b/src/virgil.clj @@ -2,12 +2,16 @@ (:require [clojure.java.io :as io] [virgil.watch :refer (watch-directory make-idle-callback)] - [virgil.compile :refer (compile-all-java java-file?)])) + [virgil.compile :refer (compile-all-java java-file?)] + virgil.util)) (def watches (atom #{})) (defn compile-java [directories & {:keys [options verbose]}] - (compile-all-java directories options verbose)) + (let [diags (compile-all-java directories options verbose)] + (when (virgil.util/compilation-errored? diags) + (throw (ex-info (format "Compilation failed: %d error(s)." (count diags)) + {:diagnostics diags}))))) (defn watch-and-recompile [directories & {:keys [options verbose post-hook]}] (let [recompile (fn [] diff --git a/src/virgil/compile.clj b/src/virgil/compile.clj index 2c4d936..48da47e 100644 --- a/src/virgil/compile.clj +++ b/src/virgil/compile.clj @@ -151,10 +151,9 @@ (let [collector (DiagnosticCollector.) options (ArrayList. (vec options)) name->source (generate-classname->source directories)] - (println "Compiling" (count name->source)"Java source files in" directories "...") + (println "\nCompiling" (count name->source)"Java source files in" directories "...") (binding [*print-compiled-classes* verbose?] (compile-java options collector name->source)) (when-let [diags (seq (.getDiagnostics collector))] (print-diagnostics diags) - (throw (ex-info (format "Compilation failed: %d error(s)." (count diags)) - {:diagnostics diags})))))) + diags)))) diff --git a/src/virgil/util.clj b/src/virgil/util.clj index 6e01c53..150b6e3 100644 --- a/src/virgil/util.clj +++ b/src/virgil/util.clj @@ -20,11 +20,14 @@ (let [k (.getKind d) log (infer-print-function k)] (if (nil? (.getSource d)) - (println-err (format "%s: %s\n" + (println-err (format "%s: %s" (.toString k) (.getMessage d nil))) - (println-err (format "%s: %s, line %d: %s\n" + (println-err (format "%s: %s, line %d: %s" (.toString k) (.. d getSource getName) (.getLineNumber d) (.getMessage d nil))))))) + +(defn compilation-errored? [diagnostics] + (some #(= (.getKind ^Diagnostic %) Diagnostic$Kind/ERROR) diagnostics)) diff --git a/test/virgil_test.clj b/test/virgil_test.clj index 319c9e0..a36e67e 100644 --- a/test/virgil_test.clj +++ b/test/virgil_test.clj @@ -81,3 +81,17 @@ (cp "B" 'B) (wait-until-true #(= 42 (magic-number))) (is (= 42 (magic-number))))) + +(deftest warnings-shouldnt-throw-test + (binding [*dir* (mk-tmp)] + (cp "ClassWithWarning" 'ClassWithWarning) + (is (nil? (recompile)))) + + (binding [*dir* (mk-tmp)] + (cp "ClassWithError" 'ClassWithError) + (is (thrown? clojure.lang.ExceptionInfo (recompile))))) + +(deftest errors-shouldnt-break-watch-and-recompile-test + (binding [*dir* (mk-tmp)] + (cp "ClassWithError" 'ClassWithError) + (is (nil? (virgil/watch-and-recompile [(str *dir*)])))))