-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
environment.lisp
173 lines (146 loc) · 6.95 KB
/
environment.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
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
(in-package #:org.shirakumo.radiance.core)
(defvar *deploying-p* NIL)
(defvar *environment* NIL)
(define-hook environment-change ())
(defgeneric environment-directory (environment type))
(defgeneric environment-module-directory (module type))
(defmethod environment-directory (environment kind)
(ecase kind
(:configuration
(let ((base (or* (uiop:getenv "XDG_CONFIG_HOME")
#+windows (uiop:getenv "AppData")
#+windows (make-pathname :directory '(:absolute :home "Application Data"))
(make-pathname :directory '(:absolute :home ".config")))))
(merge-pathnames (make-pathname :directory `(:relative "radiance" ,environment))
(etypecase base
(pathname base)
(string (uiop:parse-native-namestring base))))))
(:cache
(let ((base (or* (uiop:getenv "XDG_CACHE_HOME")
#+windows (uiop:getenv "TEMP")
#+windows (make-pathname :directory '(:absolute :home "Local Settings" "Temp"))
(make-pathname :directory '(:absolute :home ".cache")))))
(merge-pathnames (make-pathname :directory `(:relative "radiance" ,environment))
(etypecase base
(pathname base)
(string (uiop:parse-native-namestring base))))))
((:data :template :static)
(let ((base (or* (uiop:getenv "XDG_DATA_HOME")
#+windows (uiop:getenv "LocalAppData")
#+windows (make-pathname :directory '(:absolute :home "Local Settings"))
(make-pathname :directory '(:absolute :home ".local" "share")))))
(merge-pathnames (make-pathname :directory `(:relative "radiance" ,environment ,(string-downcase kind)))
(etypecase base
(pathname base)
(string (uiop:parse-native-namestring base))))))))
(defmethod environment-directory ((environment (eql T)) kind)
(environment-directory *environment* kind))
(defmethod environment-module-directory (identifier kind)
(environment-module-directory (module identifier) kind))
(defmethod environment-module-directory ((module package) kind)
(check-environment)
(let ((name (module-name module)))
(merge-pathnames
(make-pathname :directory `(:relative ,(string-downcase name)))
(environment-directory T kind))))
(defun environment-module-pathname (module kind pathname)
(merge-pathnames
pathname
(environment-module-directory module kind)))
(defun environment ()
*environment*)
(defun (setf environment) (environment)
(check-type environment string)
;; Clear config
(dolist (module (list-modules))
(setf (module-storage module :config) NIL))
;; Update environment
(setf *environment* environment)
;; Reload configuration
(handler-case
(ubiquitous:restore #.*package*)
(ubiquitous:no-storage-file ()
(warn "Configuration for ~s not found-- creating from defaults." environment)
(ubiquitous:restore (asdf:system-relative-pathname :radiance "default-config.lisp"))
(ubiquitous:offload #.*package*)))
(trigger 'environment-change))
(defun check-environment ()
(restart-case
(unless *environment*
(error 'environment-not-set))
(continue ()
:report "Use and load the default environment."
(setf (environment) "default"))
(set-environment (environment)
:report "Set and load a specific environment."
:interactive (lambda () (list (read *query-io*)))
(setf (environment) environment))))
(defun reload-environment ()
(check-environment)
(setf (environment) (environment)))
(defun sync-environment ()
(dolist (module (modularize:list-modules) T)
(ubiquitous:with-local-storage (module :storage (mconfig-storage module))
(ubiquitous:offload))))
(defun mconfig-pathname (module &optional (type :lisp))
(make-pathname :name (string-downcase (module-name module))
:type (format NIL "conf.~(~a~)" type)
:defaults (environment-module-directory module :configuration)))
(defmethod ubiquitous:designator-pathname ((designator package) type)
(mconfig-pathname designator type))
(defun mconfig-storage (module)
(let* ((module (module module))
(*package* module))
(or (module-storage module :config)
(setf (module-storage module :config)
(ubiquitous:with-local-storage (module :transaction NIL)
(handler-bind ((ubiquitous:no-storage-file
(lambda (err)
(declare (ignore err))
(invoke-restart 'ubiquitous:use-new-storage
(make-hash-table :test 'equal)))))
(ubiquitous:restore))
(ubiquitous:offload))))))
(defun mconfig (module &rest path)
(let ((module (module module)))
(ubiquitous:with-local-storage (module :storage (mconfig-storage module))
(apply #'ubiquitous:value path))))
(defun (setf mconfig) (value module &rest path)
(let* ((module (module module))
(*package* module))
(ubiquitous:with-local-storage (module :storage (mconfig-storage module))
(apply #'(setf ubiquitous:value) value path))))
(defun defaulted-mconfig (default module &rest path)
(let* ((module (module module))
(*package* module))
(ubiquitous:with-local-storage (module :storage (mconfig-storage module))
(apply #'ubiquitous:defaulted-value default path))))
(defun remmconfig (module &rest path)
(let* ((module (module module))
(*package* module))
(ubiquitous:with-local-storage (module :storage (mconfig-storage module))
(apply #'ubiquitous:remvalue path))))
(defmacro config (&rest path)
`(mconfig ,*package* ,@path))
(defmacro defaulted-config (default &rest path)
`(defaulted-mconfig ,default ,*package* ,@path))
(defmacro remconfig (&rest path)
`(remmconfig ,*package* ,@path))
(defun static-file (namestring &optional base)
(let ((base (or base *package*)))
(or (probe-file (environment-module-pathname base :static namestring))
(merge-pathnames namestring (merge-pathnames "static/" (merge-pathnames (resolve-base base)))))))
(defmacro @static (&environment env namestring)
(if (and (constantp namestring env)
(not *deploying-p*))
`(load-time-value (static-file ,namestring ,*package*))
`(static-file ,namestring ,*package*)))
(defun template-file (namestring &optional base)
(let ((base (or base *package*)))
(or (probe-file (environment-module-pathname base :template namestring))
(merge-pathnames namestring (merge-pathnames "template/" (merge-pathnames (resolve-base base)))))))
(defmacro @template (&environment env namestring)
(if (and (constantp namestring env)
(not *deploying-p*))
`(load-time-value (template-file ,namestring ,*package*))
`(template-file ,namestring ,*package*)))