unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 4f1aee26d65db6932367c8375cad33e611f73a3f 9946 bytes (raw)
name: guix/import/go.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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 swedebugia <swedebugia@riseup.net>
;;;
;;; 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 go)
  #:use-module (ice-9 hash-table)
  #:use-module (ice-9 match)
  #:use-module (ice-9 textual-ports)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-2) ;and-let
  #:use-module (guix utils)
  #:use-module ((guix git-download) #:prefix download:)
  #:use-module (guix import github)
  #:use-module (guix import json)
  #:use-module (guix import utils)
  #:use-module (guix packages)
  #:use-module (guix http-client)
  #:use-module (web uri))

;;; Commentary:
;;; This utilizes the https://go-search.org/infoapi API.
;;; This API contains no licenses or versions. We fetch those from github when
;;; possible.

;;; Code:

(define (go-name->url name)
  "Takes a go-name on the form github.com/andyleap/go-ssb and turns it into
https://github.com/andyleap/go-ssb"
  (string-append "https://" name))

;; from opam.scm - should probably be factored out to utils.scm
(define (substitute-char str what with)
  (string-join (string-split str what) with))

(define (go-name->guix-name name)
  "Takes a go-name e.g. on the form github.com/x/y and turns it into
go-github-com-x-y"
  (substitute-char
   (substitute-char
    (cond
     ;;((equal? name "ocamlfind") "ocaml-findlib")
     ;;((string-prefix? "ocaml" name) name)
     ((string-prefix? "github.com/" name) (string-append "go-github-com-" (substring name 11)))
     ((string-prefix? "golang.org/x/" name) (string-append "go-golang-org-" (substring name 11)))
     ((string-prefix? "cryptoscope.co/go/" name) (string-append "go-cryptoscope-co-" (substring name 11)))
     (else (string-append "go-" name)))
    #\_ "-")
   #\/ "-"))
;;(display (go-name->guix-name "golang.org/x/text/transform"))

(define (fetch-data name)
  "Fetches data about imports and description"
  (json-fetch (string-append "https://go-search.org/api"
                             "?action=package&id=" name)))
;;(display (hash-table->alist (fetch-data "golang.org/x/text/transform")))

(define (synopsis name)
  (and-let* ((data (fetch-data name)))
    (if (assoc-ref data "Synopsis")
        (assoc-ref data "Synopsis")
        ;; If synopsis is empty get the description instead
        (assoc-ref data "Description"))))

;;(display (synopsis "golang.org/x/text/transform"))

;; Github projects enable us to get the license and readme
(define (github-url? url)
  (->bool (string-prefix? "https://github.com/" url)))

(define (string->license name)
  "Get SPDX-id from github if github-url"
  (and-let* ((url (go-name->url name))
            (github-url? url)
            (data (fetch-license url))
            (hasht (assoc-ref data "license"))
            (str (string-downcase (assoc-ref hasht "spdx_id"))))
    (cond
     ((equal? str "gpl-3.0") '(license:gpl-3))
     (else `(,string-append "license:" ,str)))))

;;(display (string->license "github.com/andyleap/go-ssb"))

(define (readme name)
  "We get the first 1000 characters for the description"
  (and-let* ((url (go-name->url name))
            (github-url? url))
    (get-string-n (fetch-readme url) 1000)))

(define (description name)
  (and-let* ((data (fetch-data name)))
    (if (assoc-ref data "Synopsis")
        ;; Synopsis is non-empty.
        (if (assoc-ref data "Description")
            (assoc-ref data "Description")
            ;; Description is empty
            (readme name))
        ;; Synopsis is empty and the description from GSAPI has been used as
        ;; synopsis, get the readme instead
        (readme name))))

;;(display (description "golang.org/x/text/transform"))

;; Versions are tricky because the go-ecosystem does not rely on them at
;; all. We get the latest released or tagged version from github and fall
;; backto the latest commit.
(define (version name)
  "Get the latest release or tag if any."
  (and-let* ((url (go-name->url name))
             (github-url? url))
    (latest-released-version url name)))

;;(display (version "github.com/andyleap/go-ssb"))

(define (commit name)
  "Get latest commit-id"
  (and-let* ((url (go-name->url name))
             (github-url? url))
    (fetch-latest-commit url)))

;;(display (commit "github.com/andyleap/go-ssb"))

(define (dependencies name)
  (and-let* ((data (fetch-data name)))
    ;; Join with (assoc-ref data "TestImports")?
    (assoc-ref data "Imports")))

;;(display (dependencies "golang.org/x/text/transform"))

(define (test-dependencies name)
  (and-let* ((data (fetch-data name)))
    ;; Join with (assoc-ref data "TestImports")?
    (assoc-ref data "TestImports")))

;; this is from ocaml.scm
(define (dependencies->inputs dependencies)
  "Transform the list of dependencies in a list of inputs."
  (if (not dependencies)
    '()
    (map (lambda (input)
           (list input (list 'unquote (string->symbol input))))
         (map go-name->guix-name dependencies))))

;;(display (dependencies->inputs (dependencies "github.com/andyleap/go-ssb")))

(define (go->guix-package name)
  (let ((version (version name)))
    (if (equal? version #t)
        ;; Got release or tag
        (let ((source-url (go-name->url name))
              (commit version)
              (inputs (dependencies->inputs (dependencies name)))
              (synopsis (synopsis name))
              (description (description name)))
          ;; This is broken because of git-fetch from git-download does not at
          ;; all work like the similar url-fetch-procedure.
          (call-with-temporary-output-file
           (lambda (temp port)
             (and (download:git-fetch (download:git-reference
                                       (url source-url)
                                       (commit commit))
                                      "sha256" "dummy-hash")
                  `(package
                     (name ,(go-name->guix-name name))
                     (version ,version)
                     (source
                      (origin
                        (method git-fetch)
                        (uri (git-reference
                              (url ,source-url)
                              (commit ,commit)))
                        (file-name (git-file-name name version))
                        (sha256 (base32 ,(guix-hash-url temp)))))
                     (build-system go-build-system)
                     ,@(if (null? inputs)
                           '()
                           `((inputs ,(list 'quasiquote inputs))))
                     (home-page ,source-url)
                     (synopsis ,synopsis)
                     (description ,description)
                     (license ,@(string->license name)))))))
        ;; No release or tag, fall back to latest commit
        (let ((source-url (go-name->url name))
              (commit (commit name))
              (inputs (dependencies->inputs (dependencies name)))
              (synopsis (synopsis name))
              (description (description name)))
          ;; This is broken because of git-fetch from git-download does not at
          ;; all work like the similar url-fetch-procedure.
          (call-with-temporary-output-file
           (lambda (temp port)
             (and (download:git-fetch (download:git-reference
                                       (url source-url)
                                       (commit commit))
                                      "sha256" "dummy-hash")
                  `(package
                     (name ,(go-name->guix-name name))
                     (version ,version)
                     (source
                      (origin
                        (method git-fetch)
                        (uri (git-reference
                              (url ,source-url)
                              (commit ,commit)))
                        (file-name (git-file-name name version))
                        (sha256 (base32 ,(guix-hash-url temp)))))
                     (build-system go-build-system)
                     ,@(if (null? inputs)
                           '()
                           `((inputs ,(list 'quasiquote inputs))))
                     (home-page ,source-url)
                     (synopsis ,synopsis)
                     (description ,description)
                     (license ,@(string->license name))))))))))

(go->guix-package "github.com/gogo/protobuf")

;; Debug
#;
(display (let* ((name "github.com/gogo/protobuf")
               (url (go-name->url name))
               (commit name))
           (call-with-temporary-output-file
           (lambda (temp port)
             (download:git-fetch (download:git-reference
                                  (url url)
                                  (commit commit))
                                 "sha256" commit temp)))))


#;
(display (let* ((name "github.com/gogo/protobuf")
               (url (go-name->url name))
               (commit name))
           (download:git-fetch (download:git-reference
                                (url url)
                                (commit commit))
"sha256" commit name)))

;; Both of these returns procedures like this:#<procedure 1b72f50 at guix/git-download.scm:145:2 (state)>
;; How do we get it to return something we can compute the hash from?

debug log:

solving 4f1aee26d ...
found 4f1aee26d in https://yhetil.org/guix-devel/4c2f5f76-b431-57ce-3697-ac2cbb48f395@riseup.net/

applying [1/1] https://yhetil.org/guix-devel/4c2f5f76-b431-57ce-3697-ac2cbb48f395@riseup.net/
diff --git a/guix/import/go.scm b/guix/import/go.scm
new file mode 100644
index 000000000..4f1aee26d

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

index at:
100644 4f1aee26d65db6932367c8375cad33e611f73a3f	guix/import/go.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).