import.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  4. ;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com>
  5. ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
  6. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  7. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix scripts import)
  24. #:use-module (guix ui)
  25. #:use-module (guix scripts)
  26. #:use-module (guix utils)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-11)
  29. #:use-module (srfi srfi-26)
  30. #:use-module (srfi srfi-37)
  31. #:use-module (ice-9 format)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 pretty-print)
  34. #:export (%standard-import-options
  35. guix-import))
  36. ;;;
  37. ;;; Helper.
  38. ;;;
  39. (define (newline-rewriting-port output)
  40. "Return an output port that rewrites strings containing the \\n escape
  41. to an actual newline. This works around the behavior of `pretty-print'
  42. and `write', which output these as \\n instead of actual newlines,
  43. whereas we want the `description' field to contain actual newlines
  44. rather than \\n."
  45. (define (write-string str)
  46. (let loop ((chars (string->list str)))
  47. (match chars
  48. (()
  49. #t)
  50. ((#\\ #\n rest ...)
  51. (newline output)
  52. (loop rest))
  53. ((chr rest ...)
  54. (write-char chr output)
  55. (loop rest)))))
  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. ;;; Command line options.
  65. ;;;
  66. (define %standard-import-options '())
  67. ;;;
  68. ;;; Entry point.
  69. ;;;
  70. (define importers '("gnu" "pypi" "cpan" "hackage" "stackage" "egg" "elpa"
  71. "gem" "go" "cran" "crate" "texlive" "json" "opam"
  72. "minetest"))
  73. (define (resolve-importer name)
  74. (let ((module (resolve-interface
  75. `(guix scripts import ,(string->symbol name))))
  76. (proc (string->symbol (string-append "guix-import-" name))))
  77. (module-ref module proc)))
  78. (define (show-help)
  79. (display (G_ "Usage: guix import IMPORTER ARGS ...
  80. Run IMPORTER with ARGS.\n"))
  81. (newline)
  82. (display (G_ "IMPORTER must be one of the importers listed below:\n"))
  83. (newline)
  84. (format #t "~{ ~a~%~}" importers)
  85. (display (G_ "
  86. -h, --help display this help and exit"))
  87. (display (G_ "
  88. -V, --version display version information and exit"))
  89. (newline)
  90. (show-bug-report-information))
  91. (define-command (guix-import . args)
  92. (category packaging)
  93. (synopsis "import a package definition from an external repository")
  94. (match args
  95. (()
  96. (format (current-error-port)
  97. (G_ "guix import: missing importer name~%")))
  98. ((or ("-h") ("--help"))
  99. (show-help)
  100. (exit 0))
  101. ((or ("-V") ("--version"))
  102. (show-version-and-exit "guix import"))
  103. ((importer args ...)
  104. (if (member importer importers)
  105. (let ((print (lambda (expr)
  106. (pretty-print expr (newline-rewriting-port
  107. (current-output-port))
  108. #:max-expr-width 80))))
  109. (match (apply (resolve-importer importer) args)
  110. ((and expr (or ('package _ ...)
  111. ('let _ ...)
  112. ('define-public _ ...)))
  113. (print expr))
  114. ((? list? expressions)
  115. (for-each (lambda (expr)
  116. (print expr)
  117. (newline))
  118. expressions))
  119. (x
  120. (leave (G_ "'~a' import failed~%") importer))))
  121. (let ((hint (string-closest importer importers #:threshold 3)))
  122. (report-error (G_ "~a: invalid importer~%") importer)
  123. (when hint
  124. (display-hint
  125. (format #f (G_ "Did you mean @code{~a}?~%") hint)))
  126. (exit 1))))))