bump-version 3.92 KB
Newer Older
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
1
#!/bin/sh
2
3
4
":" ; exec sbcl --script "$0" "$@" ; exit # -*- Lisp -*-
;;; Really runs on any decent Common Lisp implementation

5
6
(load (make-pathname :name "prelude" :type "lisp" :defaults *load-pathname*)
  :verbose nil :print nil)
7

8
(in-package :fare-utils)
9

10
11
(asdf-debug)

12
13
14
15
16
17
(defparameter *versioned-files*
  '(("version.lisp-expr" "\"" "\"")
    ("asdf.asd" "  :version \"" "\" ;; to be automatically updated by bin/bump-revision")
    ("header.lisp" "This is ASDF " ": Another System Definition Facility.")
    ("upgrade.lisp" "   (asdf-version \"" "\")")))

18
(defvar *adir* (asdf:system-relative-pathname :asdf nil))
19

20
(defun afile (x) (subpathname *adir* x))
21
22
23
24

(defparameter *version-file*
  (afile "version.lisp-expr"))

25
26
(defparameter *old-version* nil)
(defparameter *new-version* nil)
27
28

(defun next-version (v)
29
  (let ((pv (parse-version v 'error)))
30
31
32
    (incf (third pv))
    (unparse-version pv)))

33
(defun version-from-file ()
34
  (safe-read-file-form *version-file*))
35
36

(defun versions-from-argv (argv)
37
38
39
40
41
42
43
44
45
  (labels ((check (old new)
             (parse-version old 'error)
             (parse-version new 'error)
             (values old new)))
    (ecase (length argv)
      ((2) (check (second argv) (first argv)))
      ((1) (check (version-from-file) (first argv)))
      ((0) (let ((old (version-from-file)))
             (check old (next-version old)))))))
46
47
48

(multiple-value-setq (*old-version* *new-version*)
  (versions-from-argv *command-line-arguments*))
49

50
(a "Bumping ASDF version from " *old-version* " to " *new-version*)
51
52
53
54
55

(deftype byte-vector () '(array (unsigned-byte 8) (*)))

(defun maybe-replace-file (file transformer
                           &key (reader 'read-file-string)
56
                             (writer nil) (comparator 'equalp)
57
                             (external-format *utf-8-external-format*))
58
  (format t "Transforming file ~A... " (file-namestring file))
59
60
  (let* ((old-contents (funcall reader file))
         (new-contents (funcall transformer old-contents)))
61
    (if (funcall comparator old-contents new-contents)
62
        (format t "no changes needed!~%")
63
64
65
66
67
68
        (let ((written-contents
                (if writer
                    (with-output (s ())
                      (funcall writer s new-contents))
                    new-contents)))
          (check-type written-contents (or string (byte-vector)))
69
70
          (clobber-file-with-vector file written-contents :external-format external-format)
          (format t "done.~%")))))
71

72
(defun version-transformer (new-version file prefix suffix &optional dont-warn)
73
74
75
76
77
78
79
80
81
  (let* ((qprefix (cl-ppcre:quote-meta-chars prefix))
         (versionrx "([0-9]+(\\.[0-9]+)+)")
         (qsuffix (cl-ppcre:quote-meta-chars suffix))
         (regex (strcat "(" qprefix ")(" versionrx ")(" qsuffix ")"))
         (replacement
           (constantly (strcat prefix new-version suffix))))
    (lambda (text)
      (multiple-value-bind (new-text foundp)
          (cl-ppcre:regex-replace regex text replacement)
82
        (unless (or foundp dont-warn)
83
84
          (warn "Missing version in ~A" (file-namestring file)))
        (values new-text foundp)))))
85

86
87
88
89
90
91
92
93
94
95
(defun transform-file (new-version file prefix suffix)
  (maybe-replace-file (afile file) (version-transformer new-version file prefix suffix)))

(defun transform-files ()
  (loop :for f :in *versioned-files* :do (apply 'transform-file *new-version* f)))

(defun test-transform-file (new-version file prefix suffix)
  (let ((lines (read-file-lines (afile file))))
    (dolist (l lines (progn (warn "Couldn't find a match in ~A" file) nil))
      (multiple-value-bind (new-text foundp)
96
          (funcall (version-transformer new-version file prefix suffix t) l)
97
98
99
100
101
102
103
        (when foundp
          (format t "Found a match:~%  ==> ~A~%Replacing with~%  ==> ~A~%~%"
                  l new-text)
          (return t))))))
(defun z () (apply 'test-transform-file *new-version* (first *versioned-files*)))

(transform-files)
104
105
106

(a "Re-generate ASDF with bumped version")
(asdf:build-system :asdf/generate)