import.scm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2014, 2020-2023 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, 2022 Ricardo Wurmus <rekado@elephly.net>
  6. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  7. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  8. ;;; Copyright © 2022 Philip McGrath <philip@philipmcgrath.com>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (guix scripts import)
  25. #:use-module (guix ui)
  26. #:use-module (guix scripts)
  27. #:use-module (guix read-print)
  28. #:use-module (guix utils)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (ice-9 format)
  31. #:use-module (ice-9 match)
  32. #:export (%standard-import-options
  33. guix-import))
  34. ;;;
  35. ;;; Command line options.
  36. ;;;
  37. (define %standard-import-options '())
  38. ;;;
  39. ;;; Entry point.
  40. ;;;
  41. (define importers '("gnu" "pypi" "cpan" "hackage" "stackage" "egg" "elpa"
  42. "gem" "go" "cran" "crate" "texlive" "json" "opam"
  43. "minetest" "elm" "hexpm"))
  44. (define (resolve-importer name)
  45. (let ((module (resolve-interface
  46. `(guix scripts import ,(string->symbol name))))
  47. (proc (string->symbol (string-append "guix-import-" name))))
  48. (module-ref module proc)))
  49. (define (show-help)
  50. (display (G_ "Usage: guix import IMPORTER ARGS ...
  51. Run IMPORTER with ARGS.\n"))
  52. (newline)
  53. (display (G_ "IMPORTER must be one of the importers listed below:\n"))
  54. (newline)
  55. (format #t "~{ ~a~%~}" importers)
  56. (display (G_ "
  57. -h, --help display this help and exit"))
  58. (display (G_ "
  59. -V, --version display version information and exit"))
  60. (newline)
  61. (show-bug-report-information))
  62. (define-command (guix-import . args)
  63. (category packaging)
  64. (synopsis "import a package definition from an external repository")
  65. (match args
  66. (()
  67. (format (current-error-port)
  68. (G_ "guix import: missing importer name~%")))
  69. ((or ("-h") ("--help"))
  70. (show-help)
  71. (exit 0))
  72. ((or ("-V") ("--version"))
  73. (show-version-and-exit "guix import"))
  74. ((importer args ...)
  75. (if (member importer importers)
  76. (let ((print (lambda (expr)
  77. (pretty-print-with-comments (current-output-port) expr))))
  78. (match (apply (resolve-importer importer) args)
  79. ((and expr (or ('package _ ...)
  80. ('let _ ...)
  81. ('define-public _ ...)))
  82. (print expr))
  83. ((? list? expressions)
  84. (for-each (lambda (expr)
  85. (print expr)
  86. ;; Two newlines: one after the closing paren, and
  87. ;; one to leave a blank line.
  88. (newline) (newline))
  89. expressions))
  90. (x
  91. (leave (G_ "'~a' import failed~%") importer))))
  92. (let ((hint (string-closest importer importers #:threshold 3)))
  93. (report-error (G_ "~a: invalid importer~%") importer)
  94. (when hint
  95. (display-hint (G_ "Did you mean @code{~a}?~%") hint))
  96. (exit 1))))))