[mkly] remove `else` rule

This commit is contained in:
contrapunctus 2021-03-29 02:10:59 +05:30
parent dfb179093d
commit 4b26085a15
2 changed files with 40 additions and 37 deletions

69
mkly
View File

@ -136,13 +136,14 @@
;; command line matches this symbol;
;; * a string - treated as a regular expression; COMMAND will be run
;; when it matches the target passed on the command line
;; * `else', as the last rule - COMMAND will be run when no other rule matches
;;
;; COMMAND and arguments can be any value - lists will be flattened,
;; all values will be converted to strings, and spaces will be added.
;; String values will be quoted, which is useful for escaping file
;; names in the final command to be run.
(define (rules target)
;; if TARGET is a subdirectory in the project root, descend to it
;; before executing the action
(let* ((subdir (parent target))
(branch (vcs-current-branch))
(dest (begin
@ -161,14 +162,13 @@
,target)
("part-.*\\.ly" lilypond -dno-point-and-click
-o ,(string-append
dest project-name "-"
(file-name-no-extension target))
,target)
(else mkly all))))
dest project-name "-" (file-name-no-extension target))
,target))))
;; Return a rule from RULES which matches TARGET
;; RULES must be a list of lists (see procedure `rules') in the form
;; (PATTERN COMMAND [ARGS ...])
;;
;; RULES must be a list (see procedure `rules'), where each element is
;; in the form (PATTERN COMMAND [ARGS ...])
;;
;; TARGET must be a string.
;; If it is empty, return the first rule in RULES.
@ -176,36 +176,37 @@
;; symbol, or if TARGET is matched by a PATTERN regular expression,
;; return the rule whose TARGET matched.
(define (select-rule rules target)
(let* ((no-match? (when (null? rules) (error "mkly: no matching rule")))
(rule (first rules))
(pattern (first rule))
(rule-command (rest rule)))
;; (format (current-error-port) "~%rule - ~s~%pattern - ~s~%" rule target)
(cond ((and (= 1 (length rules))
(eq? 'else (first rule)))
rule)
((equal? "" target) rule)
((and (string? pattern)
(string-match pattern target))
;; (format #t "~%mkly: running ~s~%" rule-command)
rule)
((and (symbol? pattern)
(equal? target (symbol->string pattern)))
;; (format #t "~%mkly: running ~s~%" rule-command)
rule)
(else (select-rule (rest rules) target)))))
(if (null? rules)
(begin (debug "mkly: no matching rule") #f)
(let* ((rule (first rules))
(pattern (first rule))
(rule-command (rest rule)))
;; (format (current-error-port) "~%rule - ~s~%pattern - ~s~%" rule target)
(cond ((equal? "" target) rule)
((and (string? pattern)
(string-match pattern target))
;; (format #t "~%mkly: running ~s~%" rule-command)
rule)
((and (symbol? pattern)
(equal? target (symbol->string pattern)))
;; (format #t "~%mkly: running ~s~%" rule-command)
rule)
(else (select-rule (rest rules) target))))))
;; target ::= action -> run
;; | target -> for each target, recurse with new target
(define (run-rule-for target)
(let* ((rules (rules target))
(let* ((rules (rules target))
(selected-rule (flatten (select-rule rules target)))
(action (if (equal? "./mkly" (first (command-line)))
(map (lambda (it)
(if (or (eq? it 'mkly)
(equal? it "mkly"))
(string-append (getcwd) "/mkly")
it))
(rest selected-rule))
(rest selected-rule))))
;; Support for actions which call mkly itself. Do we really want this?
(action (if (equal? "./mkly" (first (command-line)))
(map (lambda (it)
(if (or (eq? it 'mkly)
(equal? it "mkly"))
(string-append (getcwd) "/mkly")
it))
(rest selected-rule))
(rest selected-rule))))
(debug "~%~{~a - ~s~%~}~%"
(list 'command-line (command-line)
'rules rules

View File

@ -8,8 +8,8 @@
#:file-name-part
(equal? "-foo" (file-name-part "foo"))
(equal? "" (file-name-part #f))
#:use-dir!
#:vcs-current-branch
;; #:use-dir!
;; #:vcs-current-branch
#:flatten
(equal? (flatten '(1 "a" b (c "d" 3)))
'(1 "a" b c "d" 3))
@ -24,4 +24,6 @@
(equal? (select-rule rules "dev") '(dev 2))
(equal? (select-rule rules "main.ly") '(main.ly 3))
(equal? (select-rule rules "part-flute.ly") '("part-.*\\.ly" 4))
(equal? (select-rule rules "frobnicate") '(else 5))))))
(equal? (select-rule rules "frobnicate") #f)))))
(mkly-tests)