1
- (ns generator )
2
-
3
- (require
4
- '[clojure.data.json :as json]
5
- '[clojure.java.shell :refer [sh]]
6
- '[clojure.java.io :as io]
7
- '[selmer.parser :refer [render render-file]]
8
- '[toml-clj.core :as toml])
9
-
10
- (defn error [message]
11
- (println message)
12
- (System/exit 1 ))
13
-
14
- (def root-dir (.getCanonicalPath (io/file *file* " .." " .." " .." )))
15
- (def prob-specs-dir (io/file root-dir " .problem-specifications" ))
16
- (defn exercise-dir [slug] (io/file root-dir " exercises" " practice" slug))
17
-
18
- (defn sync-prob-specs []
19
- (if (.isDirectory prob-specs-dir)
20
- (sh " git" " pull" :dir prob-specs-dir)
21
- (sh " git" " clone" " https://github.com/exercism/problem-specifications.git" prob-specs-dir)))
22
-
23
- (defn canonical-data-file [slug] (io/file prob-specs-dir " exercises" slug " canonical-data.json" ))
24
- (defn canonical-data [slug]
25
- (let [file (canonical-data-file slug)]
26
- (if (.exists file)
27
- (json/read (io/reader file) :key-fn keyword)
28
- (error (str " No canonical-data.json found for exercise '" slug " '" )))))
29
-
30
- (defn tests-toml-file [slug] (io/file (exercise-dir slug) " .meta" " tests.toml" ))
31
- (defn excluded-uuids [slug]
32
- (let [file (tests-toml-file slug)]
33
- (if (.exists file)
34
- (->> file
35
- (io/reader )
36
- (toml/read )
37
- (filter #(= false (get (last %) " include" )))
38
- (map first)
39
- (set ))
40
- (error (str " No tests.toml data found for exercise '" slug " '" )))))
41
-
42
- (defn excluded? [slug]
43
- (let [excluded (excluded-uuids slug)]
44
- (fn [node] (contains? excluded (:uuid node)))))
45
-
46
- (defn node->test-case [node path]
47
- (-> node
48
- (assoc :path path :error (get-in node [:expected :error ]))
49
- (dissoc :reimplements :comments :scenarios )))
50
-
51
- (defn test-case-nodes
52
- ([node] (test-case-nodes node []))
53
- ([node path]
54
- (let [description (:description node)
55
- children (:cases node)
56
- updated-path (if description (conj path description) path)]
57
- (if children
58
- (mapcat #(test-case-nodes % updated-path) children)
59
- [(node->test-case node updated-path)]))))
60
-
61
- (defn test-cases [slug]
62
- (->> slug
63
- (canonical-data )
64
- (test-case-nodes )
65
- (remove (excluded? slug))
66
- (into [])))
67
-
68
- (sync-prob-specs )
69
- (test-cases " isogram" )
70
-
71
- (def data {:slug " isogram"
72
- :cases (cases (canonical-data " isogram" ))})
73
-
74
- (def template " /Users/erik/Code/exercism/tracks/clojure/exercises/practice/collatz-conjecture/.meta/tests.template" )
75
- (def tests " /Users/erik/Code/exercism/tracks/clojure/exercises/practice/collatz-conjecture/.meta/tests.clj" )
76
-
77
- (def toml-file " /home/erik/exercism/tracks/clojure/exercises/practice/collatz-conjecture/.meta/tests.toml" )
78
-
79
- (spit tests (render (slurp template) data))
80
- (def t (toml/read (io/reader toml-file)))
81
- (set (map first (filter #(not= false (get (last %) " include" )) t)))
82
-
83
- (.getAbsolutePath (io/file *file* " .." " .." ))
84
- (.getCanonicalPath (io/file *file* " .." " .." " .." ))
1
+ (ns generator
2
+ (:require [canonical-data]
3
+ [templates]
4
+ [log]))
5
+
6
+ (defn- slugs-to-generate [args]
7
+ (let [slugs templates/exercises-with-template]
8
+ (if-let [slug (first args)]
9
+ (if (contains? slugs slug)
10
+ [slug]
11
+ (log/error (str " No template found for exercise '" slug " '" )))
12
+ slugs)))
13
+
14
+ (defn- run [args]
15
+ (canonical-data/sync-repo )
16
+ (doseq [slug (slugs-to-generate args)]
17
+ (log/normal (str " Generating tests for exercise '" slug " '" ))
18
+ (templates/generate-tests-file slug (canonical-data/test-cases slug))))
0 commit comments