Use a macro to define commands

This commit is contained in:
Tom Willemse 2013-06-05 00:58:16 +02:00
parent c00a0ca730
commit 5f3dfee5ac

View file

@ -28,6 +28,17 @@
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:export (main)) #:export (main))
(define-syntax define-command
(syntax-rules ()
((_ (name . args)
exp exp* ...)
(begin
(set! command-list
(cons (cons (symbol->string (quote name))
(lambda args
exp exp* ...))
command-list))))))
(define command-list '()) (define command-list '())
(define config-exclusion-list '()) (define config-exclusion-list '())
@ -46,7 +57,7 @@ set in the current environment."
(define (data-dir) (storage-dir "XDG_DATA_HOME" "/.local/share")) (define (data-dir) (storage-dir "XDG_DATA_HOME" "/.local/share"))
(define (data-file file) (string-append (data-dir) "/" file)) (define (data-file file) (string-append (data-dir) "/" file))
(define (version) (define-command (version)
"Display version information." "Display version information."
(display "gitto 0.1.0") (newline) (display "gitto 0.1.0") (newline)
(display "Copyright (C) 2012 Tom Willemse") (newline) (display "Copyright (C) 2012 Tom Willemse") (newline)
@ -54,9 +65,8 @@ set in the current environment."
(display "You may redistribute copies of this program") (newline) (display "You may redistribute copies of this program") (newline)
(display "under the terms of the GNU General Public License.") (newline) (display "under the terms of the GNU General Public License.") (newline)
(display "For more information about these matters, see the file named COPYING.") (newline)) (display "For more information about these matters, see the file named COPYING.") (newline))
(set! command-list (append command-list `(("version" . ,version))))
(define (help) (define-command (help)
"Display some help." "Display some help."
(display "\ (display "\
gitto [command [arguments ...]] gitto [command [arguments ...]]
@ -74,7 +84,6 @@ gitto [command [arguments ...]]
version Display version version Display version
help Display this help help Display this help
")) "))
(set! command-list (append command-list `(("help" . ,help))))
(define (known? repo) (define (known? repo)
"Do we know REPO?" "Do we know REPO?"
@ -99,14 +108,12 @@ gitto [command [arguments ...]]
(write repos port) (write repos port)
(close-port port))) (close-port port)))
(define (repository-registered? repository) (define-command (check repository)
"Check to see if REPOSITORY has been registered." "Check to see if REPOSITORY has been registered."
(format #t "Repository is~a registered~%" (format #t "Repository is~a registered~%"
(if (known? repository) "" " not"))) (if (known? repository) "" " not")))
(set! command-list (append command-list
`(("check" . ,repository-registered?))))
(define (register-repository repository) (define-command (add repository)
"Register REPOSITORY in the repository list." "Register REPOSITORY in the repository list."
(set! repository (make <repository> (realpath repository))) (set! repository (make <repository> (realpath repository)))
(if (not (known? repository)) (if (not (known? repository))
@ -126,10 +133,8 @@ gitto [command [arguments ...]]
(update-repo-config repository))) (update-repo-config repository)))
(display "Repository already registered.")) (display "Repository already registered."))
(newline)) (newline))
(set! command-list (append command-list
`(("add" . ,register-repository))))
(define (remove-repository repository) (define-command (remove repository)
"Remove/unregister REPOSITORY from the repository list." "Remove/unregister REPOSITORY from the repository list."
(unless (member repository repositories same-repository?) (unless (member repository repositories same-repository?)
(set! repository (realpath repository))) (set! repository (realpath repository)))
@ -142,16 +147,12 @@ gitto [command [arguments ...]]
(simple-format #t "Repository ~A removed." repository)) (simple-format #t "Repository ~A removed." repository))
(display "Not a registered repository.")) (display "Not a registered repository."))
(newline)) (newline))
(set! command-list (append command-list
`(("remove" . ,remove-repository))))
(define (list-repositories . args) (define-command (list . args)
"List information about every repository." "List information about every repository."
(if (and (not (eq? args '())) (equal? (car args) "locations")) (if (and (not (eq? args '())) (equal? (car args) "locations"))
(list-repository-locations) (list-repository-locations)
(for-each print repositories))) (for-each print repositories)))
(set! command-list (append command-list
`(("list" . ,list-repositories))))
(define (list-repository-locations) (define (list-repository-locations)
"List the registered locations of repositories." "List the registered locations of repositories."
@ -163,20 +164,19 @@ gitto [command [arguments ...]]
(lambda (s1 s2) (lambda (s1 s2)
(string<? (repo-location s1) (repo-location s2)))))) (string<? (repo-location s1) (repo-location s2))))))
(define (purge) (define-command (purge)
"Purge all items from the list that can no longer be found." "Purge all items from the list that can no longer be found."
(set! repositories (set! repositories
(filter (lambda (repo) (filter (lambda (repo)
(file-exists? (repo-location repo))) (file-exists? (repo-location repo)))
repositories)) repositories))
(save-repositories-list)) (save-repositories-list))
(set! command-list (append command-list `(("purge" . ,purge))))
(define (show-global-config) (define (show-global-config)
"Show the template specified in `global-config'." "Show the template specified in `global-config'."
(write-config global-config)) (write-config global-config))
(define (show-config . args) (define-command (config . args)
"Do something with the config module. "Do something with the config module.
If ARGS is an empty list, show each repository's current If ARGS is an empty list, show each repository's current
@ -202,7 +202,6 @@ repositories in `config-exclusion-list'."
(unless (member (repo-name r) config-exclusion-list) (unless (member (repo-name r) config-exclusion-list)
(install-hooks (repo-location r)))) (install-hooks (repo-location r))))
repositories)))) repositories))))
(set! command-list (append command-list `(("config" . ,show-config))))
(define (update-repo-config repo) (define (update-repo-config repo)
"Merge the configured configuration with REPO's configuration. "Merge the configured configuration with REPO's configuration.