-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.clj
216 lines (191 loc) 路 9.1 KB
/
generator.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(ns epsilon.generator
(:require [puget.printer :refer [pprint]]
[me.raynes.fs :as fs]
[taoensso.timbre :as log]
[epsilon.utility :refer :all]
[medley.core :as m]
[clojure.java.io :as io]
[clojure.string :as string])
(:import [org.eclipse.epsilon.egl EgxModule]
[org.eclipse.epsilon.emc.plainxml PlainXmlModel]
[epsilon CustomEglFileGeneratingTemplateFactory DirectoryWatchingUtility]
[org.eclipse.epsilon.evl EvlModule]
[org.eclipse.epsilon.egl.internal EglModule]
[java.net URL]
[java.io File]
[java.nio.file Path]))
(defn path->xml
"Load the model at the path and convert it to PlainXmlModel."
[path]
(doto (new PlainXmlModel)
(.setFile (fs/file path))
(.setName (fs/name path))
(.load)))
(defn ->template-factory
"Return a new template factory that outputs to the given path."
[path ops]
(doto (new CustomEglFileGeneratingTemplateFactory)
(.setOutputRoot path)
(.addOperations ops)))
(defmulti ->epsilon-module
"Given a path, convert it into the appropriate module based on its extension."
(fn [path & _] (fs/extension path)))
(defmethod ->epsilon-module ".egl"
[path]
(let [^File egl-file (case (class path)
File path
Path (.toFile path)
String (fs/file path))
egl-module (doto (new EglModule) (.parse (.toURI egl-file)))
parse-problems (.getParseProblems egl-module)]
(if (empty? parse-problems)
egl-module
(throw (ex-info "Parsed problems found" {:payload parse-problems})))))
(defmethod ->epsilon-module ".egx"
[path output-path]
(let [ops (-> "protected.egl"
io/resource
((fn [^URL url]
(let [egl-module (doto (new EglModule) (.parse (.toURI url)))
parse-problems (.getParseProblems egl-module)]
(if (empty? parse-problems)
egl-module
(throw (ex-info "Parsed problems found" {:payload parse-problems}))))))
.getOperations)
factory (->template-factory output-path ops)
egx-file (fs/file path)
egx-module (doto (new EgxModule factory) (.parse egx-file))
parse-problems (.getParseProblems egx-module)]
(if (empty? parse-problems)
egx-module
(throw (ex-info "Parsed problems found" {:payload parse-problems})))))
(defmethod ->epsilon-module ".evl"
[path]
(let [evl-file (fs/file path)
evl-module (doto (new EvlModule) (.parse evl-file))
parse-problems (.getParseProblems evl-module)]
(if (empty? parse-problems)
evl-module
(throw (ex-info "Parsed problems found" {:payload parse-problems})))))
(defn path->epsilon-files
"Return all files that satisfy any of the given types within the given directory.
Accept eol?, egl?, egx? and evl? or any combinations. Ignore invalid functions."
([path types]
(path->epsilon-files path types true))
([path types recursive?]
(let [types (filter #{eol? egl? egx? evl?} types)
filter-fn #(->> % ((apply juxt types)) ((partial some true?)))]
(if recursive?
(m/join (fs/walk (fn [root _ files] (->> files (filter filter-fn) (map #(fs/file root %)))) path))
(->> path (fs/list-dir) (filter fs/file?) (filter filter-fn))))))
(defn execute
"Execute an EOL file with the given XML models."
([model-paths path & args]
(let [xml-models (doall (map #(path->xml %) model-paths))]
(doto (apply ->epsilon-module path args)
(-> .getContext .getModelRepository .getModels (.addAll xml-models))
(.execute)))))
(defn generate
"Generate an EGX file with the given XML models."
[egx-path model-paths output-path]
(log/info "Generating" egx-path)
(execute model-paths egx-path output-path))
(defn validate
"Validate an EVL file with the given XML models."
[evl-path model-paths]
(let [module (execute model-paths evl-path)
constraints (-> module .getContext .getUnsatisfiedConstraints)]
(if (empty? constraints)
(do
(log/info "No violation found for" evl-path)
module)
(throw (ex-info "Constraints violation were found." {:payload constraints})))))
(defmulti file-change-handler
"Triggered when a file change. Will dispatch according to what type of file just got changed."
(fn [file _ _ _] (fs/extension file)))
(defmethod file-change-handler ".egx"
[file _ model-paths output-path]
(log/info file "changed. Regenerating.")
(handle-exception #(generate file model-paths output-path)))
(defmethod file-change-handler ".egl"
[egl-file _ model-paths output-path]
(log/info egl-file "changed. Regenerating using the accompanying EGX.")
(let [egx-path (replace-ext egl-file "egx")]
(if (fs/exists? egx-path)
(handle-exception #(generate egx-path model-paths output-path))
(log/error "Unable to hot-reload because accompanying" egx-path "file is missing."))))
(defmethod file-change-handler ".evl"
[file _ model-paths _]
(log/info file "changed. Rerun validation.")
(handle-exception #(validate (.getAbsolutePath file) model-paths)))
(defmethod file-change-handler ".eol"
[file _ _ _]
(log/info file "changed. Regenerating.")
;; Nothing yet when EOL is created/modified. One thing we can do is to figure out
;; which modules depend on this EOL, then trigger a hot reload on the leaf modules
;; since doing so will trigger a down-cascade hot-reload on all relevant dependent
;; modules. But meh maybe later.
(log/warn "No EOL support yet."))
(defmethod file-change-handler ".xml"
[file template-dir model-paths output-path]
(log/info "Model" file "changed. Regenerating all.")
(let [egx-files (path->epsilon-files template-dir [egx?])]
(handle-exception (fn [] (doall (map #(generate % model-paths output-path) egx-files))))))
(defn watch
"Watch the given template directory and regenerate if file change is detected.
Takes a bunch of preds that will filter out which file type to listen to.
For example, (watch _ _ _ [egl?]) will listen for EGL files only. Can add more as see fit."
([template-dir model-paths output-path]
(watch template-dir model-paths output-path [egl? egx? evl? eol? xml?]))
([template-dir model-paths output-path preds]
(log/info "Watching for file changes. You can now edit files as needed.")
(let [file-change-handler (fn [f] (file-change-handler f template-dir model-paths output-path))
watcher (DirectoryWatchingUtility/watch (-> template-dir fs/file .toPath)
(fn [f] (true? (some true? ((apply juxt preds) f))))
file-change-handler
file-change-handler
file-change-handler)]
{:future (.watchAsync watcher)
:handler (fn [] (.close watcher))})))
(defn validate-all
"Go through the provided template directory and validate everything.
If watch? is true, return the watcher handler to be called to stop the watcher."
([{:keys [template-dir model-paths watch?]}]
(validate-all template-dir model-paths watch?))
([template-dir model-paths]
(validate-all template-dir model-paths true))
([template-dir model-paths watch?]
(log/info
(->> ["Validation will now begin with the following:"
(str "- Template directory: " template-dir)
(str "- Model paths: " model-paths)]
(string/join \newline)))
(let [evl-files (path->epsilon-files template-dir [evl?])
evl-modules (doall (map #(validate % model-paths) evl-files))]
(if watch?
(watch template-dir model-paths nil [evl? xml?])
evl-modules))))
(defn generate-all
"Go through the provided template directory and generate everything.
If watch? is true, return the watcher handler to be called to stop the watcher."
([{:keys [template-dir model-paths output-path watch?]}]
(generate-all template-dir model-paths output-path watch?))
([template-dir model-paths output-path]
(generate-all template-dir model-paths output-path true))
([template-dir model-paths output-path watch?]
(log/info
(->> ["Generation will now begin with the following:"
(str "- Template directory: " template-dir)
(str "- Model paths: " model-paths)
(str "- Output path: " output-path)]
(string/join \newline)))
(let [_ (validate-all template-dir model-paths false)
egx-files (path->epsilon-files template-dir [egx?])
egx-modules (doall (map #(generate % model-paths output-path) egx-files))]
(if watch?
(watch template-dir model-paths output-path [egx? egl? evl? xml?])
egx-modules))))
(comment
(generate "test/resources/templates/generate_test/library.html.egx"
["test/resources/templates/generate_test/library.xml"]
"test/resources/actual"))