/
generators.clj
119 lines (107 loc) · 4.15 KB
/
generators.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(ns coast.generators
(:require [word.core :as word]
[coast.db :as db]
[clojure.string :as string]
[clojure.java.io :as io])
(:import (java.io File)))
(def mustache-re #"\{\{([\w-_]+)\}\}")
(defn replacement [match m]
(let [default (first match)
k (-> match last keyword)]
(str (get m k default))))
(defn render [s m]
(string/replace s mustache-re #(replacement % m)))
(defn render-resource [r m]
(-> (io/resource r)
(slurp)
(render m)))
(defn form-col? [s]
(and (not= s "id")
(not= s "created_at")
(not= s "created-at")))
(defn path [& parts]
(string/join "/" parts))
(defn overwrite? [filename]
(if (.exists (io/file filename))
(do
(println filename "already exists. Overwrite? y/n")
(let [input (-> (read-line)
(.toLowerCase))]
(= input "y")))
true))
(defn sql [project table]
(let [params {:project project
:table (string/replace table #"-" "_")}
dir (path "resources" "sql")
filename (path dir (str table ".sql"))
_ (.mkdirs (File. dir))]
(if (overwrite? filename)
(do
(spit filename (render-resource "crud.sql" params))
(println table "sql generated"))
(println table "sql skipped"))))
(defn model [project table]
(let [params {:project project
:ns (string/replace project #"_" "-")
:table (string/replace table #"_" "-")
:columns (->> (db/get-cols table)
(map :column_name)
(filter form-col?)
(map #(str ":" %))
(string/join " "))}
dir (path "src" project "models")
filename (path dir (str table ".clj"))
_ (.mkdirs (File. dir))]
(sql project table)
(if (overwrite? filename)
(do
(spit filename (render-resource "model.clj" params))
(println table "model generated"))
(println table "model skipped"))))
(defn controller [project table]
(let [params {:project project
:ns (string/replace project #"_" "-")
:table (string/replace table #"_" "-")
:singular (word/singular table)}
dir (path "src" project "controllers")
filename (path dir (str table "_controller.clj"))
_ (.mkdirs (File. dir))]
(if (overwrite? filename)
(do
(spit filename (render-resource "controller.clj" params))
(println table "controller generated"))
(println table "controller skipped"))))
(defn view [project table]
(let [cols (->> (db/get-cols table)
(map :column_name)
(map #(string/replace % "_" "-")))
form-cols (filter form-col? cols)
params {:project project
:ns (string/replace project #"_" "-")
:table (string/replace table #"_" "-")
:singular (word/singular table)
:columns cols
:form_columns form-cols
:column_string (string/join " " cols)
:form_column_string (string/join " " form-cols)
:th_col_string (->> (map #(str "[:th \"" % "\"]") cols)
(string/join "\n "))
:td_col_string (->> (map #(str "[:td " % "]") cols)
(string/join "\n "))
:div_col_string (->> (map #(str "[:div " % "]") cols)
(string/join "\n "))
:form_col_string (->> (map #(format "[:div\n [:label \"%s\"]\n [:input {:type \"text\" :name \"%s\" :value %s}]]" % % %) form-cols)
(string/join "\n "))}
dir (str "src/" project "/views")
filename (str dir "/" table ".clj")
_ (.mkdirs (File. dir))]
(if (overwrite? filename)
(do
(spit filename (render-resource "view.clj" params))
(println table "view generated"))
(println table "view skipped"))))
(defn mvc [project table]
(do
(model project table)
(view project table)
(controller project table)))