weather: Fix pasto in --version output.
[jackhill/guix/guix.git] / guix / scripts / import.scm
CommitLineData
233e7676 1;;; GNU Guix --- Functional package management for GNU
7047133f 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
1b3e9685 3;;; Copyright © 2014 David Thompson <davet@gnu.org>
10226c05 4;;;
233e7676 5;;; This file is part of GNU Guix.
10226c05 6;;;
233e7676 7;;; GNU Guix is free software; you can redistribute it and/or modify it
10226c05
LC
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
233e7676 12;;; GNU Guix is distributed in the hope that it will be useful, but
10226c05
LC
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
233e7676 18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
10226c05 19
e49951eb 20(define-module (guix scripts import)
10226c05 21 #:use-module (guix ui)
10226c05
LC
22 #:use-module (guix utils)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-11)
25 #:use-module (srfi srfi-26)
26 #:use-module (srfi srfi-37)
1b3e9685 27 #:use-module (ice-9 format)
10226c05
LC
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 pretty-print)
1b3e9685
DT
30 #:export (%standard-import-options
31 guix-import))
10226c05
LC
32
33\f
34;;;
35;;; Helper.
36;;;
37
38(define (newline-rewriting-port output)
39 "Return an output port that rewrites strings containing the \\n escape
40to an actual newline. This works around the behavior of `pretty-print'
41and `write', which output these as \\n instead of actual newlines,
42whereas we want the `description' field to contain actual newlines
43rather than \\n."
44 (define (write-string str)
45 (let loop ((chars (string->list str)))
46 (match chars
47 (()
48 #t)
49 ((#\\ #\n rest ...)
50 (newline output)
51 (loop rest))
52 ((chr rest ...)
53 (write-char chr output)
54 (loop rest)))))
55
56 (make-soft-port (vector (cut write-char <>)
57 write-string
58 (lambda _ #t) ; flush
59 #f
60 (lambda _ #t) ; close
61 #f)
62 "w"))
63
64\f
65;;;
1b3e9685 66;;; Command line options.
10226c05
LC
67;;;
68
1b3e9685
DT
69(define %standard-import-options '())
70
71\f
72;;;
73;;; Entry point.
74;;;
75
bc5844d1 76(define importers '("gnu" "nix" "pypi" "cpan" "hackage" "stackage" "elpa" "gem"
fb1db385 77 "cran" "crate" "texlive" "json"))
1b3e9685
DT
78
79(define (resolve-importer name)
80 (let ((module (resolve-interface
81 `(guix scripts import ,(string->symbol name))))
82 (proc (string->symbol (string-append "guix-import-" name))))
83 (module-ref module proc)))
10226c05
LC
84
85(define (show-help)
69daee23 86 (display (G_ "Usage: guix import IMPORTER ARGS ...
1b3e9685
DT
87Run IMPORTER with ARGS.\n"))
88 (newline)
69daee23 89 (display (G_ "IMPORTER must be one of the importers listed below:\n"))
2a4e2e4b 90 (newline)
1b3e9685 91 (format #t "~{ ~a~%~}" importers)
69daee23 92 (display (G_ "
10226c05 93 -h, --help display this help and exit"))
69daee23 94 (display (G_ "
10226c05
LC
95 -V, --version display version information and exit"))
96 (newline)
3441e164 97 (show-bug-report-information))
10226c05 98
10226c05 99(define (guix-import . args)
1b3e9685
DT
100 (match args
101 (()
102 (format (current-error-port)
69daee23 103 (G_ "guix import: missing importer name~%")))
1b3e9685
DT
104 ((or ("-h") ("--help"))
105 (show-help)
106 (exit 0))
107 (("--version")
108 (show-version-and-exit "guix import"))
109 ((importer args ...)
110 (if (member importer importers)
d68ba5f4
RW
111 (let ((print (lambda (expr)
112 (pretty-print expr (newline-rewriting-port
113 (current-output-port))))))
114 (match (apply (resolve-importer importer) args)
115 ((and expr ('package _ ...))
116 (print expr))
117 ((? list? expressions)
118 (for-each (lambda (expr)
119 (print expr)
120 (newline))
121 expressions))
122 (x
69daee23
LC
123 (leave (G_ "'~a' import failed~%") importer))))
124 (leave (G_ "~a: invalid importer~%") importer)))))