utils: Use '@' for separating package names and version numbers.
[jackhill/guix/guix.git] / guix / scripts / import / hackage.scm
CommitLineData
863af4e1
FB
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix scripts import hackage)
20 #:use-module (guix ui)
21 #:use-module (guix utils)
d8c66da7 22 #:use-module (guix packages)
88981dd3 23 #:use-module (guix scripts)
863af4e1
FB
24 #:use-module (guix import hackage)
25 #:use-module (guix scripts import)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-11)
28 #:use-module (srfi srfi-37)
29 #:use-module (ice-9 match)
30 #:use-module (ice-9 format)
31 #:export (guix-import-hackage))
32
33\f
34;;;
35;;; Command-line options.
36;;;
37
d8c66da7
FB
38(define ghc-default-version
39 (string-append "ghc-" (package-version (@ (gnu packages haskell) ghc))))
40
863af4e1 41(define %default-options
d8c66da7 42 `((include-test-dependencies? . #t)
a4154748 43 (read-from-stdin? . #f)
d8c66da7 44 (cabal-environment . ,`(("impl" . ,ghc-default-version)))))
863af4e1
FB
45
46(define (show-help)
47 (display (_ "Usage: guix import hackage PACKAGE-NAME
48Import and convert the Hackage package for PACKAGE-NAME. If PACKAGE-NAME
1b846da8 49includes a suffix constituted by a at-sign followed by a numerical version (as
863af4e1
FB
50used with Guix packages), then a definition for the specified version of the
51package will be generated. If no version suffix is pecified, then the
52generated package definition will correspond to the latest available
53version.\n"))
54 (display (_ "
88981dd3 55 -e ALIST, --cabal-environment=ALIST
a4154748
FB
56 specify environment for Cabal evaluation"))
57 (display (_ "
863af4e1
FB
58 -h, --help display this help and exit"))
59 (display (_ "
a4154748
FB
60 -s, --stdin read from standard input"))
61 (display (_ "
8e01e300 62 -t, --no-test-dependencies don't include test-only dependencies"))
863af4e1
FB
63 (display (_ "
64 -V, --version display version information and exit"))
65 (newline)
66 (show-bug-report-information))
67
68(define %options
69 ;; Specification of the command-line options.
70 (cons* (option '(#\h "help") #f #f
71 (lambda args
72 (show-help)
73 (exit 0)))
74 (option '(#\V "version") #f #f
75 (lambda args
76 (show-version-and-exit "guix import hackage")))
77 (option '(#\t "no-test-dependencies") #f #f
78 (lambda (opt name arg result)
79 (alist-cons 'include-test-dependencies? #f
80 (alist-delete 'include-test-dependencies?
81 result))))
a4154748
FB
82 (option '(#\s "stdin") #f #f
83 (lambda (opt name arg result)
84 (alist-cons 'read-from-stdin? #t
85 (alist-delete 'read-from-stdin?
86 result))))
87 (option '(#\e "cabal-environment") #t #f
88 (lambda (opt name arg result)
89 (alist-cons 'cabal-environment (read/eval arg)
90 (alist-delete 'cabal-environment
91 result))))
863af4e1
FB
92 %standard-import-options))
93
94\f
95;;;
96;;; Entry point.
97;;;
98
99(define (guix-import-hackage . args)
100 (define (parse-options)
101 ;; Return the alist of option values.
102 (args-fold* args %options
103 (lambda (opt name arg result)
104 (leave (_ "~A: unrecognized option~%") name))
105 (lambda (arg result)
106 (alist-cons 'argument arg result))
107 %default-options))
108
a4154748
FB
109 (define (run-importer package-name opts error-fn)
110 (let ((sexp (hackage->guix-package
111 package-name
112 #:include-test-dependencies?
113 (assoc-ref opts 'include-test-dependencies?)
114 #:port (if (assoc-ref opts 'read-from-stdin?)
115 (current-input-port)
116 #f)
117 #:cabal-environment
118 (assoc-ref opts 'cabal-environment))))
119 (unless sexp (error-fn))
120 sexp))
121
863af4e1
FB
122 (let* ((opts (parse-options))
123 (args (filter-map (match-lambda
124 (('argument . value)
125 value)
126 (_ #f))
127 (reverse opts))))
a4154748
FB
128 (if (assoc-ref opts 'read-from-stdin?)
129 (match args
130 (()
131 (run-importer "stdin" opts
132 (lambda ()
2c59cbc2
LC
133 (leave (_ "failed to import cabal file \
134from standard input~%")))))
a4154748
FB
135 ((many ...)
136 (leave (_ "too many arguments~%"))))
137 (match args
138 ((package-name)
139 (run-importer package-name opts
140 (lambda ()
5453de3d
LC
141 (leave (_ "failed to download cabal file \
142for package '~a'~%")
143 package-name))))
a4154748
FB
144 (()
145 (leave (_ "too few arguments~%")))
146 ((many ...)
147 (leave (_ "too many arguments~%")))))))