import: github: Improve readability.

* guix/import/github.scm (latest-released-version): Use any and cond instead
of a recursive loop and an if-else ladder respectively.
This commit is contained in:
Arun Isaac 2019-01-21 01:43:09 +05:30
parent 6b7ea49bed
commit cb5fe915d2
No known key found for this signature in database
GPG Key ID: 2E25EE8B61802BB3
1 changed files with 26 additions and 29 deletions

View File

@ -183,35 +183,32 @@ API when using a GitHub token")
API. This may be fixed by using an access token and setting the environment API. This may be fixed by using an access token and setting the environment
variable GUIX_GITHUB_TOKEN, for instance one procured from variable GUIX_GITHUB_TOKEN, for instance one procured from
https://github.com/settings/tokens")) https://github.com/settings/tokens"))
(let loop ((releases (any
(match (remove pre-release? json) (lambda (release)
(() json) ; keep everything
(releases releases))))
(match releases
(() ;empty release list
#f)
((release . rest) ;one or more releases
(let ((tag (or (hash-ref release "tag_name") ;a "release" (let ((tag (or (hash-ref release "tag_name") ;a "release"
(hash-ref release "name"))) ;a tag (hash-ref release "name"))) ;a tag
(name-length (string-length package-name))) (name-length (string-length package-name)))
(cond
;; some tags include the name of the package e.g. "fdupes-1.51" ;; some tags include the name of the package e.g. "fdupes-1.51"
;; so remove these ;; so remove these
(if (and (< name-length (string-length tag)) ((and (< name-length (string-length tag))
(string=? (string-append package-name "-") (string=? (string-append package-name "-")
(substring tag 0 (+ name-length 1)))) (substring tag 0 (+ name-length 1))))
(substring tag (+ name-length 1)) (substring tag (+ name-length 1)))
;; some tags start with a "v" e.g. "v0.25.0" ;; some tags start with a "v" e.g. "v0.25.0"
;; where some are just the version number ;; where some are just the version number
(if (string-prefix? "v" tag) ((string-prefix? "v" tag)
(substring tag 1) (substring tag 1))
;; Finally, reject tags that don't start with a digit: ;; Finally, reject tags that don't start with a digit:
;; they may not represent a release. ;; they may not represent a release.
(if (and (not (string-null? tag)) ((and (not (string-null? tag))
(char-set-contains? char-set:digit (char-set-contains? char-set:digit
(string-ref tag 0))) (string-ref tag 0)))
tag tag)
(loop rest))))))))))) (else #f))))
(match (remove pre-release? json)
(() json) ; keep everything
(releases releases))))))
(define (latest-release pkg) (define (latest-release pkg)
"Return an <upstream-source> for the latest release of PKG." "Return an <upstream-source> for the latest release of PKG."