Add specifying task to candle run
[candle] / src / main / run.lisp
1 (in-package #:candle)
2
3 (defun import-package (pkg)
4  (asdf:load-system pkg))
5
6 (defun run-task (task)
7  (format t "~%~c[1;33mRunning ~:(~A~)~c[0m~%" #\Esc (getf task :name) #\Esc)
8  (if
9   (eval (getf task :directions))
10   (progn (format t "~c[1;32m- ~:(~A~) Passed!~c[0m~%" #\Esc (getf task :name) #\Esc) t)
11   (format t "~c[1;31m- ~:(~A~) Failed!~c[0m~%" #\Esc (getf task :name) #\Esc)))
12
13 (defun list-tasks ()
14  "LIST-TASKS => TASKS
15
16   TASKS: TASK*
17
18 ARGUMENTS AND VALUES:
19
20   TASK: a keyword, representing a task that can be run
21
22 DESCRIPTION:
23
24   Get a list of tasks available in the current .candle file."
25  (if
26   (not (probe-file ".candle"))
27   :dot-candle-absent
28   (with-open-file (str ".candle")
29    (asdf:initialize-source-registry `(:source-registry (:tree ,(car (directory "."))) :INHERIT-CONFIGURATION))
30    (mapcar #'import-package (cdr (read str)))
31    (mapcar (lambda (task) (getf task :name)) (getf (read str) :tasks)))))
32
33 (defun run (&optional specified-task)
34  "RUN &optional SPECIFIED-TASK => RESULT
35
36 ARGUMENTS AND VALUES:
37
38   SPECIFIED-TASK: a keyword, the task to run
39   RESULT: a boolean, whether the process was successful
40
41 DESCRIPTION:
42
43   Runs the script specified by the .candle file in the current directory.
44   When completed, the boolean will be returned if it was successful or not.
45
46   If SPECIFIED-TASK exists, only that task is run."
47  (if
48   (not (probe-file ".candle"))
49   :dot-candle-absent
50   (with-open-file (str ".candle")
51    (asdf:initialize-source-registry `(:source-registry (:tree ,(car (directory "."))) :INHERIT-CONFIGURATION))
52    (mapcar #'import-package (cdr (read str)))
53    (let
54     ((candle-definition (read str)))
55     (format t "Running tasks for ~(~A~)~%" (getf candle-definition :name))
56     (every #'identity
57      (mapcar #'run-task
58       (remove-if
59        (lambda (task) (and specified-task (not (eql (getf task :name) specified-task))))
60        (getf candle-definition :tasks))))))))