1 ;;; inversion.el --- When you need something in version XX.XX
3 ;;; Copyright (C) 2002-2003, 2005-2018 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; Keeping track of rapidly developing software is a tough thing to
27 ;; do, especially if you want to have co-dependent packages which all
28 ;; move at different rates.
30 ;; This library provides a framework for specifying version numbers
31 ;; and (as side effect) have a flexible way of getting a desired feature set.
33 ;; If you would like to use this package to satisfy dependency replace this:
39 ;; (require 'inversion)
40 ;; (inversion-require 'spiffy "1.0")
42 ;; If you feel the need to not throw errors, you can do this instead:
44 ;; (let ((err (inversion-test 'spiffy "1.0")))
45 ;; (if err (your-stuff-here)))
47 ;; If you new package (2.0) needs to make sure a load file from your
48 ;; package is compatible, use this test:
50 ;; (if (not (inversion-reverse-test 'spiffy version-from-file))
56 ;; If you would like to make inversion optional, do this:
58 ;; (or (require 'inversion nil t)
59 ;; (defun inversion-test (p v)
60 ;; (string= v (symbol-value
61 ;; (intern-soft (concat (symbol-string p) "-version"))))))
63 ;; Or modify to specify `inversion-require' instead.
66 ;; Offer to download newer versions of a package.
70 ;; Sept 3, 2002: First general publication.
74 (defvar inversion-version
"1.3"
75 "Current version of InVersion.")
77 (defvar inversion-incompatible-version
"0.1alpha1"
78 "An earlier release which is incompatible with this release.")
80 (defconst inversion-decoders
82 (alpha "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?alpha\\([0-9]+\\)?$" 4)
83 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?beta\\([0-9]+\\)?$" 4)
84 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]*\\)?\\s-*\\.?(beta\\([0-9]+\\)?)$" 4)
85 (beta "^[^/]+/\\w+--\\w+--\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)--patch-\\([0-9]+\\)" 4)
86 (beta "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)-\\([0-9]+\\)-\\(.*\\)" 5)
87 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?pre\\([0-9]+\\)?$" 3)
88 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?$" 3)
89 (fullsingle "^\\([0-9]+\\)$" 1)
90 (patch "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?\\s-*(patch \\([0-9]+\\))" 4)
91 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
92 (point "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)-\\(0\\)-\\(.*\\)" 5)
93 (build "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\).\\([0-9]+\\)$" 4)
94 (full "^[^/]+/\\w+--\\w+--\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)--version-\\([0-9]+\\)" 4)
95 (full "^\\w+: v\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)" 5)
97 "List of decoders for version strings.
98 Each decoder is of the form:
100 ( RELEASE-TYPE REGEXP MAX )
102 RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
103 REGEXP is the regular expression to match a version string.
104 MAX is the maximum number of match-numbers in the release number.
105 Decoders must be ordered to decode least stable versions before the
110 (defun inversion-decode-version (version-string)
111 "Decode VERSION-STRING into an encoded list.
112 Return value is of the form:
113 (RELEASE MAJOR MINOR ...)
114 where RELEASE is a symbol such as `full', or `beta'."
115 (let ((decoders inversion-decoders
)
117 (while (and decoders
(not result
))
118 (if (string-match (nth 1 (car decoders
)) version-string
)
120 (num-left (nth 2 (car decoders
)))
122 (while (<= count num-left
)
124 (if (match-beginning count
)
126 (substring version-string
127 (match-beginning count
)
132 (setq result
(cons (caar decoders
) (nreverse ver
))))
133 (setq decoders
(cdr decoders
))))
136 (defun inversion-package-version (package)
137 "Return the decoded version for PACKAGE."
138 (let ((ver (symbol-value
140 (concat (symbol-name package
)
144 (error "Package %S does not define %S-version" package package
))
146 (setq code
(inversion-decode-version ver
))
148 (error "%S-version value (%s) cannot be decoded" package ver
))
151 (defun inversion-package-incompatibility-version (package)
152 "Return the decoded incompatibility version for PACKAGE.
153 The incompatibility version is specified by the programmer of
154 a package when a package is not backward compatible. It is
155 not an indication of new features or bug fixes."
156 (let ((ver (symbol-value
158 (concat (symbol-name package
)
159 "-incompatible-version")))))
163 (inversion-decode-version ver
))))
165 (defun inversion-recode (code)
166 "Convert CODE into a string."
167 (let ((r (nth 0 code
)) ; release-type
168 (n (nth 1 code
)) ; main number
169 (i (nth 2 code
)) ; first increment
170 (p (nth 3 code
))) ; second increment
176 (format "%s.%s%s%s" n i r p
)))
178 (defun inversion-release-to-number (release-symbol)
179 "Convert RELEASE-SYMBOL into a number."
180 (let* ((ra (assoc release-symbol inversion-decoders
))
181 (rn (- (length inversion-decoders
)
182 (length (member ra inversion-decoders
)))))
185 (defun inversion-= (ver1 ver2
)
186 "Return non-nil if VER1 is equal to VER2."
189 (defun inversion-< (ver1 ver2
)
190 "Return non-nil if VER1 is less than VER2."
191 (let ((v1-0 (inversion-release-to-number (nth 0 ver1
)))
197 (v2-0 (inversion-release-to-number (nth 0 ver2
)))
204 (cond ((and (equal (list v1-1 v1-2 v1-3 v1-4
)
205 (list v2-1 v2-2 v2-3 v2-4
))
208 ((and (equal v1-1 v2-1
)
211 v1-4 v2-4
) ; all or nothing if elt - is =
213 ((and (equal v1-1 v2-1
)
215 v1-3 v2-3
) ; all or nothing if elt - is =
217 ((and (equal v1-1 v2-1
)
224 (defun inversion-check-version (version incompatible-version
225 minimum
&rest reserved
)
226 "Check that a given version meets the minimum requirement.
227 VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
228 return entries of `inversion-decode-version', or a classic version
229 string. INCOMPATIBLE-VERSION can be nil.
230 RESERVED arguments are kept for a later use.
232 - nil if everything is ok.
233 - `outdated' if VERSION is less than MINIMUM.
234 - `incompatible' if VERSION is not backward compatible with MINIMUM.
235 - t if the check failed."
236 (let ((code (if (stringp version
)
237 (inversion-decode-version version
)
239 (req (if (stringp minimum
)
240 (inversion-decode-version minimum
)
245 ((inversion-= code req
)
246 ;; Same version.. Yay!
248 ((inversion-< code req
)
249 ;; Version is too old!
251 ((inversion-< req code
)
252 ;; Newer is installed. What to do?
254 (if (stringp incompatible-version
)
255 (inversion-decode-version incompatible-version
)
256 incompatible-version
)))
258 ((not incompatible
) nil
)
259 ((or (inversion-= req incompatible
)
260 (inversion-< req incompatible
))
261 ;; The requested version is = or < than what the package
262 ;; maintainer says is incompatible.
269 (defun inversion-test (package minimum
&rest reserved
)
270 "Test that PACKAGE meets the MINIMUM version requirement.
271 PACKAGE is a symbol, similar to what is passed to `require'.
272 MINIMUM is of similar format to return entries of
273 `inversion-decode-version', or a classic version string.
274 RESERVED arguments are kept for a later user.
275 This depends on the symbols `PACKAGE-version' and optionally
276 `PACKAGE-incompatible-version' being defined in PACKAGE.
277 Return nil if everything is ok. Return an error string otherwise."
278 (let ((check (inversion-check-version
279 (inversion-package-version package
)
280 (inversion-package-incompatibility-version package
)
284 ;; Same version.. Yay!
286 ((eq check
'outdated
)
287 ;; Version is too old!
288 (format "You need to upgrade package %s to %s" package minimum
))
289 ((eq check
'incompatible
)
290 ;; Newer is installed but the requested version is = or < than
291 ;; what the package maintainer says is incompatible, then throw
293 (format "Package %s version is not backward compatible with %s"
296 (t "Inversion version check failed."))))
298 (defun inversion-reverse-test (package oldversion
&rest reserved
)
299 "Test that PACKAGE at OLDVERSION is still compatible.
300 If something like a save file is loaded at OLDVERSION, this
301 test will identify if OLDVERSION is compatible with the current version
303 PACKAGE is a symbol, similar to what is passed to `require'.
304 OLDVERSION is of similar format to return entries of
305 `inversion-decode-version', or a classic version string.
306 RESERVED arguments are kept for a later user.
307 This depends on the symbols `PACKAGE-version' and optionally
308 `PACKAGE-incompatible-version' being defined in PACKAGE.
309 Return nil if everything is ok. Return an error string otherwise."
310 (let ((check (inversion-check-version
311 (inversion-package-version package
)
312 (inversion-package-incompatibility-version package
)
313 oldversion reserved
)))
316 ;; Same version.. Yay!
318 ((eq check
'outdated
)
319 ;; Version is too old!
320 (format "Package %s version %s is not compatible with current version"
322 ((eq check
'incompatible
)
323 ;; Newer is installed but the requested version is = or < than
324 ;; what the package maintainer says is incompatible, then throw
326 (format "Package %s version is not backward compatible with %s"
329 (t "Inversion version check failed."))))
331 (defun inversion-require (package version
&optional file directory
333 "Declare that you need PACKAGE with at least VERSION.
334 PACKAGE might be found in FILE. (See `require'.)
335 Throws an error if VERSION is incompatible with what is installed.
336 Optional argument DIRECTORY is a location where new versions of
337 this tool can be located. If there is a versioning problem and
338 DIRECTORY is provided, inversion will offer to download the file.
339 Optional argument RESERVED is saved for later use."
340 (require package file
)
341 (let ((err (inversion-test package version
)))
344 (inversion-download-package-ask err package directory version
)
346 ;; Return the package symbol that was required.
350 (defun inversion-require-emacs (emacs-ver xemacs-ver sxemacs-ver
)
351 "Declare that you need either EMACS-VER, XEMACS-VER or SXEMACS-ver.
352 Only checks one based on which kind of Emacs is being run."
353 (let ((err (inversion-test 'emacs
354 (cond ((featurep 'sxemacs
)
364 (defconst inversion-find-data
365 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
366 "Regexp template and match data index of a version string.")
368 (defun inversion-find-version (package)
369 "Search for the version and incompatible version of PACKAGE.
370 Does not load PACKAGE nor requires that it has been previously loaded.
371 Search in the directories in `load-path' for a PACKAGE.el library.
372 Visit the file found and search for the declarations of variables or
373 constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
374 value of these variables must be a version string.
376 Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
377 INCOMPATIBLE-VERSION-STRING can be nil.
378 Return nil when VERSION-STRING was not found."
379 (let* ((file (locate-library (format "%s.el" package
) t
))
380 (tag (car inversion-find-data
))
381 (idx (nth 1 inversion-find-data
))
385 ;; The 3000 is a bit arbitrary, but should cut down on
386 ;; fileio as version info usually is at the very top
387 ;; of a file. After a long commentary could be bad.
388 (insert-file-contents-literally file nil
0 3000)
389 (goto-char (point-min))
390 (when (re-search-forward (format tag package
'version
) nil t
)
391 (setq version
(list (match-string idx
)))
392 (goto-char (point-min))
393 (when (re-search-forward
394 (format tag package
'incompatible-version
) nil t
)
395 (setcdr version
(match-string idx
))))))
398 (defun inversion-add-to-load-path (package minimum
401 "Add the PACKAGE path to `load-path' if necessary.
402 MINIMUM is the minimum version requirement of PACKAGE.
403 Optional argument INSTALLDIR is the base directory where PACKAGE is
404 installed. It defaults to `default-directory'/PACKAGE.
405 SUBDIRS are sub-directories to add to `load-path', following the main
407 (let ((ver (inversion-find-version package
)))
408 ;; If PACKAGE not found or a bad version already in `load-path',
409 ;; prepend the new PACKAGE path, so it will be loaded first.
412 (inversion-check-version (car ver
) (cdr ver
) minimum
)
413 (message "Outdated %s %s shadowed to meet minimum version %s"
414 package
(car ver
) minimum
)
416 (let* ((default-directory
418 (expand-file-name (format "./%s" package
))))
420 (when (file-directory-p default-directory
)
423 (setq subdir
(expand-file-name (car subdirs
))
424 subdirs
(cdr subdirs
))
425 (when (file-directory-p subdir
)
426 ;;(message "%S added to `load-path'" subdir)
427 (add-to-list 'load-path subdir
)))
429 ;;(message "%S added to `load-path'" default-directory)
430 (add-to-list 'load-path default-directory
))
431 ;; We get to this point iff we do not accept or there is no
432 ;; system file. Let's check the version of what we just
433 ;; installed... just to be safe.
434 (let ((newver (inversion-find-version package
)))
436 (error "Failed to find version for newly installed %s"
438 (if (inversion-check-version (car newver
) (cdr newver
) minimum
)
439 (error "Outdated %s %s just installed" package
(car newver
)))
442 ;;; URL and downloading code
444 (defun inversion-locate-package-files (package directory
&optional version
)
445 "Get a list of distributions of PACKAGE from DIRECTORY.
446 DIRECTORY can be an ange-ftp compatible filename, such as:
447 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
448 If it is a URL, wget will be used for download.
449 Optional argument VERSION will restrict the list of available versions
450 to the file matching VERSION exactly, or nil."
451 ;;DIRECTORY should also allow a URL:
452 ;; \"http://ftp1.sourceforge.net/PACKAGE\"
453 ;; but then I can get file listings easily.
454 (if (symbolp package
) (setq package
(symbol-name package
)))
455 (directory-files directory t
457 (concat "^" package
"-" version
"\\>")
460 (defvar inversion-package-common-tails
'( ".tar.gz"
465 "Common distribution mechanisms for Emacs Lisp packages.")
467 (defun inversion-locate-package-files-and-split (package directory
&optional version
)
468 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
469 DIRECTORY is the location where distributions of PACKAGE are.
470 VERSION is an optional argument specifying a version to restrict to.
471 The return list is an alist with the version string in the CAR,
472 and the full path name in the CDR."
473 (if (symbolp package
) (setq package
(symbol-name package
)))
474 (let ((f (inversion-locate-package-files package directory version
))
477 (let* ((file (car f
))
478 (dist (file-name-nondirectory file
))
479 (tails inversion-package-common-tails
)
481 (while (and tails
(not verstring
))
482 (when (string-match (concat (car tails
) "$") dist
)
484 (substring dist
(1+ (length package
)) (match-beginning 0))))
485 (setq tails
(cdr tails
)))
487 (error "Cannot decode version for %s" dist
))
490 (cons verstring file
)
495 (defun inversion-download-package-ask (err package directory version
)
496 "Due to ERR, offer to download PACKAGE from DIRECTORY.
497 The package should have VERSION available for download."
498 (if (symbolp package
) (setq package
(symbol-name package
)))
499 (let ((files (inversion-locate-package-files-and-split
500 package directory version
)))
503 (if (not (y-or-n-p (concat err
": Download update? ")))
505 (let ((dest (read-directory-name (format "Download %s to: "
508 (if (> (length files
) 1)
512 (read-file-name "Version to download: "
517 (file-name-as-directory directory
)
521 (copy-file (cdr (car files
)) dest
))))))
523 ;;; How we upgrade packages in Emacs has yet to be ironed out.
525 ;; (defun inversion-upgrade-package (package &optional directory)
526 ;; "Try to upgrade PACKAGE in DIRECTORY is available."
527 ;; (interactive "sPackage to upgrade: ")
528 ;; (if (stringp package) (setq package (intern package)))
529 ;; (if (not directory)
530 ;; ;; Hope that the package maintainer specified.
531 ;; (setq directory (symbol-value (or (intern-soft
532 ;; (concat (symbol-name package)
535 ;; (concat (symbol-name package)
536 ;; "-directory"))))))
537 ;; (let ((files (inversion-locate-package-files-and-split
538 ;; package directory))
539 ;; (cver (inversion-package-version package))
542 ;; (if (inversion-< cver (inversion-decode-version (car f)))
543 ;; (setq newer (cons f newer))))
550 ;;; inversion.el ends here