unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob b4710c6fb93736f077e5c5462af99f5d9aa05885 10840 bytes (raw)
name: guix/import/composer.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix import composer)
  #:use-module (ice-9 match)
  #:use-module (json)
  #:use-module (gcrypt hash)
  #:use-module (guix base32)
  #:use-module (guix build git)
  #:use-module (guix build utils)
  #:use-module (guix build-system)
  #:use-module (guix import json)
  #:use-module (guix import utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix memoization)
  #:use-module (guix packages)
  #:use-module (guix serialization)
  #:use-module (guix upstream)
  #:use-module (guix utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-26)
  #:export (composer->guix-package
            %composer-updater
            composer-recursive-import

            %composer-base-url))

(define %composer-base-url
  (make-parameter "https://repo.packagist.org"))

;; XXX adapted from (guix scripts hash)
(define (file-hash file select? recursive?)
  ;; Compute the hash of FILE.
  (if recursive?
      (let-values (((port get-hash) (open-sha256-port)))
        (write-file file port #:select? select?)
        (force-output port)
        (get-hash))
      (call-with-input-file file port-sha256)))

;; XXX taken from (guix scripts hash)
(define (vcs-file? file stat)
  (case (stat:type stat)
    ((directory)
     (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
    ((regular)
     ;; Git sub-modules have a '.git' file that is a regular text file.
     (string=? (basename file) ".git"))
    (else
     #f)))

(define (fix-version version)
  "Return a fixed version from a version string.  For instance, v10.1 -> 10.1"
  (cond
    ((string-prefix? "version" version)
     (if (char-set-contains? char-set:digit (string-ref version 7))
         (substring version 7)
         (substring version 8)))
    ((string-prefix? "v" version)
     (substring version 1))
    (else version)))

(define (latest-version versions)
  (fold (lambda (a b) (if (version>? (fix-version a) (fix-version b)) a b))
        (car versions) versions))

(define (json->require dict)
  (if dict
      (let loop ((result '()) (require dict))
        (match require
          (() result)
          ((((? (cut string-contains <> "/") name) . _)
             require ...)
           (loop (cons name result) require))
          ((_ require ...) (loop result require))
          (_ result)))
      '()))

(define-json-mapping <composer-source> make-composer-source composer-source?
  json->composer-source
  (type      composer-source-type)
  (url       composer-source-url)
  (reference composer-source-reference))

(define-json-mapping <composer-package> make-composer-package composer-package?
  json->composer-package
  (description composer-package-description)
  (homepage    composer-package-homepage)
  (source      composer-package-source "source" json->composer-source)
  (name        composer-package-name "name" php-package-name)
  (version     composer-package-version "version" fix-version)
  (require     composer-package-require "require" json->require)
  (dev-require composer-package-dev-require "require-dev" json->require)
  (license     composer-package-license "license"
               (lambda (vector)
                 (map string->license (vector->list vector)))))

(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 (%composer-base-url) "/p/" name ".json"))))
    (if package
        (let* ((packages (assoc-ref package "packages"))
               (package (or (assoc-ref packages name) package))
               (versions (filter
                           (lambda (version)
                             (and (not (string-contains version "dev"))
                                  (not (string-contains version "beta"))))
                           (map car package)))
               (version (or (if (null? version) #f version)
                            (latest-version versions))))
          (assoc-ref package version))
        #f)))

(define (php-package-name name)
  "Given the NAME of a package on Packagist, return a Guix-compliant name for
the package."
  (let ((name (string-join (string-split name #\/) "-")))
    (if (string-prefix? "php-" name)
        (snake-case name)
        (string-append "php-" (snake-case name)))))

(define (make-php-sexp composer-package)
  "Return the `package' s-expression for a PHP package for the given
COMPOSER-PACKAGE."
  (let* ((source (composer-package-source composer-package))
         (dependencies (map php-package-name
                            (composer-package-require composer-package)))
         (dev-dependencies (map php-package-name
                                (composer-package-dev-require composer-package)))
         (git? (equal? (composer-source-type source) "git")))
    ((if git? call-with-temporary-directory call-with-temporary-output-file)
     (lambda* (temp #:optional port)
       (and (if git?
                (begin
                  (mkdir-p temp)
                  (git-fetch (composer-source-url source)
                             (composer-source-reference source)
                             temp))
                (url-fetch (composer-source-url source) temp))
            `(package
               (name ,(composer-package-name composer-package))
               (version ,(composer-package-version composer-package))
               (source (origin
                         ,@(if git?
                               `((method git-fetch)
                                 (uri (git-reference
                                        (url ,(composer-source-url source))
                                        (commit ,(composer-source-reference source))))
                                 (file-name (git-file-name name version))
                                 (sha256
                                   (base32
                                     ,(bytevector->nix-base32-string
                                       (file-hash temp (negate vcs-file?) #t)))))
                               `((method url-fetch)
                                 (uri ,(composer-source-url source))
                                 (sha256 (base32 ,(guix-hash-url temp)))))))
               (build-system composer-build-system)
               ,@(if (null? dependencies)
                     '()
                     `((inputs
                        (list ,(map string->symbol dependencies)))))
               ,@(if (null? dev-dependencies)
                     '()
                     `((native-inputs
                        (list ,(map string->symbol dev-dependencies)))))
               (synopsis "")
               (description ,(composer-package-description composer-package))
               (home-page ,(composer-package-homepage composer-package))
               (license ,(or (composer-package-license composer-package)
                             'unknown-license!))))))))

(define composer->guix-package
  (memoize
   (lambda* (package-name #:key version #:allow-other-keys)
     "Fetch the metadata for PACKAGE-NAME from packagist.org, and return the
`package' s-expression corresponding to that package, or #f on failure."
     (let ((package (composer-fetch package-name version)))
       (and package
            (let* ((package (json->composer-package package))
                   (dependencies-names (composer-package-require package))
                   (dev-dependencies-names (composer-package-dev-require package)))
              (values (make-php-sexp package)
                      (append dependencies-names dev-dependencies-names))))))))

(define (guix-name->composer-name name)
  "Given a guix package name, return the name of the package in Packagist."
  (if (string-prefix? "php-" name)
      (let ((components (string-split (substring name 4) #\-)))
        (match components
          ((namespace name ...)
           (string-append namespace "/" (string-join name "-")))))
      name))

(define (guix-package->composer-name package)
  "Given a Composer PACKAGE built from Packagist, return the name of the
package in Packagist."
  (let ((upstream-name (assoc-ref
                         (package-properties package)
                         'upstream-name))
        (name (package-name package)))
    (if upstream-name
      upstream-name
      (guix-name->composer-name name))))

(define (string->license str)
  "Convert the string STR into a license object."
  (or (spdx-string->license str)
      (match str
        ("GNU LGPL" 'license:lgpl2.0)
        ("GPL" 'license:gpl3)
        ((or "BSD" "BSD License") 'license:bsd-3)
        ((or "MIT" "MIT license" "Expat license") 'license:expat)
        ("Public domain" 'license:public-domain)
        ((or "Apache License, Version 2.0" "Apache 2.0") 'license:asl2.0)
        (_ 'unknown-license!))))

(define (php-package? package)
  "Return true if PACKAGE is a PHP package from Packagist."
  (and
    (eq? (build-system-name (package-build-system package)) 'composer)
    (string-prefix? "php-" (package-name package))))

(define (latest-release package)
  "Return an <upstream-source> for the latest release of PACKAGE."
  (let* ((php-name (guix-package->composer-name package))
         (metadata (composer-fetch php-name))
         (package (json->composer-package metadata))
         (version (composer-package-version package))
         (url (composer-source-url (composer-package-source package))))
    (upstream-source
     (package (package-name package))
     (version version)
     (urls (list url)))))

(define %composer-updater
  (upstream-updater
   (name 'composer)
   (description "Updater for Composer packages")
   (pred php-package?)
   (import latest-release)))

(define* (composer-recursive-import package-name #:optional version)
  (recursive-import package-name
                    #:version version
                    #:repo->guix-package composer->guix-package
                    #:guix-name php-package-name))

debug log:

solving b4710c6fb9 ...
found b4710c6fb9 in https://yhetil.org/guix-patches/810bc98dafaceec34fb584732fc19b7f836ae110.1695724304.git.ngraves@ngraves.fr/
found 2ce7206ef9 in https://yhetil.org/guix-patches/ea519115fce9bd5788fb476d59bf4a2f42b66334.1695724304.git.ngraves@ngraves.fr/
found 3acbbecf82 in https://yhetil.org/guix-patches/20231102151725.31362-6-ngraves@ngraves.fr/ ||
	https://yhetil.org/guix-patches/20231102151523.30581-6-ngraves@ngraves.fr/ ||
	https://yhetil.org/guix-patches/5e275b0faafb6897f995d2c37d39984fca5eb9e1.1695724304.git.ngraves@ngraves.fr/
found 177dc63092 in https://yhetil.org/guix-patches/20231102151523.30581-5-ngraves@ngraves.fr/ ||
	https://yhetil.org/guix-patches/20231102151725.31362-5-ngraves@ngraves.fr/ ||
	https://yhetil.org/guix-patches/7fa9b1098369b75b162944d5db9a6d646af115ad.1695724304.git.ngraves@ngraves.fr/
found c152f402bb in https://yhetil.org/guix-patches/20231102151523.30581-2-ngraves@ngraves.fr/ ||
	https://yhetil.org/guix-patches/20231102151725.31362-2-ngraves@ngraves.fr/ ||
	https://yhetil.org/guix-patches/6abfbb4d4d57642fea6bae5835ff0d365de7e24a.1695724304.git.ngraves@ngraves.fr/

applying [1/5] https://yhetil.org/guix-patches/20231102151523.30581-2-ngraves@ngraves.fr/
diff --git a/guix/import/composer.scm b/guix/import/composer.scm
new file mode 100644
index 0000000000..c152f402bb

Checking patch guix/import/composer.scm...
Applied patch guix/import/composer.scm cleanly.

skipping https://yhetil.org/guix-patches/20231102151725.31362-2-ngraves@ngraves.fr/ for c152f402bb
skipping https://yhetil.org/guix-patches/6abfbb4d4d57642fea6bae5835ff0d365de7e24a.1695724304.git.ngraves@ngraves.fr/ for c152f402bb
index at:
100644 c152f402bbb9e308de875d2e406e5b4df1b2ed9e	guix/import/composer.scm

applying [2/5] https://yhetil.org/guix-patches/20231102151523.30581-5-ngraves@ngraves.fr/
diff --git a/guix/import/composer.scm b/guix/import/composer.scm
index c152f402bb..177dc63092 100644

Checking patch guix/import/composer.scm...
Applied patch guix/import/composer.scm cleanly.

skipping https://yhetil.org/guix-patches/20231102151725.31362-5-ngraves@ngraves.fr/ for 177dc63092
skipping https://yhetil.org/guix-patches/7fa9b1098369b75b162944d5db9a6d646af115ad.1695724304.git.ngraves@ngraves.fr/ for 177dc63092
index at:
100644 177dc6309247d295ed1391e9c5f6ffe9768af29f	guix/import/composer.scm

applying [3/5] https://yhetil.org/guix-patches/20231102151725.31362-6-ngraves@ngraves.fr/
diff --git a/guix/import/composer.scm b/guix/import/composer.scm
index 177dc63092..3acbbecf82 100644

Checking patch guix/import/composer.scm...
Applied patch guix/import/composer.scm cleanly.

skipping https://yhetil.org/guix-patches/20231102151523.30581-6-ngraves@ngraves.fr/ for 3acbbecf82
skipping https://yhetil.org/guix-patches/5e275b0faafb6897f995d2c37d39984fca5eb9e1.1695724304.git.ngraves@ngraves.fr/ for 3acbbecf82
index at:
100644 3acbbecf828a8cb5f7e8977a0b5a4ac9acf594fe	guix/import/composer.scm

applying [4/5] https://yhetil.org/guix-patches/ea519115fce9bd5788fb476d59bf4a2f42b66334.1695724304.git.ngraves@ngraves.fr/
diff --git a/guix/import/composer.scm b/guix/import/composer.scm
index 3acbbecf82..2ce7206ef9 100644


applying [5/5] https://yhetil.org/guix-patches/810bc98dafaceec34fb584732fc19b7f836ae110.1695724304.git.ngraves@ngraves.fr/
diff --git a/guix/import/composer.scm b/guix/import/composer.scm
index 2ce7206ef9..b4710c6fb9 100644

Checking patch guix/import/composer.scm...
Applied patch guix/import/composer.scm cleanly.
Checking patch guix/import/composer.scm...
Applied patch guix/import/composer.scm cleanly.

index at:
100644 b4710c6fb93736f077e5c5462af99f5d9aa05885	guix/import/composer.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).