list.scm 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;; List --- List scripts that can be invoked by guild -*- coding: iso-8859-1 -*-
  2. ;;;; Copyright (C) 2009, 2010, 2011 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. #:export (list-scripts))
  25. (define %include-in-guild-list #f)
  26. (define %summary "An alias for \"help\".")
  27. (define (directory-files dir)
  28. (if (and (file-exists? dir) (file-is-directory? dir))
  29. (let ((dir-stream (opendir dir)))
  30. (let loop ((new (readdir dir-stream))
  31. (acc '()))
  32. (if (eof-object? new)
  33. (begin
  34. (closedir dir-stream)
  35. acc)
  36. (loop (readdir dir-stream)
  37. (if (or (string=? "." new) ; ignore
  38. (string=? ".." new)) ; ignore
  39. acc
  40. (cons new acc))))))
  41. '()))
  42. (define (strip-extensions path)
  43. (or-map (lambda (ext)
  44. (and
  45. (string-suffix? ext path)
  46. ;; We really can't be adding e.g. ChangeLog-2008 to the set
  47. ;; of runnable scripts, just because "" is a valid
  48. ;; extension, by default. So hack around that here.
  49. (not (string-null? ext))
  50. (substring path 0
  51. (- (string-length path) (string-length ext)))))
  52. (append %load-compiled-extensions %load-extensions)))
  53. (define (unique l)
  54. (cond ((null? l) l)
  55. ((null? (cdr l)) l)
  56. ((equal? (car l) (cadr l)) (unique (cdr l)))
  57. (else (cons (car l) (unique (cdr l))))))
  58. (define (find-submodules head)
  59. (let ((shead (map symbol->string head)))
  60. (unique
  61. (sort
  62. (append-map (lambda (path)
  63. (fold (lambda (x rest)
  64. (let ((stripped (strip-extensions x)))
  65. (if stripped (cons stripped rest) rest)))
  66. '()
  67. (directory-files
  68. (fold (lambda (x y) (in-vicinity y x)) path shead))))
  69. %load-path)
  70. string<?))))
  71. (define (list-scripts . args)
  72. (for-each (lambda (x)
  73. ;; would be nice to show a summary.
  74. (format #t "~A\n" x))
  75. (find-submodules '(scripts))))
  76. (define (main . args)
  77. (apply (@@ (scripts help) main) args))