changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/bin/skel.lisp

changeset 570: d1c75054df03
parent: 32995daa9a07
child: 35bb0d5ec95e
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 30 Jul 2024 21:54:16 -0400
permissions: -rw-r--r--
description: added phases
1 ;;; Code:
2 
3 ;; TODO 2024-05-09: add shell configurables to rules - maybe at sk-command
4 ;; level. :INPUT :WAIT :OUTPUT
5 (in-package :std-user)
6 (defpkg :bin/skel
7  (:use :cl :std :cli/clap
8  :vc :sb-ext :skel :log
9  :dat/sxp #+tools :skel/tools/viz)
10  (:import-from :cli/shell :*shell-input*)
11  (:use :cli/tools/sbcl)
12  (:export :main))
13 
14 (in-package :bin/skel)
15 (in-readtable :shell)
16 
17 (defopt skc-help (print-help *cli*) *arg*)
18 (defopt skc-version (print-version *cli*))
19 (defopt skc-level *log-level*
20  (setq *log-level* (if *arg* (if (stringp *arg*)
21  (sb-int:keywordicate (string-upcase *arg*))
22  *arg*)
23  :info)))
24 
25 ;; TODO 2023-10-13: almost there
26 ;; (defopt skc-config
27 ;; (init-user-skelrc (when *arg* (parse-file-opt *arg*))))
28 
29 (defcmd skc-edit
30  (let ((file (or (when *args* (pop *args*)) (sk-path *skel-project*))))
31  (cli/ed:run-emacsclient (namestring file))))
32 
33 (defcmd skc-init
34  (let ((file (when *args* (pop *args*)))
35  (name (when (> *argc* 1) (pop *args*))))
36  ;; TODO: test, may need to be
37  ;; sequential for side-effect
38  ;; of pop
39  (handler-bind
40  ((sb-ext:file-exists
41  #'(lambda (s)
42  (std:println (format nil "file already exists: ~A" (or file *default-skelfile*)))
43  (let ((f2 (read-line)))
44  (if (string= f2 "")
45  (error s)
46  (use-value f2 s))))))
47  (init-skelfile file name))))
48 
49 (defcmd skc-describe
50  (describe
51  (if (> *argc* 0)
52  (find-skelfile (pathname (car *args*)) :load t)
53  (or *skel-project* *skel-user-config* *skel-system-config*))))
54 
55 
56 (defcmd skc-inspect
57  (sb-ext:enable-debugger)
58  (setq *no-exit* t)
59  (inspect
60  (find-skelfile
61  (if *args* (pathname (car *args*))
62  #P".")
63  :load t)))
64 
65 #+tools
66 (defcmd skc-view
67  (if *args*
68  (let ((stuff (loop for a in *args*
69  collect (sk-slot-case a))))
70  (sk-view (if (= 1 (length stuff)) (car stuff) stuff)))
71  (sk-view (if (boundp '*skel-project*) *skel-project*
72  (if (boundp '*skel-user-config*) *skel-user-config*
73  (if (boundp '*skel-system-config*) *skel-system-config*
74  (skel-simple-error "skel config files not installed")))))))
75 
76 (defcmd skc-id
77  (println (std:format-sxhash (obj/id:id (find-skelfile #P"." :load t)))))
78 
79 (defun call-with-args (action args)
80  (if (null args)
81  (sk-call *skel-project* action)
82  (mapc (lambda (x)
83  (sk-call *skel-project* (keywordicate action '- (string-upcase x))))
84  args)))
85 
86 (defcmd skc-compile
87  (call-with-args :compile *args*))
88 (defcmd skc-build
89  (call-with-args :build *args*))
90 (defcmd skc-dist
91  (call-with-args :dist *args*))
92 (defcmd skc-install
93  (call-with-args :install *args*))
94 (defcmd skc-pack
95  (call-with-args :pack *args*))
96 (defcmd skc-unpack
97  (call-with-args :unpack *args*))
98 (defcmd skc-bundle
99  (call-with-args :bundle *args*))
100 (defcmd skc-unbundle
101  (call-with-args :unbundle *args*))
102 (defcmd skc-clean
103  (call-with-args :clean *args*))
104 (defcmd skc-test
105  (call-with-args :test *args*))
106 (defcmd skc-bench
107  (call-with-args :bench *args*))
108 (defcmd skc-save
109  (call-with-args :save *args*))
110 
111 (defun sk-slot-case (sel)
112  (std/string:string-case (sel :default (skel-simple-error "invalid slot"))
113  (":id" (std:format-sxhash (obj/id:id *skel-project*)))
114  (":name" (sk-name *skel-project*))
115  (":author" (sk-author *skel-project*))
116  (":version" (sk-version *skel-project*))
117  (":description" (sk-description *skel-project*))
118  (":tags" (sk-tags *skel-project*))
119  (":license" (sk-license *skel-project*))
120  (":vc" (sk-vc *skel-project*))
121  (":components" (sk-components *skel-project*))
122  (":scripts" (sk-scripts *skel-project*))
123  (":rules" (sk-rules *skel-project*))
124  (":phases" (hash-table-alist (sk-phases *skel-project*)))
125  (":env" (sk-env *skel-project*))
126  (":bind" (sk-bind *skel-project*))
127  (":include" (sk-include *skel-project*))
128  (":stash" (sk-stash *skel-project*))
129  (":store" (sk-store *skel-project*))
130  (":config" *skel-user-config*)
131  (":sys" *skel-system-config*)
132  (":cache" (sk-cache *skel-user-config*))))
133 
134 (defcmd skc-show
135  (if *args*
136  (mapc (lambda (x) (when-let ((ret (sk-slot-case x))) (println ret))) *args*)
137  (describe (if (boundp '*skel-project*) *skel-project*
138  (if (boundp '*skel-user-config*) *skel-user-config*
139  (if (boundp '*skel-system-config*) *skel-system-config*
140  (skel-simple-error "skel config files not installed")))))))
141 
142 (defcmd skc-push
143  (case (sk-vc-meta-kind (sk-vc (find-skelfile #P"." :load t)))
144  (:git (run-git-command "push" *args* t))
145  (:hg (run-hg-command "push" *args* t))
146  (t (skel-simple-error "unknown VC type"))))
147 
148 (defcmd skc-pull
149  (case (sk-vc-meta-kind (sk-vc (find-skelfile #P"." :load t)))
150  (:git (run-git-command "pull" *args* t))
151  (:hg (run-hg-command "pull" (append '("-u") *args*) t))
152  (t (skel-simple-error "unknown VC type"))))
153 
154 (defun hg-status ()
155  (with-open-stream (proc (process-output (run-hg-command "status" nil :stream)))
156  (loop for x = (read-line proc nil)
157  while x
158  do (println x))))
159 
160 (defun git-status ()
161  (with-open-stream (proc (run-git-command "status" nil :stream))
162  (loop for x = (read-line proc nil)
163  while x
164  do (println x))))
165 
166 (defcmd skc-status
167  (case (sk-vc-meta-kind (sk-vc (find-skelfile #P"." :load t)))
168  (:git (git-status))
169  (:hg (hg-status))
170  (t (hg-status))))
171 
172 (defcmd skc-clone
173  (case (sk-vc-meta-kind (sk-vc (find-skelfile #P"." :load t)))
174  (:git (run-git-command "clone" *args* t))
175  (:hg (run-hg-command "clone" *args* t))
176  (t (skel-simple-error "unknown VC type"))))
177 
178 (defcmd skc-commit
179  ;; (debug! *optc* *argc*)
180  (case (sk-vc-meta-kind (sk-vc (find-skelfile #P"." :load t)))
181  (:git (run-git-command "commit" *args* t))
182  (:hg (run-hg-command "commit" *args* t))
183  (t (skel-simple-error "unknown VC type"))))
184 
185 (defcmd skc-make
186  (let ((sk (find-skelfile #P"." :load t)))
187  (sb-ext:enable-debugger)
188  (print *args*)
189  ;; (setq *no-exit* t)
190  (if *args*
191  (loop for a in *args*
192  do (debug!
193  (when-let ((rule (sk-find-rule a sk)))
194  (sk-make sk rule))))
195  (debug! (sk-make sk (aref (sk-rules sk) 0))))))
196 
197 (defcmd skc-run
198  (if *args*
199  (mapc (lambda (script)
200  (debug!
201  (sk-run
202  (sk-find-script
203  (pathname-name script)
204  (find-skelfile #P"." :load t))))) *args*)
205  (required-argument 'name)))
206 
207 (defcmd skc-vc
208  (if *args*
209  (std/string:string-case ((car *args*) :default (skel-simple-error "invalid command"))
210  ("status" (skc-status nil nil)))
211  (skc-status nil *opts*)))
212 
213 (defcmd skc-shell
214  (sb-ext:enable-debugger)
215  (trace! "starting skel shell")
216  (setq *no-exit* t)
217  (cli/clap::with-cli-handlers
218  (progn
219  (in-package :sk-user)
220  (use-package :cl-user)
221  (use-package :sb-ext)
222  (use-package :std-user)
223  (init-skel-vars)
224  (println "Welcome to SKEL")
225  (sb-impl::toplevel-repl nil))))
226 
227 (defcmd skc-new
228  (trace! *args* *opts*))
229 
230 (define-cli *cli*
231  :name "skel"
232  :version #.(format nil "0.1.1:~A" (read-line (sb-ext:process-output (vc:run-hg-command "id" '("-i") :stream))))
233  :description "A hacker's project compiler."
234  :thunk skc-show
235  :opts ((:name "help" :global t :description "print this message"
236  :thunk skc-help)
237  (:name "version" :global t :description "print version"
238  :thunk skc-version)
239  (:name "level" :global t :description "set log level (warn,info,debug,trace)"
240  :thunk skc-level)
241  (:name "config" :global t :description "set a custom skel user config" :kind file)
242  (:name "input" :global t :description "input source" :kind string)
243  (:name "output" :global t :description "output target" :kind string))
244  :cmds ((:name init
245  :description "initialize a skelfile in the current directory"
246  :opts ((:name "name" :description "project name" :kind string))
247  :thunk skc-init)
248  (:name new
249  :description "make a new skel project"
250  :opts ((:name "name" :description "project name" :kind string))
251  :thunk skc-new)
252  (:name describe
253  :description "describe a skelfile"
254  :thunk skc-describe)
255  (:name show
256  :description "show project slots"
257  :opts ((:name "file" :description "path to skelfile" :kind file)
258  (:name "user" :description "print user configuration")
259  (:name "system" :description "print system configuration"))
260  :thunk skc-show)
261  (:name vc
262  :description "version control"
263  :thunk skc-vc
264  :opts ((:name "root" :description "repository path" :kind directory)))
265  (:name id
266  :description "print the project id"
267  :thunk skc-id)
268  (:name inspect
269  :description "inspect the project skelfile"
270  :opts ((:name "file" :description "path to skelfile" :kind file))
271  :thunk skc-inspect)
272  #+tools
273  (:name view
274  :description "view an object in a new GUI window"
275  :thunk skc-view)
276  (:name make
277  :description "build project targets"
278  :opts ((:name "target" :description "target to build" :kind string))
279  :thunk skc-make)
280  (:name run
281  :description "run a script or command"
282  :thunk skc-run)
283  (:name compile
284  :description "compile source code"
285  :thunk skc-compile)
286  (:name build
287  :description "build programs and libraries"
288  :thunk skc-build)
289  (:name dist
290  :description "distribute build artifacts"
291  :thunk skc-dist)
292  (:name install
293  :description "install stuff"
294  :thunk skc-install)
295  (:name pack
296  :description "pack stuff"
297  :thunk skc-pack)
298  (:name unpack
299  :description "unpack stuff"
300  :thunk skc-unpack)
301  (:name bundle
302  :description "bundle source code"
303  :thunk skc-bundle)
304  (:name unbundle
305  :description "unbundle source code"
306  :thunk skc-unbundle)
307  (:name clean
308  :description "clean up the project"
309  :thunk skc-clean)
310  (:name test
311  :description "run tests"
312  :thunk skc-test)
313  (:name bench
314  :description "run benchmark"
315  :thunk skc-bench)
316  (:name status
317  :description "print the vc status"
318  :thunk skc-status)
319  (:name push
320  :description "push the current project upstream"
321  :thunk skc-push)
322  (:name pull
323  :description "pull the current project from remote"
324  :thunk skc-pull)
325  (:name clone
326  :description "clone a remote project"
327  :thunk skc-clone)
328  (:name commit
329  :description "commit changes to the project vc"
330  :thunk skc-commit)
331  (:name edit
332  :description "edit a project file in emacs."
333  :thunk skc-edit)
334  (:name shell
335  :description "open the sk-shell interpreter"
336  :thunk skc-shell)))
337 
338 (defmain ()
339  (in-package :sk-user)
340  (let ((*log-level* :info))
341  (in-readtable :shell)
342  (with-cli (opts cmds) *cli*
343  (debug-opts *cli*)
344  (init-skel-vars)
345  (when-let ((project (find-skelfile #P".")))
346  (setq *skel-project* (load-skelfile project)))
347  (do-cmd *cli*))))