From c2c9a15cea9c7579d470281a16183c820f913dda Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Fri, 29 Apr 2011 23:12:28 -0500 Subject: [PATCH] Split freshener and koan ordering out into namespaces. --- src/path_to_answer_sheet.clj | 6 ++- src/path_to_enlightenment.clj | 74 +---------------------------------- src/runner/freshness.clj | 34 ++++++++++++++++ src/runner/koans.clj | 45 +++++++++++++++++++++ 4 files changed, 85 insertions(+), 74 deletions(-) create mode 100644 src/runner/freshness.clj create mode 100644 src/runner/koans.clj diff --git a/src/path_to_answer_sheet.clj b/src/path_to_answer_sheet.clj index c1ed4b4..8b2126e 100644 --- a/src/path_to_answer_sheet.clj +++ b/src/path_to_answer_sheet.clj @@ -1,6 +1,8 @@ (ns path-to-answer-sheet - (:use [path-to-enlightenment :exclude (run)] - [clojure.string :only (join split trim)])) + (:use [runner.koans :only [ordered-koans]] + [path-to-enlightenment :only [meditations __ ___]] + [clojure.string :only [join split trim]] + [clojure.test :only [*test-out*]])) (def answers {"equalities" {"__" [true diff --git a/src/path_to_enlightenment.clj b/src/path_to_enlightenment.clj index a4d0a88..bc68f89 100644 --- a/src/path_to_enlightenment.clj +++ b/src/path_to_enlightenment.clj @@ -1,11 +1,8 @@ (ns path-to-enlightenment - (:use [fresh.core :only [clj-files-in freshener]] - [clojure.java.io :only [file]]) + (:use [runner.freshness :only [setup-freshener]]) (:require [clojure.set] [clojure.string] - [clojure.test]) - (:import - [java.util.concurrent ScheduledThreadPoolExecutor TimeUnit])) + [clojure.test])) (def __ :fill-in-the-blank) (def ___ (fn [& args] __)) @@ -19,26 +16,6 @@ pairs)] `(and ~@tests))) -(def ordered-koans - ["equalities" - "lists" - "vectors" - "sets" - "maps" - "functions" - "conditionals" - "higher_order_functions" - "runtime_polymorphism" - "lazy_sequences" - "sequence_comprehensions" - "creating_functions" - "recursion" - "destructuring" - "refs" - "atoms" - "macros" - "datatypes"]) - (defn require-version [[required-major required-minor]] (let [{:keys [major minor]} *clojure-version*] (if (or (< major required-major) @@ -60,54 +37,7 @@ (map read-string (take 3 (clojure.string/split version-string #"[\.\-]"))))) -(defn files-to-keep-fresh [] - (clj-files-in (file "src" "koans"))) - -(defn- tests-pass? [file-path] - (use 'path-to-enlightenment) - (load-file file-path)) - -(defn- among-paths? [files] - (into #{} (map #(.getCanonicalPath %) files))) - -(defn- ordered-koan-paths [] - (map (fn [koan-name] - (.getCanonicalPath (file "src" "koans" (str koan-name ".clj")))) - ordered-koans)) - -(defn- next-koan-path [last-koan-path] - (loop [[this-koan & more :as koan-paths] (ordered-koan-paths)] - (when (seq more) - (if (= last-koan-path this-koan) - (first more) - (recur more))))) - -(defn namaste [] - (println "\nYou have achieved clojure enlightenment. Namaste.")) - -(defn report-refresh [report] - (when-let [refreshed-files (seq (:reloaded report))] - (let [these-koans (filter - (among-paths? refreshed-files) - (ordered-koan-paths))] - (println "Refreshing:" these-koans) - (when (every? tests-pass? these-koans) - (if-let [next-koan-file (file (next-koan-path (last these-koans)))] - (report-refresh {:reloaded [next-koan-file]}) - (namaste)))) - (println)) - :refreshed) - -(def refresh! (freshener files-to-keep-fresh report-refresh)) - -(def scheduler (ScheduledThreadPoolExecutor. 1)) - -(defn setup-freshener [] - (.scheduleWithFixedDelay scheduler refresh! 0 500 TimeUnit/MILLISECONDS) - (.awaitTermination scheduler Long/MAX_VALUE TimeUnit/SECONDS)) - (defn run [] (require-version (parse-required-version)) (setup-freshener)) - diff --git a/src/runner/freshness.clj b/src/runner/freshness.clj new file mode 100644 index 0000000..3a4b9a5 --- /dev/null +++ b/src/runner/freshness.clj @@ -0,0 +1,34 @@ +(ns runner.freshness + (:use [fresh.core :only [clj-files-in freshener]] + [clojure.java.io :only [file]] + [runner.koans :only [among-paths? + namaste + next-koan-path + ordered-koans + ordered-koan-paths + tests-pass?]]) + (:import [java.util.concurrent ScheduledThreadPoolExecutor TimeUnit])) + +(defn files-to-keep-fresh [] + (clj-files-in (file "src" "koans"))) + +(defn report-refresh [report] + (when-let [refreshed-files (seq (:reloaded report))] + (let [these-koans (filter + (among-paths? refreshed-files) + (ordered-koan-paths))] + (println "Refreshing:" these-koans) + (when (every? tests-pass? these-koans) + (if-let [next-koan-file (file (next-koan-path (last these-koans)))] + (report-refresh {:reloaded [next-koan-file]}) + (namaste)))) + (println)) + :refreshed) + +(def refresh! (freshener files-to-keep-fresh report-refresh)) + +(def scheduler (ScheduledThreadPoolExecutor. 1)) + +(defn setup-freshener [] + (.scheduleWithFixedDelay scheduler refresh! 0 500 TimeUnit/MILLISECONDS) + (.awaitTermination scheduler Long/MAX_VALUE TimeUnit/SECONDS)) diff --git a/src/runner/koans.clj b/src/runner/koans.clj new file mode 100644 index 0000000..dce51da --- /dev/null +++ b/src/runner/koans.clj @@ -0,0 +1,45 @@ +(ns runner.koans + (:use [clojure.java.io :only [file]])) + +(def ordered-koans + ["equalities" + "lists" + "vectors" + "sets" + "maps" + "functions" + "conditionals" + "higher_order_functions" + "runtime_polymorphism" + "lazy_sequences" + "sequence_comprehensions" + "creating_functions" + "recursion" + "destructuring" + "refs" + "atoms" + "macros" + "datatypes"]) + +(defn ordered-koan-paths [] + (map (fn [koan-name] + (.getCanonicalPath (file "src" "koans" (str koan-name ".clj")))) + ordered-koans)) + +(defn among-paths? [files] + (into #{} (map #(.getCanonicalPath %) files))) + +(defn next-koan-path [last-koan-path] + (loop [[this-koan & more :as koan-paths] (ordered-koan-paths)] + (when (seq more) + (if (= last-koan-path this-koan) + (first more) + (recur more))))) + +(defn tests-pass? [file-path] + (use '[path-to-enlightenment :only [meditations __ ___]]) + (load-file file-path)) + +(defn namaste [] + (println "\nYou have achieved clojure enlightenment. Namaste.")) +