deprecation.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 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/alias
  24. warn-about-deprecation))
  25. ;;; Commentary:
  26. ;;;
  27. ;;; Provide a mechanism to mark bindings as deprecated.
  28. ;;;
  29. ;;; Code:
  30. (define* (warn-about-deprecation variable properties
  31. #:key replacement)
  32. (let ((location (and properties (source-properties->location properties))))
  33. (if replacement
  34. (warning location (G_ "'~a' is deprecated, use '~a' instead~%")
  35. variable replacement)
  36. (warning location (G_ "'~a' is deprecated~%")
  37. variable))))
  38. (define-syntax define-deprecated
  39. (lambda (s)
  40. "Define a deprecated variable or procedure, along these lines:
  41. (define-deprecated foo bar 42)
  42. (define-deprecated old new)
  43. (define-deprecated (baz x y) qux (qux y x))
  44. This will write a deprecation warning to GUIX-WARNING-PORT."
  45. (syntax-case s ()
  46. ((_ (proc formals ...) replacement body ...)
  47. #'(define-deprecated proc replacement
  48. (lambda* (formals ...) body ...)))
  49. ((_ variable replacement exp)
  50. (identifier? #'variable)
  51. (with-syntax ((real (datum->syntax
  52. #'variable
  53. (symbol-append '%
  54. (syntax->datum #'variable)
  55. '/deprecated))))
  56. #`(begin
  57. (define real
  58. (begin
  59. (lambda () replacement) ;just to ensure it's bound
  60. exp))
  61. (define-syntax variable
  62. (lambda (s)
  63. (warn-about-deprecation 'variable (syntax-source s)
  64. #:replacement 'replacement)
  65. (syntax-case s ()
  66. ((_ args (... ...))
  67. #'(real args (... ...)))
  68. (id
  69. (identifier? #'id)
  70. #'real)))))))
  71. ((_ variable alias)
  72. (identifier? #'alias)
  73. #'(define-deprecated variable alias alias)))))
  74. (define-syntax-rule (define-deprecated/alias deprecated replacement)
  75. "Define as an alias a deprecated variable, procedure, or macro, along
  76. these lines:
  77. (define-deprecated/alias nix-server? store-connection?)
  78. where 'nix-server?' is the deprecated name for 'store-connection?'.
  79. This will write a deprecation warning to GUIX-WARNING-PORT."
  80. (define-syntax deprecated
  81. (lambda (s)
  82. (warn-about-deprecation 'deprecated (syntax-source s)
  83. #:replacement 'replacement)
  84. (syntax-case s ()
  85. ((_ args (... ...))
  86. #'(replacement args (... ...)))
  87. (id
  88. (identifier? #'id)
  89. #'replacement)))))