unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob c0679990e14e5236b0a2cd621266176fec94c856 7509 bytes (raw)
name: guix/import/golang.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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 by Amar Singh <nly@disroot.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; This program 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.
;;;
;;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (guix import golang))
(use-modules
 (srfi srfi-1) ;; fold
 (ice-9 rdelim) ;; read-string
 (guix import github)  ;; latest-release
 (guix utils) ;; string-replace-substring
 (guix memoization)        ;; memoize network operations
 (guix download)       ;; download-to-store
 ((guix import utils) #:prefix utils:)   ;; hash
 (guix packages)       ;; packages
 (guix build-system)   ;; build-system printer
 (guix build-system go)  ;; go-build-system
 (guix store)          ;; with-store
 (gnu packages golang) ;; inherit (simple) go package
 (ice-9 textual-ports) ;; to parse readme.md
 (ice-9 popen) ;; open-input-ouput-pipe
 (web uri) ;; uri->string
 (srfi srfi-26) ;; cut
 )

(define-public go-name* "github.com/gohugoio/hugo") ;; for tests

(define* (go-name->url go-name #:rest args)
  (if (string-contains go-name ".")
      (uri->string (string->uri (apply string-append
                                       "https://" go-name args)))
      #f))

(define (go-name->tarball go-name version)
  (go-name->url go-name "/archive/v"
                version ".tar.gz"))

(define* (string-replace-substrings string substrings
                                    #:optional (replacement "-"))
  (if (null-list? substrings)
      string
      ((cut string-replace-substring <> (car substrings) replacement)
       (string-replace-substrings string (cdr substrings)))))

;;; Possible remove @@ if upstream exports the symbols
(define (go-name->guix-name go-name)
  (string-append "go-"
                 (string-replace-substrings go-name '("." "/") "-")))

;;; Slow; accesses the network; memoized
(define latest-release
  (memoize
   (lambda (go-name)
     ((@@ (guix import github) latest-released-version)
      (go-name->url go-name)
      (go-name->guix-name go-name)))))

;;; Slow; downloads the url from network; memoized
(define url->store
  (@@ (guix import cran) download))

;;; Slow; download src tarball from network, returns base32 nix-hash;
;;; memoized
(define (go-name->sha256 go-name version)
  (utils:guix-hash-url (url->store (go-name->tarball go-name version))))

;;; Slow; network access; memoized
(define go-name->readme-string
  (memoize
   (lambda (go-name)
     (define (go-name->readme go-name)
       (go-name->url "raw.githubusercontent.com"
                     ;; TODO, detect the domain
                     (substring go-name
                                (string-length "github.com"))
                     "/master/"
                     "README.md"))
     (call-with-input-file (url->store (go-name->readme go-name))
       read-string))))

;;; Maybe try to match the first sentence.
(define (go-name->synopsis go-name)
  (substring (go-name->readme-string go-name) 0 100))

;;; Maybe try to match the the next two sentences.
(define (go-name->description go-name)
  (substring (go-name->readme-string go-name) 100 300))
(go-name->description go-name*)

(define shell-command
  (lambda* (command #:rest args)
    (let* ((cmd (string-join (cons command (delete #f (delete '() args))) " "))
           (port (open-input-output-pipe cmd))
           (result (read-string port))
           (exit-code (close-pipe port)))
      (and (zero? exit-code)
           (string-split (string-trim-right result) #\newline)))))

(define go-name->inputs
  (lambda (go-name)
    (let ((recursive-depends "-f '{{ join .Deps \"\\n\" }}'")
          (direct-depends "-f '{{ join .Imports \"\\n\" }}'")
          (go-command (car (shell-command "which go"))))
      (shell-command go-command "list" direct-depends go-name))))

;;; License
(define (string->license license-string)
  ((@@ (guix import cran) string->license) (string-upcase license-string)))

;;; For inputs
(define format-inputs
  (@@ (guix import cran) format-inputs))

(define-public (make-go-package go-name)
  ;; Do the expensive operations only once; query network for latest
  ;; version
  (let* ((version (latest-release go-name))
         (sha256 (go-name->sha256 go-name version))
         (readme-string (go-name->readme-string go-name)))
    (package (inherit go-github-com-alsm-ioprogress)
      (name
       (string-append "go-" go-name))
      (version version)
      (source
       (origin (method url-fetch)
               (uri (go-name->tarball go-name version))
               (sha256 (base32 sha256))))
      (home-page
       (go-name->url go-name))
      (build-system
        go-build-system)
      (arguments
       `(#:import-path ,go-name))
      ;; TODO: make inputs into (unquote ..) form
      (inputs
       (format-inputs (map go-name->guix-name (go-name->inputs go-name))))
      (synopsis (go-name->synopsis go-name))
      (description (go-name->description go-name))
      ;; TODO: license
      )))

(define (filter-newlines string)
  (string-filter (lambda (x) (not (equal? x #\newline))) string))

;;; Ask the upstream to export variable?
(define bv->nix-base32 (@@ (guix packages)
                           bytevector->nix-base32-string))

(define (origin-sexp origin)
  `(origin
     (method url-fetch)
     (uri ,(origin-uri origin))
     (sha256 (base32 ,(bv->nix-base32 (origin-sha256 origin))))
     (file-name ,(origin-file-name origin))
     (patches ,(origin-patches origin))
     (snippet ,(origin-snippet origin))
     (patch-flags ,(origin-patch-flags origin))
     (patch-inputs ,(origin-patch-inputs origin))
     (modules ,(origin-modules origin))
     (patch-guile ,(origin-patch-guile origin))))

(define (build-system-sexp build-system)
  (symbol-append (build-system-name build-system) '-build-system))

(define-public (package-sexp package)
  `(package
     (name ,(package-name package))
     (version ,(package-version package))
     (source ,(origin-sexp (package-source package)))
     (home-page ,(package-home-page package))
     (build-system ,(build-system-sexp (package-build-system package)))
     (arguments ,(package-arguments package))
     (synopsis ,(filter-newlines (package-synopsis package)))
     (description ,(filter-newlines (package-description package)))
     (inputs ,(alist-sexp (package-inputs package)))
     (native-inputs ,(alist-sexp (package-native-inputs package)))
     (propagated-inputs ,(alist-sexp (package-propagated-inputs package)))))


;;; STATUS
;;; 1. latest-release  DONE
;;; 1.b latest-commit PENDING/STALLED
;;; 2. go-name->name DONE
;;; 4. go-name->url DONE
;;; 4.b go-name->tarball DONE
;;; 5. go-name->sha256 (go-name version) DONE
;;; 6. go-name->synopsis DONE
;;; 7. go-name->description DONE
;;; 6-7.b try to extract sentences. TODO
;;; 8. go-name->license TODO
;;; 9. go-name->inputs DONE
;;; 9.b. inputs alist TODO
;;; 10. package-sexp DONE
;;; 10.a origin-sexp DONE

;;; golang.scm ends here

debug log:

solving c0679990e1 ...
found c0679990e1 in https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
found e0ffca4b42 in https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
found e6ef62a3b4 in https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
found e11d1c2230 in https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
found f9872ee8e3 in https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
found ad822f6b69 in https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/

applying [1/6] https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
diff --git a/guix/import/golang.scm b/guix/import/golang.scm
new file mode 100644
index 0000000000..ad822f6b69


applying [2/6] https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
diff --git a/guix/import/golang.scm b/guix/import/golang.scm
index ad822f6b69..f9872ee8e3 100644


applying [3/6] https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
diff --git a/guix/import/golang.scm b/guix/import/golang.scm
index f9872ee8e3..e11d1c2230 100644


applying [4/6] https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
diff --git a/guix/import/golang.scm b/guix/import/golang.scm
index e11d1c2230..e6ef62a3b4 100644


applying [5/6] https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
diff --git a/guix/import/golang.scm b/guix/import/golang.scm
index e6ef62a3b4..e0ffca4b42 100644


applying [6/6] https://yhetil.org/guix-patches/87lfzhjx1s.fsf@disroot.org/
diff --git a/guix/import/golang.scm b/guix/import/golang.scm
index e0ffca4b42..c0679990e1 100644

6:11: trailing whitespace.
;;; 
6:16: trailing whitespace.
;;; 
6:21: trailing whitespace.
;;; 
Checking patch guix/import/golang.scm...
Applied patch guix/import/golang.scm cleanly.
Checking patch guix/import/golang.scm...
Applied patch guix/import/golang.scm cleanly.
Checking patch guix/import/golang.scm...
Applied patch guix/import/golang.scm cleanly.
Checking patch guix/import/golang.scm...
Applied patch guix/import/golang.scm cleanly.
Checking patch guix/import/golang.scm...
Applied patch guix/import/golang.scm cleanly.
Checking patch guix/import/golang.scm...
Applied patch guix/import/golang.scm cleanly.
warning: 3 lines add whitespace errors.

index at:
100644 c0679990e14e5236b0a2cd621266176fec94c856	guix/import/golang.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).