 (ns codewars.runner
   (:require [codewars.runner :refer [run]]
             [clojure.java.io :as io]
-            [codewars.util :as util])
+            [codewars.util :refer [write-code!]])
   (:import [codewars.java TempDir]
            [java.net URLClassLoader]
            [javax.tools ToolProvider]

    (.addListener runner (CwRunListener.))
    (.run runner (into-array [fixture-class]))))

(defmethod solution-only "java"
  [{:keys [:setup :solution]}]
  (let [dir (TempDir/create "java")
        setup (when (not (empty? setup)) (util/write-code! "java" dir setup))
        solution (util/write-code! "java" dir solution)
        files (for [{:keys [:file-name]} [setup solution]
                    :when (not (nil? file-name))]
                file-name)]
    (apply compile! files)
    (-> solution
        :class-name

(defmethod full-project "java"
  [{:keys [:fixture :setup :solution]}]
  (let [dir (TempDir/create "java")
        fixture (write-code! "java" dir fixture)
        setup (when (not (empty? setup)) (write-code! "java" dir setup))
        solution (write-code! "java" dir solution)
        files (file-names [fixture setup solution])]
    (apply compile! files)
    (->> fixture :class-name (load-class dir) run-junit-tests)))
