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
| | ;;; 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
(guix import github) ;; latest-release
(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
)
(define go-name* "github.com/gohugoio/hugo") ;; for tests
(define (go-name->url go-name)
(string-append "https://" go-name))
(define (go-name->tarball go-name version)
(string-append (go-name->url go-name) "/archive/v" version
".tar.gz"))
;;; Possible remove @@ if upstream exports the symbols
(define (go-name->name go-name)
((@@ (guix import github) github-repository)
(go-name->url go-name)))
;;; Slow; accesses the network
(define (latest-release go-name)
((@@ (guix import github) latest-released-version)
(go-name->url go-name)
(go-name->name go-name)))
;;; Slow; downloads the url from network;
(define (url->store url)
(with-store store
(download-to-store store
url)))
;;; Slow; download the source tarball from network and returns base32
;;; nix-hash
(define (go-name->sha256 go-name version)
(utils:guix-hash-url (url->store (go-name->tarball go-name version))))
;;; Towards go-name->synopsis,description
(define (go-name->readme go-name)
(string-append "https://raw.githubusercontent.com"
(substring go-name
(string-length "github.com"))
"/master/"
"README.md"))
;;; Slow; network access
(define (go-name->readme-string go-name)
"Slow; network access."
(call-with-input-file (url->store (go-name->readme go-name))
(lambda (port) (get-string-n port 4096))))
;;; Maybe try to match the first sentence.
(define (go-name->synopsis go-name readme-string)
(string-append (go-name->name go-name)
(substring readme-string 0 100)))
;;; Maybe try to match the the next two sentences.
(define (go-name->description go-name readme-string)
(string-append (go-name->name go-name)
(substring readme-string 100 300)))
;;; go list -f '{{ join .Deps "\n" }}',recursively find dependencies
;;; go list -f '{{ join .Imports "\n" }}' ,non recursive
(define (go-name->inputs go-name)
(let ((tmp (tmpnam)))
(and (zero? (system (string-append
"go list -f '{{ join .Deps \"\\n\" }}'"
" " go-name " > " tmp)))
(string-split (string-trim-both (call-with-input-file tmp get-string-all))
(string->char-set "\n")))))
;;; For inputs
(define (alist-sexp alist)
(let ((magic (lambda (x)
(list x
(string->symbol (string-append x))))))
(if (and (list? alist) (not (equal? '() alist)))
(map magic
(if (list? (car alist))
(map car alist)
alist))
'())))
(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
(alist-sexp (go-name->inputs go-name)))
(synopsis
(go-name->synopsis go-name readme-string))
(description
(go-name->description go-name readme-string))
;; TODO: license
)))
(define (filter-newlines string)
(string-filter (lambda (x) (not (equal? #\ x))) 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
|