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