-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerator.xql
325 lines (302 loc) · 11.9 KB
/
generator.xql
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
xquery version "3.1";
(:~
: App generator module
:)
module namespace generator="http://tei-publisher.com/library/generator";
declare namespace repo="http://exist-db.org/xquery/repo";
import module namespace cpy="http://tei-publisher.com/library/generator/copy" at "cpy.xql";
import module namespace inspect="http://exist-db.org/xquery/inspection";
import module namespace config="https://tei-publisher.com/generator/xquery/config" at "config.xql";
import module namespace path="http://tei-publisher.com/jinks/path";
import module namespace tmpl="http://e-editiones.org/xquery/templates";
declare variable $generator:NAMESPACE := "http://tei-publisher.com/library/generator";
declare variable $generator:ERROR_NOT_FOUND := xs:QName("generator:not-found");
declare variable $generator:PROFILES_ROOT := $config:app-root || "/profiles";
(:~
: Generate or update an application using the provided configuration and settings.
:
: In the settings, property "overwrite" controls how files in an existing app are updated:
:
: * "overwrite=all": the entire app is regenerated and then reinstalled
: * "overwrite=update": files will be updated by the potentially newer versions from the profile –
: unless local modifications were applied by the user
:
: @param $settings general settings to control the generator
: @param $config user-supplied configuration, which will overwrite the config.json in the profile
:)
declare function generator:process($settings as map(*)?, $config as map(*)?) {
let $context := generator:prepare($settings, $config)
let $result :=
for $profileName in $context?profiles?*
let $adjContext := map:merge((
$context,
map {
"source": $generator:PROFILES_ROOT || "/" || $profileName,
"_noDeploy": map:contains($config, "profiles") or $settings?dry
}
))
return
generator:write($adjContext, $generator:PROFILES_ROOT || "/" || $profileName, $config)
let $postProcessed :=
for $profileName in $context?profiles?*
return
generator:after-write($context, $result, $generator:PROFILES_ROOT || "/" || $profileName)
let $nextStep :=
if (not(repo:list() = $context?id)) then
map {
"message": "Package is not deployed yet. Deploy it by calling /api/generator/{profile}/deploy" ,
"action": "DEPLOY"
}
else
map {
"message": "Package is already deployed. No action needed",
"action": "NONE"
}
return
map {
"messages": array { $result, $postProcessed },
"config": $context,
"nextStep": $nextStep
}
};
(:~
: Prepare the configuration by merging all profile configurations, then for each profile,
: call the function annotated with "prepare" in the setup.xql, merging in any changes or additions
: returned by that function.
:
: @param $settings general settings to control the generator
: @param $config user-supplied configuration, which will overwrite the config.json in the profile
:)
declare function generator:prepare($settings as map(*), $config as map(*)) {
let $baseConfig := generator:config($settings, $config)
let $mergedConfig :=
fold-right($baseConfig?profiles?*, $baseConfig, function($profile, $config) {
generator:call-prepare($generator:PROFILES_ROOT || "/" || $profile, $config)
})
return
$mergedConfig
};
declare %private function generator:call-prepare($collection as xs:string, $baseConfig as map(*)?) {
let $prepFunc := generator:find-callback($collection, "prepare")
return
if (exists($prepFunc)) then
map:merge(($baseConfig, ($prepFunc?2)($baseConfig)))
else
$baseConfig
};
(:
: Write the updated app to the DB.
: If the app is not yet deployed, or the app needs to be redeployed the dep:deploy function needs to be called afterwards
:)
declare %private function generator:write($context as map(*)?, $collection as xs:string, $appConfig as map(*)) {
let $writeFunc := generator:find-callback($collection, "write")
return
if (exists($writeFunc)) then (
($writeFunc?2)($context),
generator:save-config($context, $appConfig)
) else (
cpy:copy-collection($context),
generator:save-config($context, $appConfig)
)
};
declare function generator:after-write($context as map(*), $result as map(*)*, $collection as xs:string) {
let $func := generator:find-callback($collection, "after-write")
return
if (exists($func)) then
let $targetCollection := if ($context?_update) then path:get-package-target($context?id) else $context?target
let $adjContext := map:merge((
$context,
map {
"base-uri": "http://localhost:" || request:get-server-port() ||
request:get-context-path() || "/apps/" ||
substring-after($targetCollection, repo:get-root()),
"target": $targetCollection
}
))
return
($func?2)($adjContext, $targetCollection)
else
()
};
declare %private function generator:find-callback($collection as xs:string, $type as xs:string) {
let $funcs :=
if (util:binary-doc-available($collection || "/setup.xql")) then
inspect:module-functions(xs:anyURI($collection || "/setup.xql"))
else
()
return
if (exists($funcs)) then
fold-right($funcs, (), function($func, $in) {
if (exists($in)) then
$in
else
let $desc := inspect:inspect-function($func)
let $anno :=
$desc/annotation[@namespace = $generator:NAMESPACE]
[replace($desc/annotation/@name, "^.*?:(.*)$", "$1") = $type]
return
if ($anno) then
[$anno, $func]
else
()
})
else
()
};
declare function generator:find-setup($collection as xs:string) {
if (util:binary-doc-available($collection || "/setup.xql")) then
inspect:module-functions(xs:anyURI($collection || "/setup.xql"))
else
()
};
(:~
: Returns the merged configuration for the given profile.
:)
declare function generator:profile($name as xs:string) as map(*) {
let $collection := $config:app-root || "/profiles/" || $name
return
generator:load-json($collection || "/config.json", map {})
=> generator:extends()
};
(:~
: Assemble the configuration by merging the config.json of the source profile and the one stored in an already installed
: application. $userConfig will overwrite the other two.
:)
declare %private function generator:config($settings as map(*)?, $userConfig as map(*)?) {
let $config := generator:extends($userConfig)
let $installedPkg :=
if ($config?id) then
path:get-package-target(head(($userConfig?id, $config?id)))
else
()
let $tempTarget := $config:temp_directory || "/" || $config?pkg?abbrev
let $config :=
map:merge((
$config,
map {
"target":
if ($settings?overwrite = "all") then
$tempTarget
else
head(($installedPkg, $tempTarget)),
"_update": exists($installedPkg) and $settings?overwrite != "all",
"_overwrite": $settings?overwrite,
"_dry": $settings?dry,
"template-suffix": "\.tpl"
})
)
return
map:merge(($config, map {
"skip": distinct-values(($config?skip?*, "setup.xql", "config.json"))
}))
};
(:~
: Merge the configurations of all inherited profiles. List of inherited profiles
: is written to property "profiles".
:)
declare function generator:extends($config as map(*)) {
generator:extends($config, ())
};
declare %private function generator:extends($config as map(*), $profile as xs:string?) {
if (exists($config?extends)) then
let $extendedProfiles :=
if ($config?extends instance of array(*)) then
$config?extends?*
else
$config?extends
let $extendedConfig :=
for $profile in $extendedProfiles
let $log := util:log("INFO", ("Loading extended profile: " || $profile))
return
generator:load-json($generator:PROFILES_ROOT || "/" || $profile || "/config.json", map {})
=> generator:extends($profile)
return
tmpl:merge-deep((
$extendedConfig,
map {
"profiles": array { $extendedConfig?profiles?*, $profile }
},
map:remove($config, "extends")
))
else
map:merge((
$config,
map {
"profiles": array { $config?profiles?*, $profile }
}
))
};
declare function generator:load-json($path as xs:string, $default as map(*)?) {
if (util:binary-doc-available($path)) then
json-doc($path)
else
$default
};
declare %private function generator:save-config($context as map(*), $appConfig as map(*)) {
if ($context?_dry) then
()
else
let $config := map:merge(
map:for-each($context, function($key, $value) {
if (starts-with($key, "_") or $key = "source" or $key = "target") then
()
else
map:entry($key, $value)
})
)
let $storeConfig := map:merge((
(: map:entry("profiles", $context?profiles), :)
$appConfig
))
return (
xmldb:store($context?target, "config.json", serialize($storeConfig, map { "method": "json", "indent": true()}))[2],
xmldb:store($context?target, "context.json", serialize($context, map { "method": "json", "indent": true()}))[2]
)
};
declare function generator:get-package-descriptor($uri as xs:string?) {
if (not(repo:list()[. = $uri])) then
()
else
repo:get-resource($uri, "expath-pkg.xml")
=> util:binary-to-string()
=> parse-xml()
};
declare %private function generator:load-template-map($collection as xs:string?) {
if ($collection and util:binary-doc-available($collection || "/.jinks.json")) then
json-doc($collection || "/.jinks.json")
else
map {}
};
declare function generator:list-actions($context as map(*)) as array(*) {
array {
let $mcontext := generator:extends($context)
let $actions :=
for $profile in $mcontext?profiles?*
return
generator:find-callback($generator:PROFILES_ROOT || "/" || $profile, "action")
for $action in $actions
group by $name := function-name($action?2) => local-name-from-QName()
return
map {
"name": $name,
"description": $action[1]?1/value/string()
}
}
};
declare function generator:run-action($collection as xs:string, $actionName as xs:string) {
util:log("INFO", "<jinks> Running action " || $actionName),
let $configPath := path:resolve-path($collection, "config.json")
let $config := generator:load-json($configPath, ())
let $context := generator:extends($config)
let $actions :=
for $profile in $context?profiles?*
let $callback := generator:find-callback($generator:PROFILES_ROOT || "/" || $profile, "action")
return
if (exists($callback) and local-name-from-QName(function-name($callback?2)) = $actionName) then
$callback
else
()
for $action in $actions
return
$action?2($context)
};