changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/bin/skel.lisp

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