Subject: [bug#42338] [PATCH 01/34] guix: import: Add composer importer.

From: Ludovic Courtès <[email-masked]>
Date: Mon, 07 Sep 2020 16:06:13 +0200

Hi Julien,

There’s a lot of interesting work in here!  I’m not familiar with PHP;
I’ll just make a bird’s eye review.

Julien Lepiller <[email-masked]> skribis:

> * guix/import/composer.scm: New file.
> * guix/scripts/import/composer.scm: New file.
> * Makefile.am: Add them.
> * guix/scripts/import.scm: Add composer importer.

Please add tests and a mention in “Invoking guix import” in the manual.

For tests, a strategy that I think works well is that used in
tests/cpan.scm, where we spawn an HTTP server to mock the real one.

> +(define* (composer-fetch name #:optional version)
> +  "Return an alist representation of the Composer metadata for the package NAME,
> +or #f on failure."
> +  (let ((package (json-fetch
> +                   (string-append "https://repo.packagist.org/p/" name ".json"))))
> +    (if package
> +        (let* ((packages (assoc-ref package "packages"))
> +               (package (assoc-ref packages name))
> +               (versions (filter
> +                           (lambda (version)
> +                             (and (not (string-contains version "dev"))
> +                                  (not (string-contains version "beta"))))
> +                           (map car package)))
> +               (versions (map
> +                           (lambda (version)
> +                             (cons (fix-version version) version))
> +                           versions))
> +               (version (or (if (null? version) #f version)
> +                            (latest-version (map car versions)))))
> +          (assoc-ref package (assoc-ref versions version)))
> +        #f)))

I recommend using ‘define-json-mapping’ instead of browsing alists: it’s
less error-prone, hides the JSON details away, and leads to more
readable code.  The pypi, crates, cpan importers use it.

Thanks!

Ludo’.