list.scm 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; List --- List scripts that can be invoked by guild -*- coding: iso-8859-1 -*-
  2. ;;;; Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free
  16. ;;;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;;;; Boston, MA 02110-1301 USA
  18. ;;; Commentary:
  19. ;; Usage: list
  20. ;;
  21. ;; List scripts that can be invoked by guild.
  22. ;;; Code:
  23. (define-module (scripts list)
  24. #:use-module (srfi srfi-1)
  25. #:export (list-scripts))
  26. (define %include-in-guild-list #f)
  27. (define %summary "An alias for \"help\".")
  28. (define (directory-files dir)
  29. (if (and (file-exists? dir) (file-is-directory? dir))
  30. (let ((dir-stream (opendir dir)))
  31. (let loop ((new (readdir dir-stream))
  32. (acc '()))
  33. (if (eof-object? new)
  34. (begin
  35. (closedir dir-stream)
  36. acc)
  37. (loop (readdir dir-stream)
  38. (if (or (string=? "." new) ; ignore
  39. (string=? ".." new)) ; ignore
  40. acc
  41. (cons new acc))))))
  42. '()))
  43. (define (strip-extensions path)
  44. (or-map (lambda (ext)
  45. (and
  46. (string-suffix? ext path)
  47. ;; We really can't be adding e.g. ChangeLog-2008 to the set
  48. ;; of runnable scripts, just because "" is a valid
  49. ;; extension, by default. So hack around that here.
  50. (not (string-null? ext))
  51. (substring path 0
  52. (- (string-length path) (string-length ext)))))
  53. (append %load-compiled-extensions %load-extensions)))
  54. (define (unique l)
  55. (cond ((null? l) l)
  56. ((null? (cdr l)) l)
  57. ((equal? (car l) (cadr l)) (unique (cdr l)))
  58. (else (cons (car l) (unique (cdr l))))))
  59. (define (find-submodules head)
  60. (let ((shead (map symbol->string head)))
  61. (unique
  62. (sort
  63. (append-map (lambda (path)
  64. (fold (lambda (x rest)
  65. (let ((stripped (strip-extensions x)))
  66. (if stripped (cons stripped rest) rest)))
  67. '()
  68. (directory-files
  69. (fold (lambda (x y) (in-vicinity y x)) path shead))))
  70. %load-path)
  71. string<?))))
  72. (define (list-scripts . args)
  73. (for-each (lambda (x)
  74. ;; would be nice to show a summary.
  75. (format #t "~A\n" x))
  76. (find-submodules '(scripts))))
  77. (define (main . args)
  78. (apply (@@ (scripts help) main) args))