deprecation.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix deprecation)
  19. #:use-module (guix i18n)
  20. #:use-module (guix diagnostics)
  21. #:autoload (guix utils) (source-properties->location)
  22. #:export (define-deprecated
  23. define-deprecated/public
  24. define-deprecated/alias
  25. warn-about-deprecation))
  26. ;;; Commentary:
  27. ;;;
  28. ;;; Provide a mechanism to mark bindings as deprecated.
  29. ;;;
  30. ;;; Code:
  31. (define* (warn-about-deprecation variable properties
  32. #:key replacement)
  33. (let ((location (and properties (source-properties->location properties))))
  34. (if replacement
  35. (warning location (G_ "'~a' is deprecated, use '~a' instead~%")
  36. variable replacement)
  37. (warning location (G_ "'~a' is deprecated~%")
  38. variable))))
  39. (define-syntax public (syntax-rules ())) ;private syntactic keyword
  40. (define-syntax define-deprecated
  41. (lambda (s)
  42. "Define a deprecated variable or procedure, along these lines:
  43. (define-deprecated foo bar 42)
  44. (define-deprecated old new)
  45. (define-deprecated (baz x y) qux (qux y x))
  46. This will write a deprecation warning to GUIX-WARNING-PORT."
  47. (syntax-case s ()
  48. ((_ (proc formals ...) replacement body ...)
  49. #'(define-deprecated proc replacement
  50. (lambda* (formals ...) body ...)))
  51. ((_ variable replacement exp)
  52. #'(define-deprecated private variable replacement exp))
  53. ((_ visibility variable replacement exp)
  54. (identifier? #'variable)
  55. (with-syntax ((real (datum->syntax
  56. #'variable
  57. (symbol-append '%
  58. (syntax->datum #'variable)
  59. '/deprecated))))
  60. #`(begin
  61. (define real
  62. (begin
  63. (lambda () replacement) ;just to ensure it's bound
  64. exp))
  65. (define-syntax variable
  66. (lambda (s)
  67. (warn-about-deprecation 'variable (syntax-source s)
  68. #:replacement 'replacement)
  69. (syntax-case s ()
  70. ((_ args (... ...))
  71. #'(real args (... ...)))
  72. (id
  73. (identifier? #'id)
  74. #'real))))
  75. ;; When asking for public visibility, export both REAL and
  76. ;; VARIABLE. Exporting REAL is useful when defining deprecated
  77. ;; packages: there must be a public variable bound to a package
  78. ;; so that the (guix discover) machinery finds it.
  79. #,(if (free-identifier=? #'visibility #'public)
  80. #'(export real variable)
  81. #'(begin)))))
  82. ((_ variable alias)
  83. (identifier? #'alias)
  84. #'(define-deprecated variable alias alias)))))
  85. (define-syntax-rule (define-deprecated/public body ...)
  86. "Like 'define/deprecated', but export all the newly introduced bindings."
  87. (define-deprecated public body ...))
  88. (define-syntax-rule (define-deprecated/alias deprecated replacement)
  89. "Define as an alias a deprecated variable, procedure, or macro, along
  90. these lines:
  91. (define-deprecated/alias nix-server? store-connection?)
  92. where 'nix-server?' is the deprecated name for 'store-connection?'.
  93. This will write a deprecation warning to GUIX-WARNING-PORT."
  94. (define-syntax deprecated
  95. (lambda (s)
  96. (warn-about-deprecation 'deprecated (syntax-source s)
  97. #:replacement 'replacement)
  98. (syntax-case s ()
  99. ((_ args (... ...))
  100. #'(replacement args (... ...)))
  101. (id
  102. (identifier? #'id)
  103. #'replacement)))))