-
Notifications
You must be signed in to change notification settings - Fork 0
/
bump-version.el
220 lines (175 loc) · 6.98 KB
/
bump-version.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
;;; bump-version.el --- Emacs package which helps bump version.
;; Copyright (C) 2014 Andrey Tykhonov <[email protected]>
;; Author: Andrey Tykhonov <[email protected]>
;; Maintainer: Andrey Tykhonov <[email protected]>
;; URL: https://github.com/atykhonov/emacs-bump-version
;; Version: 0.2.6
;; Keywords: tool
;; This file is NOT part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A little tool which may help to bump version for your projects files.
;;; Installation
;; Assuming that you cloned emacs-bump-version to the
;; `~/.emacs.d/bump-version/' folder. Add the following lines to your
;; `.emacs' file:
;;
;; (add-to-list 'load-path "~/.emacs.d/bump-version/")
;; (require 'bump-version)
;; (global-set-key (kbd "C-c C-b p") 'bump-version-patch)
;; (global-set-key (kbd "C-c C-b i") 'bump-version-minor)
;; (global-set-key (kbd "C-c C-b m") 'bump-version-major)
;;
;;; Configuration
;;
;; You can create config file for your project by means of
;; `bump-version-create-config' command. It just creates `.bump-version.el' file in
;; the `default-directory'. `bump-version-create-config` command creates and opens
;; `.bump-version.el` file. Just edit it (add file names in which project version
;; needs to be updated) and save.
;;
;; Or you can create it manually. Add `.bump-version.el' file to your project root
;; directory. For example, for `bump-version' project it looks like the following:
;;
;; ((:files
;; ("Cask"
;; "bump-version.el"
;; "bump-version-pkg.el"))
;; (:current-version "0.2.1"))
;;
;; Then you can interactively call `bump-version-patch',
;; `bump-version-minor' or `bump-version-major' commands and a version
;; will be updated for the specified files (also for the
;; `.bump-version.el' file).
;;; Contribution
;; Contribution is much welcome! When adding new features, please write tests for them!
;; Thank you! And Enjoy!
;;; Code:
(require 'cl)
(defvar bump-version-format-string "%s.%s.%s")
(defvar bump-version-config-file ".bump-version.el")
(defun bump-version--patch (version)
(format bump-version-format-string
(bump-version--major-version version)
(bump-version--minor-version version)
(+ (bump-version--patch-level version)
1)))
(defun bump-version--minor (version)
(format bump-version-format-string
(bump-version--major-version version)
(+ (bump-version--minor-version version)
1)
0))
(defun bump-version--major (version)
(format bump-version-format-string
(+ (bump-version--major-version version)
1)
0 0))
(defun bump-version--version-to-list (version)
(mapcar
(lambda (n)
(string-to-int n))
(split-string version "\\.")))
(defun bump-version--major-version (version)
(bump-version--version version 0))
(defun bump-version--minor-version (version)
(bump-version--version version 1))
(defun bump-version--patch-level (version)
(bump-version--version version 2))
(defun bump-version--version (version idx)
(nth idx (bump-version--version-to-list version)))
;;;###autoload
(defun bump-version-patch ()
(interactive)
(bump-version-with-config 'bump-version--patch))
(defalias 'bump-version-release 'bump-version-minor)
;;;###autoload
(defun bump-version-minor ()
(interactive)
(bump-version-with-config 'bump-version--minor))
;;;###autoload
(defun bump-version-major ()
(interactive)
(bump-version-with-config 'bump-version--major))
;;;###autoload
(defun bump-version-create-config ()
(interactive)
(let ((config-path (concat default-directory
"/"
bump-version-config-file)))
(if (file-exists-p config-path)
(message ".bump-version.el config already exists!")
(progn
(find-file config-path)
(with-current-buffer (current-buffer)
(insert "((:files
(\"bump-version.el\"))
(:current-version \"0.1.0\"))"))))))
(defun bump-version-with-config (bump-func)
(let* ((files (bump-version--files-to-bump))
(current-version (bump-version--current-version))
(next-version (funcall bump-func current-version))
(config-base-path (bump-version--find-config-base-dir)))
(dolist (file files)
(setq file (concat config-base-path file))
(if (file-exists-p file)
(if (get-file-buffer file)
(with-current-buffer (find-file-noselect file)
(save-excursion
(goto-char (point-min))
(while (search-forward current-version nil t)
(replace-match next-version nil t))))
(with-temp-file file
(insert-file-contents file)
(goto-char (point-min))
(while (search-forward current-version nil t)
(replace-match next-version nil t))))
(message "Bump version error. File %s doesn't exist." file)))))
(defun bump-version--get-default-directory ()
"Just a little wrapper for unit tests."
default-directory)
(defun bump-version--find-config-base-dir ()
(let* ((directory (bump-version--get-default-directory))
(config-path (concat directory "/" bump-version-config-file)))
(when (not (file-exists-p config-path))
(setq config-path nil)
(while (and (null config-path)
(not (string-equal directory "/")))
(setq directory
(file-name-directory
(directory-file-name directory)))
(setq config-path
(concat directory "/" bump-version-config-file))
(when (not (file-exists-p config-path))
(setq config-path nil))))
directory))
(defun bump-version--read-config ()
(let ((config-path (concat (bump-version--find-config-base-dir)
"/"
bump-version-config-file)))
(when (null config-path)
(if (y-or-n-p "Bump version error: %s file didn't find. Find it?")
(setq config-path
(ido-find-file))))
(with-temp-buffer
(insert-file-contents config-path)
(read (buffer-string)))))
(defun bump-version--config-property (property)
(let ((config (bump-version--read-config)))
(car (cdr (assoc-string property config)))))
(defun bump-version--files-to-bump ()
(let ((files (bump-version--config-property ":files")))
(cons bump-version-config-file files)))
(defun bump-version--current-version ()
(bump-version--config-property ":current-version"))
(provide 'bump-version)
;;; bump-version.el ends here