import: Add 'cran' importer.
[jackhill/guix/guix.git] / guix / scripts / import.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 David Thompson <davet@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
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 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
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
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (guix scripts import)
21 #:use-module (guix ui)
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)
27 #:use-module (ice-9 format)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 pretty-print)
30 #:export (%standard-import-options
31 guix-import))
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
40 to an actual newline. This works around the behavior of `pretty-print'
41 and `write', which output these as \\n instead of actual newlines,
42 whereas we want the `description' field to contain actual newlines
43 rather 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 ;;;
66 ;;; Command line options.
67 ;;;
68
69 (define %standard-import-options '())
70
71 \f
72 ;;;
73 ;;; Entry point.
74 ;;;
75
76 (define importers '("gnu" "nix" "pypi" "cpan" "hackage" "elpa" "gem" "cran"))
77
78 (define (resolve-importer name)
79 (let ((module (resolve-interface
80 `(guix scripts import ,(string->symbol name))))
81 (proc (string->symbol (string-append "guix-import-" name))))
82 (module-ref module proc)))
83
84 (define (show-help)
85 (display (_ "Usage: guix import IMPORTER ARGS ...
86 Run IMPORTER with ARGS.\n"))
87 (newline)
88 (display (_ "IMPORTER must be one of the importers listed below:\n"))
89 (newline)
90 (format #t "~{ ~a~%~}" importers)
91 (display (_ "
92 -h, --help display this help and exit"))
93 (display (_ "
94 -V, --version display version information and exit"))
95 (newline)
96 (show-bug-report-information))
97
98 (define (guix-import . args)
99 (match args
100 (()
101 (format (current-error-port)
102 (_ "guix import: missing importer name~%")))
103 ((or ("-h") ("--help"))
104 (show-help)
105 (exit 0))
106 (("--version")
107 (show-version-and-exit "guix import"))
108 ((importer args ...)
109 (if (member importer importers)
110 (let ((expr (apply (resolve-importer importer) args)))
111 (pretty-print expr (newline-rewriting-port (current-output-port))))
112 (format (current-error-port)
113 (_ "guix import: invalid importer~%"))))))