Skip to content
Snippets Groups Projects
Commit ba326ce4 authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

distro: Add `fold-packages'.

* distro.scm (fold-packages): New procedure.
  (find-packages-by-name): Use it instead of hand-written traversal;
  remove `package?' checks from `right-package?'.
* tests/packages.scm ("fold-packages"): New test.
parent 733b4130
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,7 @@ (define-module (distro) ...@@ -26,6 +26,7 @@ (define-module (distro)
#:export (search-patch #:export (search-patch
search-bootstrap-binary search-bootstrap-binary
%patch-directory %patch-directory
fold-packages
find-packages-by-name)) find-packages-by-name))
;;; Commentary: ;;; Commentary:
...@@ -105,22 +106,34 @@ (define not-slash ...@@ -105,22 +106,34 @@ (define not-slash
(false-if-exception (resolve-interface name)))) (false-if-exception (resolve-interface name))))
(package-files))) (package-files)))
(define (fold-packages proc init)
"Call (PROC PACKAGE RESULT) for each available package, using INIT as
the initial value of RESULT."
(fold (lambda (module result)
(fold (lambda (var result)
(if (package? var)
(proc var result)
result))
result
(module-map (lambda (sym var)
(false-if-exception (variable-ref var)))
module)))
init
(package-modules)))
(define* (find-packages-by-name name #:optional version) (define* (find-packages-by-name name #:optional version)
"Return the list of packages with the given NAME. If VERSION is not #f, "Return the list of packages with the given NAME. If VERSION is not #f,
then only return packages whose version is equal to VERSION." then only return packages whose version is equal to VERSION."
(define right-package? (define right-package?
(if version (if version
(lambda (p) (lambda (p)
(and (package? p) (and (string=? (package-name p) name)
(string=? (package-name p) name)
(string=? (package-version p) version))) (string=? (package-version p) version)))
(lambda (p) (lambda (p)
(and (package? p) (string=? (package-name p) name))))
(string=? (package-name p) name)))))
(fold-packages (lambda (package result)
(append-map (lambda (module) (if (right-package? package)
(filter right-package? (cons package result)
(module-map (lambda (sym var) result))
(variable-ref var)) '()))
module)))
(package-modules)))
...@@ -120,6 +120,13 @@ (define-syntax-rule (dummy-package name* extra-fields ...) ...@@ -120,6 +120,13 @@ (define-syntax-rule (dummy-package name* extra-fields ...)
(and (build-derivations %store (list drv)) (and (build-derivations %store (list drv))
(file-exists? (string-append out "/bin/make"))))))) (file-exists? (string-append out "/bin/make")))))))
(test-eq "fold-packages" hello
(fold-packages (lambda (p r)
(if (string=? (package-name p) "hello")
p
r))
#f))
(test-assert "find-packages-by-name" (test-assert "find-packages-by-name"
(match (find-packages-by-name "hello") (match (find-packages-by-name "hello")
(((? (cut eq? hello <>))) #t) (((? (cut eq? hello <>))) #t)
...@@ -136,6 +143,7 @@ (define-syntax-rule (dummy-package name* extra-fields ...) ...@@ -136,6 +143,7 @@ (define-syntax-rule (dummy-package name* extra-fields ...)
(exit (= (test-runner-fail-count (test-runner-current)) 0)) (exit (= (test-runner-fail-count (test-runner-current)) 0))
;;; Local Variables: ;;; Local Variables:
;;; eval: (put 'test-equal 'scheme-indent-function 2)
;;; eval: (put 'test-assert 'scheme-indent-function 1) ;;; eval: (put 'test-assert 'scheme-indent-function 1)
;;; eval: (put 'dummy-package 'scheme-indent-function 1) ;;; eval: (put 'dummy-package 'scheme-indent-function 1)
;;; End: ;;; End:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment