~elais/orange

orange/guix/import/go.scm -rw-r--r-- 8.5 KiB
5506f669Elais Player Remove duplicate package entries 14 days ago
                                                                                
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
(define-module (guix import go)
  #:use-module (gcrypt hash)
  #:use-module (guix base32)
  #:use-module ((guix download) #:prefix download:)
  #:use-module (guix http-client)
  #:use-module (guix import json)
  #:use-module (guix import utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix memoization)
  #:use-module (guix modules)
  #:use-module (guix packages)
  #:use-module (guix store)
  #:use-module (guix utils)
  #:use-module (gnu packages)
  #:use-module (ice-9 match)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 textual-ports)
  #:use-module (json)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-2)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-41)
  #:use-module (srfi srfi-71)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (web uri))

(module-autoload! (current-module)
		  '(semver) '(string->semver semver->string semver<? semver=?))
(module-autoload! (current-module)
		  '(semver ranges) '(string->semver-range semver-range-contains?))

(define %proxy "https://proxy.golang.org/")

;;; Modules. A module is a package.
(define-json-mapping <module> make-module module?
  json->module
  (module  module-module  "Module" json->module-dependency)        ; string
  (require module-require "Require"
           (lambda (vector)
             (if (symbol? vector) '()
                 (map json->module-dependency (vector->list vector)))))
  (exclude module-exclude "Exclude")
  (replace module-replace "Replace")) ; string

;; Module dependency.
(define-json-mapping <module-dependency> make-module-dependency module-dependency?
  json->module-dependency
  (path module-dependency-path "Path" string-downcase)
  (version module-dependency-version "Version"
           (lambda (val)
             (if (eq? val #f) #f (string->semver val))))
  (indirect module-dependency-indirect "Indirect"))

(define (golang-dependencies->names deps)
  (match deps
    (((names _ ...) ...)
     (map symbol->string names))))

(define (lookup-module path)
  "Look up PATH on https://proxy.golang.org and return the corresponding
`<module>' record."
  (let* ((separated-path (string-split path #\@))
         (proxy-path (string-append
                      %proxy
                      (car separated-path)
                      "/@v/"
                      (cadr separated-path)
                      ".mod")))
    (call-with-temporary-output-file
     (lambda (temp port)
       (begin ((@ (guix import utils) url-fetch) proxy-path temp)
              (let* ((json-port
                      (open-input-pipe
                            (string-append
                             "guix environment --ad-hoc go -- go mod edit -json"
                             " "
                             temp)))
                     (mod-json (get-string-all json-port)))
                (close-pipe json-port)
                (json->module mod-json)))))))

(define lookup-module* (memoize lookup-module))



;;;
;;; Converting modules to Guix packages.
;;;

(define (maybe-arguments arguments)
  (match arguments
    (()
     '())
    ((args ...)
     `((arguments (,'quasiquote ,args))))))

(define (version->semver-prefix version)
  "Return the version up to and including the first non-zero part"
  (first
   (map match:substring
        (list-matches "^(0+\\.){,2}[0-9]+" version))))

(define* (golang-name->package-name path)
  "Return a Guix package `name' for the go module with the PATH."
  (define (clean name)
    (string-map
     (lambda (chr) (if (or (char=? chr #\.) (char=? chr #\/)) #\- chr)) name))
  (guix-name "go-" (clean path)))


(define (golang-versions path)
  (let* ((port (http-fetch (string->uri (string-append %proxy
                                                       path
                                                       "/@v/list"))))
         (versions
          (sort
           (map (lambda (ver)
                  (string->semver ver))
                (filter (lambda (str)
                          (not (string-null? str)))
                        (string-split (get-string-all port) #\newline)))
           semver<?)))
    (close-port port)
    versions))

(define golang-versions* (memoize golang-versions))

(define (golang-latest-version path)
  (let ((versions (golang-versions* path)))
    (semver->string (last versions))))

(define* (module-source-url path proxy #:key version)
  "Return the source URL of the package described by the strings PATH and
VERSION via PROXY."
  (string-append proxy path "/@v/v" (golang-latest-version path) ".zip"))

(define* (golang->sexp module version)
  (define (format-inputs inputs)
    (map
     (match-lambda
      ((name version)
       (list (golang-name->package-name name)
             (version->semver-prefix version))))
     inputs))

  (define path (module-dependency-path (module-module module)))

  (define version* (semver->string version))

  (define golang-dependencies
    (map (lambda (dep)
           (let* ((dep-path (module-dependency-path dep)))
             (golang-name->package-name dep-path)))
         (module-require module)))

  (define (find-package-version name range)
    (let* ((semver-range (string->semver-range (string-append "~" range)))
           (versions
            (sort
             (filter (lambda (version)
                       (semver-range-contains? semver-range version))
                     (map (lambda (pkg)
                            (string->semver (package-version pkg)))
                          (find-packages-by-name
                           (golang-name->package-name name))))
             semver<?)))
      (and (not (null-list? versions))
           (semver->string (last versions)))))

  (define dependencies
    (map (lambda (dep)
           (list dep (list 'unquote (string->symbol dep))))
         golang-dependencies))

  (define (dependency-name+version dep)
    (let* ((name (module-dependency-path dep))
           (req  (module-dependency-version dep))
           (existing-version (find-package-version name (semver->string req))))
      (if existing-version
          (list name existing-version)
          (list name (semver->string req)))))


  (define (sort-map-dependencies deps)
    (sort (map dependency-name+version
               (filter (lambda (dep)
                         (not (string-null?
                               (module-dependency-path dep))))
                       deps))
          (match-lambda* (((name _) ...)
                          (apply string-ci<? name)))))

  (define (maybe-inputs input-type inputs)
    (match inputs
      (()
       '())
      ((inputs ...)
       (list (list input-type
                   (list 'quasiquote inputs))))))

  (and module version*
       (let* ((dependencies-names (sort-map-dependencies (module-require module)))
              (zipfile (with-store store
                        (download:download-to-store
                         store
                         (module-source-url path %proxy version*)))))
         (values
          `(package
             (name ,(golang-name->package-name path))
             (version ,version*)
             (source (origin
                       (method url-fetch)
                       (uri (string-append
                             ,%proxy
                             ,path
                             "/@v/v"
                             version
                             ".zip"))
                       (sha256
                        (base32
                         ,(if zipfile
                              (bytevector->nix-base32-string (file-sha256 zipfile))
                              "failed t download module")))))
             (build-system go-build-system)
             ,@(maybe-inputs 'propagated-inputs dependencies)
             (home-page "")
             (synopsis "")
             (description "")
             (license #f))
          dependencies-names))))

(define* (golang->guix-package path #:key (version #f) repo)
  "Fetch the module PATH and produce a Guix package S-expression."

  (define module
    (lookup-module* (string-append path "@v" (golang-latest-version path))))

  (define version-number
    (string->semver
     (or version (golang-latest-version path))))


  (golang->sexp module version-number))

(define golang->guix-package*
  (memoize golang->guix-package))

(define* (golang-recursive-import path #:key (version #f))
  (recursive-import path
                    #:repo '()
                    #:repo->guix-package golang->guix-package
                    #:version version
                    #:guix-name golang-name->package-name))