Skip to content

Commit d9aae13

Browse files
committed
impls hooks
1 parent ab6c43d commit d9aae13

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

Qob

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
;; -*- mode: lisp; lexical-binding: t -*-
22

3-
(files "lisp")
3+
(files "lisp"
4+
"scripts"
5+
"Makefile"
6+
"LICENSE"
7+
"Qob")
48

59
(source "quicklisp")
610
(source "ultralisp")
711

12+
;; Test local systems
813
(depends-on "fsdb" "https://github.com/billstclair/fsdb" :git)
14+
15+
;; Test hooks
16+
(qob-add-hook 'qob-before-command-hook
17+
(lambda ()
18+
(qob-info "? before command")))
19+
20+
(qob-add-hook 'qob-after-command-hook
21+
(lambda ()
22+
(qob-info "? after command")))
23+
24+
(qob-add-hook 'qob-before-info-hook
25+
(lambda ()
26+
(qob-info "? before info")))
27+
28+
(qob-add-hook 'qob-after-info-hook
29+
(lambda ()
30+
(qob-info "? after info")))

lisp/_prepare.lisp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
(defvar qob-files nil)
1717
(defvar qob-depends-on nil)
1818

19+
(defvar qob-before-command-hook nil)
20+
(defvar qob-after-command-hook nil)
21+
1922
;;
2023
;;; Utils
2124

@@ -599,6 +602,58 @@ Set up the systems; on contrary, you should use the function
599602
(defvar qob-dist-path "dist/"
600603
"Default path where to place the package artifact.")
601604

605+
;;
606+
;;; Entry
607+
608+
(defun qob-add-hook (hook function)
609+
"Add FUNCTION to the HOOK list if not already present."
610+
(set hook nil) ; Declare it.
611+
(unless (member function (symbol-value hook))
612+
(push function (symbol-value hook))))
613+
614+
(defun qob-run-hooks (hooks)
615+
"Run all functions in the HOOKS list."
616+
(dolist (fn (ignore-errors (symbol-value hooks)))
617+
(funcall fn)))
618+
619+
(defun qob--form-command-var (name)
620+
"From the command variable by NAME."
621+
(let* ((name (string-upcase name))
622+
(name (intern name)))
623+
name))
624+
625+
(defun qob--before-command-var ()
626+
"Return the before command name."
627+
(let* ((command (qob-command))
628+
(before (concatenate 'string "qob-before-" command "-hook")))
629+
(qob--form-command-var before)))
630+
631+
(defun qob--after-command-var ()
632+
"Return the after command name."
633+
(let* ((command (qob-command))
634+
(after (concatenate 'string "qob-after-" command "-hook")))
635+
(qob--form-command-var after)))
636+
637+
;; Define variable
638+
(let ((before (qob--before-command-var))
639+
(after (qob--after-command-var)))
640+
(set before nil)
641+
(set after nil))
642+
643+
(defun qob--with-hooks (body)
644+
"Execute BODY with before/after hooks."
645+
(let ((before (qob--before-command-var))
646+
(after (qob--after-command-var)))
647+
(qob-run-hooks 'qob-before-command-hook)
648+
(qob-run-hooks before)
649+
(funcall body)
650+
(qob-run-hooks after)
651+
(qob-run-hooks 'qob-after-command-hook)))
652+
653+
(defmacro qob-start (&rest body)
654+
"Execute BODY with workspace setup."
655+
`(qob--with-hooks (lambda () ,@body)))
656+
602657
;;
603658
;;; Initialization
604659

lisp/core/info.lisp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@
5252
(qob-info--print-dep depends-on)
5353
(qob-info--print-dep (mapcar #'car qob-depends-on)))))
5454

55-
(let ((names (qob-args))
56-
(default-system (qob-only-system)))
57-
(cond
58-
;; If only specified one system.
59-
(default-system
60-
(qob-info--print-system (car default-system)))
61-
;; If no system(s) specified.
62-
((zerop (length names))
63-
(qob-help "core/info"))
64-
;; Print all systems information.
65-
(t
66-
(dolist (name names)
67-
(qob-info--print-system name)))))
55+
(qob-start
56+
(let ((names (qob-args))
57+
(default-system (qob-only-system)))
58+
(cond
59+
;; If only specified one system.
60+
(default-system
61+
(qob-info--print-system (car default-system)))
62+
;; If no system(s) specified.
63+
((zerop (length names))
64+
(qob-help "core/info"))
65+
;; Print all systems information.
66+
(t
67+
(dolist (name names)
68+
(qob-info--print-system name))))))
6869

6970
;;; End of lisp/core/info.lisp

0 commit comments

Comments
 (0)