-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresources-generator.lisp
executable file
·82 lines (66 loc) · 3.1 KB
/
resources-generator.lisp
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
(in-package :resources-generator)
(defun generate-resources ()
"Generate a string with a list of eg commands"
(format t "~{~A~%~}"
(mapcar #'generate-resource (all-resources))))
(defun generate-dispatchers ()
"Generate a string with a list of dispatcher rules"
(format t "~{~A~^ ~%~}"
(mapcar #'generate-dispatcher (all-resources))))
(defun all-resources ()
"Fetch all resources from domain.lisp"
(loop for val being
the hash-values of mu-cl-resources::*resources*
collect val))
(defun generate-resource (resource)
"Generate a string with an eg command for one resource"
(format nil "~A ~A ~{~A ~}~:{~A:belongs-to:~A~#[~;~~~A~] ~}~:{~A:has-many:~A~#[~;~~~A~] ~} ~[~;--readonly~]"
"ember g mu-resource" ; terminal command
(gen-resource-name resource) ; resource name
(mapcar #'gen-resource-slot (mu-cl-resources::ld-properties resource)) ; its attributes
(mapcar #'(lambda (l) (gen-resource-rel l resource)) (mu-cl-resources::has-one-links resource)) ; its belongs-to relations
(mapcar #'(lambda (l) (gen-resource-rel l resource)) (mu-cl-resources::has-many-links resource)) ; its has-many relations
(readonly) ; sets readonly flag for ever eg command if readonly env is set
))
(defun gen-resource-name (resource)
(string-downcase (mu-cl-resources::resource-name resource)))
(defun gen-resource-slot (property)
(format nil "~A:~A"
(string-downcase (symbol-name (mu-cl-resources::json-key property)))
(string-downcase (symbol-name (mu-cl-resources::resource-type property)))))
(defun gen-resource-rel (link resource)
(let ((i (find-inverse link resource)))
(if i
`(,(mu-cl-resources::request-path link) ,(string-downcase (mu-cl-resources::resource-name link)) ,i)
`(,(mu-cl-resources::request-path link) ,(string-downcase (mu-cl-resources::resource-name link)))
)
)
)
(defun find-inverse (link resource)
;; NOTE: this method contains a workaround, as muclr:1.15.0 doesn't check whether
;; inverse relations on a predicate actually point to each other.
(let ((i (mu-cl-resources::inverse-links link)))
(when (and
i ; an inverse predicate exists
(equalp ; and the relations on the predicate point to each other
(mu-cl-resources::resource-name link)
(mu-cl-resources::resource-name (getf (car i) :resource))))
(string-downcase (mu-cl-resources::request-path (getf (car i) :link)))
)
)
)
(defun readonly ()
(if (env-value :readonly) 1 0))
(defun env-value (setting)
"Returns the value of the supplied environment variable."
(sb-ext:posix-getenv (string-upcase (string setting))))
; Generate a string with a dispatcher rule for one resource
(defun generate-dispatcher (resource)
"Returns the dispatcher rule for the specific resource"
(let ((path (string-downcase (mu-cl-resources::request-path resource))))
(format nil " match \"/~A/*path\" do~% Proxy.forward conn, path, \"http://resource/~A/\"~% end"
path
path
)
)
)