crate.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016 David Craven <david@craven.ch>
  4. ;;; Copyright © 2019, 2020 Martin Becze <mjbecze@riseup.net>
  5. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts import crate)
  22. #:use-module (guix ui)
  23. #:use-module (guix utils)
  24. #:use-module (guix scripts)
  25. #:use-module (guix import crate)
  26. #:use-module (guix scripts import)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-11)
  29. #:use-module (srfi srfi-37)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 format)
  32. #:export (guix-import-crate))
  33. ;;;
  34. ;;; Command-line options.
  35. ;;;
  36. (define %default-options
  37. '())
  38. (define (show-help)
  39. (display (G_ "Usage: guix import crate PACKAGE-NAME
  40. Import and convert the crates.io package for PACKAGE-NAME.\n"))
  41. (display (G_ "
  42. -r, --recursive import packages recursively"))
  43. (newline)
  44. (display (G_ "
  45. -h, --help display this help and exit"))
  46. (display (G_ "
  47. -V, --version display version information and exit"))
  48. (newline)
  49. (show-bug-report-information))
  50. (define %options
  51. ;; Specification of the command-line options.
  52. (cons* (option '(#\h "help") #f #f
  53. (lambda args
  54. (show-help)
  55. (exit 0)))
  56. (option '(#\V "version") #f #f
  57. (lambda args
  58. (show-version-and-exit "guix import crate")))
  59. (option '(#\r "recursive") #f #f
  60. (lambda (opt name arg result)
  61. (alist-cons 'recursive #t result)))
  62. %standard-import-options))
  63. ;;;
  64. ;;; Entry point.
  65. ;;;
  66. (define (guix-import-crate . args)
  67. (define (parse-options)
  68. ;; Return the alist of option values.
  69. (parse-command-line args %options (list %default-options)
  70. #:build-options? #f))
  71. (let* ((opts (parse-options))
  72. (args (filter-map (match-lambda
  73. (('argument . value)
  74. value)
  75. (_ #f))
  76. (reverse opts))))
  77. (match args
  78. ((spec)
  79. (define-values (name version)
  80. (package-name->name+version spec))
  81. (if (assoc-ref opts 'recursive)
  82. (crate-recursive-import name #:version version)
  83. (let ((sexp (crate->guix-package name #:version version #:include-dev-deps? #t)))
  84. (unless sexp
  85. (leave (G_ "failed to download meta-data for package '~a'~%")
  86. (if version
  87. (string-append name "@" version)
  88. name)))
  89. (list sexp))))
  90. (()
  91. (leave (G_ "too few arguments~%")))
  92. ((many ...)
  93. (leave (G_ "too many arguments~%"))))))