distro: Move (distro packages libtool) to (distro packages autotools).
[jackhill/guix/guix.git] / guix-import.in
CommitLineData
10226c05
LC
1#!/bin/sh
2# aside from this initial boilerplate, this is actually -*- scheme -*- code
3
4prefix="@prefix@"
5datarootdir="@datarootdir@"
6
7GUILE_LOAD_COMPILED_PATH="@guilemoduledir@:$GUILE_LOAD_COMPILED_PATH"
8export GUILE_LOAD_COMPILED_PATH
9
10main='(module-ref (resolve-interface '\''(guix-import)) '\'guix-import')'
11exec ${GUILE-@GUILE@} -L "@guilemoduledir@" -l "$0" \
12 -c "(apply $main (cdr (command-line)))" "$@"
13!#
14;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
15;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
16;;;
17;;; This file is part of Guix.
18;;;
19;;; Guix is free software; you can redistribute it and/or modify it
20;;; under the terms of the GNU General Public License as published by
21;;; the Free Software Foundation; either version 3 of the License, or (at
22;;; your option) any later version.
23;;;
24;;; Guix is distributed in the hope that it will be useful, but
25;;; WITHOUT ANY WARRANTY; without even the implied warranty of
26;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27;;; GNU General Public License for more details.
28;;;
29;;; You should have received a copy of the GNU General Public License
30;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
31
32(define-module (guix-import)
33 #:use-module (guix ui)
34 #:use-module (guix snix)
35 #:use-module (guix utils)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-11)
38 #:use-module (srfi srfi-26)
39 #:use-module (srfi srfi-37)
40 #:use-module (ice-9 match)
41 #:use-module (ice-9 pretty-print)
42 #:export (guix-import))
43
44\f
45;;;
46;;; Helper.
47;;;
48
49(define (newline-rewriting-port output)
50 "Return an output port that rewrites strings containing the \\n escape
51to an actual newline. This works around the behavior of `pretty-print'
52and `write', which output these as \\n instead of actual newlines,
53whereas we want the `description' field to contain actual newlines
54rather than \\n."
55 (define (write-string str)
56 (let loop ((chars (string->list str)))
57 (match chars
58 (()
59 #t)
60 ((#\\ #\n rest ...)
61 (newline output)
62 (loop rest))
63 ((chr rest ...)
64 (write-char chr output)
65 (loop rest)))))
66
67 (make-soft-port (vector (cut write-char <>)
68 write-string
69 (lambda _ #t) ; flush
70 #f
71 (lambda _ #t) ; close
72 #f)
73 "w"))
74
75\f
76;;;
77;;; Command-line options.
78;;;
79
80(define %default-options
81 '())
82
83(define (show-help)
84 (display (_ "Usage: guix-import NIXPKGS ATTRIBUTE
85Import and convert the Nix expression ATTRIBUTE of NIXPKGS.\n"))
86 (display (_ "
87 -h, --help display this help and exit"))
88 (display (_ "
89 -V, --version display version information and exit"))
90 (newline)
91 (format #t (_ "
92Report bugs to: ~a.~%") "@PACKAGE_BUGREPORT@"))
93
94(define %options
95 ;; Specification of the command-line options.
96 (list (option '(#\h "help") #f #f
97 (lambda args
98 (show-help)
99 (exit 0)))
100 (option '(#\V "version") #f #f
101 (lambda args
102 (show-version-and-exit "guix-import")))))
103
104\f
105;;;
106;;; Entry point.
107;;;
108
109(define (guix-import . args)
110 (define (parse-options)
111 ;; Return the alist of option values.
112 (args-fold args %options
113 (lambda (opt name arg result)
114 (leave (_ "~A: unrecognized option~%") name))
115 (lambda (arg result)
116 (alist-cons 'argument arg result))
117 %default-options))
118
119 (setlocale LC_ALL "")
120 (textdomain "guix")
121 (setvbuf (current-output-port) _IOLBF)
122 (setvbuf (current-error-port) _IOLBF)
123
124 (let* ((opts (parse-options))
125 (args (filter-map (match-lambda
126 (('argument . value)
127 value)
128 (_ #f))
129 (reverse opts))))
130 (match args
131 ((nixpkgs attribute)
132 (let-values (((expr loc)
133 (nixpkgs->guix-package nixpkgs attribute)))
134 (format #t ";; converted from ~a:~a~%~%"
135 (location-file loc) (location-line loc))
136 (pretty-print expr (newline-rewriting-port (current-output-port)))))
137 (_
138 (leave (_ "wrong number of arguments~%"))))))