|
15 | 15 | [clariform.ast.parser :as parser]
|
16 | 16 | [clariform.format :as format
|
17 | 17 | :refer [format-code]]
|
| 18 | + [cljs-node-io.core :as node-io] |
| 19 | + [cljs-node-io.file :as node-file] |
18 | 20 | [clariform.io :as io]
|
19 | 21 | [clariform.transform :as transform
|
20 | 22 | :refer [transform]]))
|
21 | 23 |
|
22 |
| -(def version-string "0.5.2") |
| 24 | +(def version-string "0.6.0") |
23 | 25 |
|
24 | 26 | (defn printerr [& vals]
|
25 | 27 | (binding [*print-fn* *print-err-fn*]
|
|
51 | 53 | (exit 1 (pr-str err))))))
|
52 | 54 | (recur))))))
|
53 | 55 |
|
54 |
| -(defn format-all [{:keys [arguments options summary errors] :as opts}] |
55 |
| - (let [resources (io/resources-seq arguments) |
| 56 | +(defn format-all [{:keys [arguments options summary errors] :as opts} & {:keys [output-dir]}] |
| 57 | + (let [resources (doall (io/resources-seq arguments)) |
56 | 58 | multiple (some? (next resources))
|
57 | 59 | con-chan (io/contracts-chan resources)]
|
58 | 60 | (go-loop [{:keys [locator error text] :as res} (<! con-chan)]
|
59 |
| - (binding [*out* (pprint/get-pretty-writer *out*)] |
60 |
| - (when (some? res) |
61 |
| - (when error |
62 |
| - (printerr (ex-message error)) |
63 |
| - (recur (<! con-chan))) |
64 |
| - (when multiple |
65 |
| - (pprint/fresh-line) |
66 |
| - (println ";;" (io/file-path locator))) |
| 61 | + (when (some? res) |
| 62 | + (when (some? error) |
| 63 | + (printerr (ex-message error)) |
| 64 | + (recur (<! con-chan))) |
| 65 | + (when multiple |
| 66 | + (println "") ;; pprint/fresh-line fails! |
| 67 | + (when (some? locator) |
| 68 | + (println ";; " (.toString locator)))) |
| 69 | + (binding [*out* (pprint/get-pretty-writer *out*)] |
67 | 70 | (let [parser-options (select-keys options [:strict])
|
68 | 71 | ast (format/parse-code text parser-options)]
|
69 | 72 | (if (insta/failure? ast)
|
|
82 | 85 | (try
|
83 | 86 | (let [formatted (-> (transform ast)
|
84 | 87 | (format-code options))]
|
85 |
| - (print formatted)) |
| 88 | + ;_ (node-io/spit "contracts-out/file61-locator.clar" (pr-str locator)) |
| 89 | + ;dir (if true output-dir #_(node-io/as-file output-dir)) |
| 90 | + ;_ (node-io/spit "contracts-out/file62-dir.clar" (pr-str dir)) |
| 91 | + ;filepath (case :prod |
| 92 | + ; :prod (node-io/filepath (str output-dir) #_"contracts-out" (str locator) #_"contracts/basic.clar") |
| 93 | + ; :test "contracts-out/contracts/basic.clar")] |
| 94 | + ;(node-io/spit "contracts-out/file70-filepath.clar" (pr-str filepath)) |
| 95 | + ; (apply node-io/make-parents (string/split filepath #"/")) |
| 96 | + (if-some [path (and (some? output-dir) |
| 97 | + (node-io/filepath (str output-dir) (str locator)))] |
| 98 | + (do #_(node-io/spit "contracts-out/filexx.clar" (pr-str path)) |
| 99 | + (apply node-io/make-parents (string/split path #"/")) |
| 100 | + (node-io/spit path formatted)) |
| 101 | + (print formatted))) |
86 | 102 | (catch ExceptionInfo e
|
87 | 103 | (printerr (ex-message e))))))
|
88 | 104 | (pprint/fresh-line)
|
|
99 | 115 | :validate [#{"retain" "adjust" "indent" "auto" "align" "tight" "spread" "compact"}
|
100 | 116 | "Must be one of 'retain', 'adjust', 'indent', 'auto', 'align', 'tight', 'spread' or 'compact'"]]
|
101 | 117 | [nil "--strict" "Expect strict Clarity syntax"]
|
| 118 | + ["-o" "--output-dir DIR" "Output directory"] |
102 | 119 | [nil "--verbose"]
|
103 | 120 | [nil "--debug"]])
|
104 | 121 |
|
|
120 | 137 | (some? (:check options))
|
121 | 138 | (check-all params)
|
122 | 139 | :else
|
123 |
| - (format-all params))) |
| 140 | + (format-all params :output-dir (:output-dir options)))) |
124 | 141 |
|
125 | 142 | (defonce command (atom nil))
|
126 | 143 |
|
|
0 commit comments