ui: Rename '_' to 'G_'.
[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)
69daee23 47 (display (G_ "Usage: guix import hackage PACKAGE-NAME
863af4e1 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"))
69daee23 54 (display (G_ "
88981dd3 55 -e ALIST, --cabal-environment=ALIST
a4154748 56 specify environment for Cabal evaluation"))
69daee23 57 (display (G_ "
863af4e1 58 -h, --help display this help and exit"))
69daee23 59 (display (G_ "
a4154748 60 -s, --stdin read from standard input"))
69daee23 61 (display (G_ "
8e01e300 62 -t, --no-test-dependencies don't include test-only dependencies"))
69daee23 63 (display (G_ "
863af4e1
FB
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)
69daee23 104 (leave (G_ "~A: unrecognized option~%") name))
863af4e1
FB
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 ()
69daee23 133 (leave (G_ "failed to import cabal file \
2c59cbc2 134from standard input~%")))))
a4154748 135 ((many ...)
69daee23 136 (leave (G_ "too many arguments~%"))))
a4154748
FB
137 (match args
138 ((package-name)
139 (run-importer package-name opts
140 (lambda ()
69daee23 141 (leave (G_ "failed to download cabal file \
5453de3d
LC
142for package '~a'~%")
143 package-name))))
a4154748 144 (()
69daee23 145 (leave (G_ "too few arguments~%")))
a4154748 146 ((many ...)
69daee23 147 (leave (G_ "too many arguments~%")))))))