1 ;;; inversion.el --- When you need something in version XX.XX
3 ;;; Copyright (C) 2002-2003, 2005-2011 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 <http://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]+\\)\\s-*\\.?alpha\\([0-9]+\\)?$" 3)
83 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?beta\\([0-9]+\\)?$" 3)
84 (beta "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*(beta\\([0-9]+\\)?)" 3)
85 (prerelease "^\\([0-9]+\\)\\.\\([0-9]+\\)\\s-*\\.?pre\\([0-9]+\\)?$" 3)
86 (full "^\\([0-9]+\\)\\.\\([0-9]+\\)$" 2)
87 (fullsingle "^\\([0-9]+\\)$" 1)
88 (patch "^\\([0-9]+\\)\\.\\([0-9]+\\) (patch \\([0-9]+\\))" 3)
89 (point "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$" 3)
90 (build "^\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\).\\([0-9]+\\)$" 4)
92 "List of decoders for version strings.
93 Each decoder is of the form:
95 ( RELEASE-TYPE REGEXP MAX )
97 RELEASE-TYPE is a symbol specifying something like `beta' or `alpha'.
98 REGEXP is the regular expression to match a version string.
99 MAX is the maximum number of match-numbers in the release number.
100 Decoders must be ordered to decode least stable versions before the
105 (defun inversion-decode-version (version-string)
106 "Decode VERSION-STRING into an encoded list.
107 Return value is of the form:
108 (RELEASE MAJOR MINOR ...)
109 where RELEASE is a symbol such as `full', or `beta'."
110 (let ((decoders inversion-decoders
)
112 (while (and decoders
(not result
))
113 (if (string-match (nth 1 (car decoders
)) version-string
)
115 (num-left (nth 2 (car decoders
)))
117 (while (<= count num-left
)
119 (if (match-beginning count
)
121 (substring version-string
122 (match-beginning count
)
127 (setq result
(cons (caar decoders
) (nreverse ver
))))
128 (setq decoders
(cdr decoders
))))
131 (defun inversion-package-version (package)
132 "Return the decoded version for PACKAGE."
133 (let ((ver (symbol-value
135 (concat (symbol-name package
)
139 (error "Package %S does not define %S-version" package package
))
141 (setq code
(inversion-decode-version ver
))
143 (error "%S-version value cannot be decoded" package
))
146 (defun inversion-package-incompatibility-version (package)
147 "Return the decoded incompatibility version for PACKAGE.
148 The incompatibility version is specified by the programmer of
149 a package when a package is not backward compatible. It is
150 not an indication of new features or bug fixes."
151 (let ((ver (symbol-value
153 (concat (symbol-name package
)
154 "-incompatible-version")))))
158 (inversion-decode-version ver
))))
160 (defun inversion-recode (code)
161 "Convert CODE into a string."
162 (let ((r (nth 0 code
)) ; release-type
163 (n (nth 1 code
)) ; main number
164 (i (nth 2 code
)) ; first increment
165 (p (nth 3 code
))) ; second increment
171 (format "%s.%s%s%s" n i r p
)))
173 (defun inversion-release-to-number (release-symbol)
174 "Convert RELEASE-SYMBOL into a number."
175 (let* ((ra (assoc release-symbol inversion-decoders
))
176 (rn (- (length inversion-decoders
)
177 (length (member ra inversion-decoders
)))))
180 (defun inversion-= (ver1 ver2
)
181 "Return non-nil if VER1 is equal to VER2."
184 (defun inversion-< (ver1 ver2
)
185 "Return non-nil if VER1 is less than VER2."
186 (let ((v1-0 (inversion-release-to-number (nth 0 ver1
)))
192 (v2-0 (inversion-release-to-number (nth 0 ver2
)))
198 (or (and (= v1-0 v2-0
)
202 v1-4 v2-4
; all or nothin if elt - is =
207 v1-3 v2-3
; all or nothin if elt - is =
218 (defun inversion-check-version (version incompatible-version
219 minimum
&rest reserved
)
220 "Check that a given version meets the minimum requirement.
221 VERSION, INCOMPATIBLE-VERSION and MINIMUM are of similar format to
222 return entries of `inversion-decode-version', or a classic version
223 string. INCOMPATIBLE-VERSION can be nil.
224 RESERVED arguments are kept for a later use.
226 - nil if everything is ok.
227 - 'outdated if VERSION is less than MINIMUM.
228 - 'incompatible if VERSION is not backward compatible with MINIMUM.
229 - t if the check failed."
230 (let ((code (if (stringp version
)
231 (inversion-decode-version version
)
233 (req (if (stringp minimum
)
234 (inversion-decode-version minimum
)
239 ((inversion-= code req
)
240 ;; Same version.. Yay!
242 ((inversion-< code req
)
243 ;; Version is too old!
245 ((inversion-< req code
)
246 ;; Newer is installed. What to do?
248 (if (stringp incompatible-version
)
249 (inversion-decode-version incompatible-version
)
250 incompatible-version
)))
252 ((not incompatible
) nil
)
253 ((or (inversion-= req incompatible
)
254 (inversion-< req incompatible
))
255 ;; The requested version is = or < than what the package
256 ;; maintainer says is incompatible.
263 (defun inversion-test (package minimum
&rest reserved
)
264 "Test that PACKAGE meets the MINIMUM version requirement.
265 PACKAGE is a symbol, similar to what is passed to `require'.
266 MINIMUM is of similar format to return entries of
267 `inversion-decode-version', or a classic version string.
268 RESERVED arguments are kept for a later user.
269 This depends on the symbols `PACKAGE-version' and optionally
270 `PACKAGE-incompatible-version' being defined in PACKAGE.
271 Return nil if everything is ok. Return an error string otherwise."
272 (let ((check (inversion-check-version
273 (inversion-package-version package
)
274 (inversion-package-incompatibility-version package
)
278 ;; Same version.. Yay!
280 ((eq check
'outdated
)
281 ;; Version is too old!
282 (format "You need to upgrade package %s to %s" package minimum
))
283 ((eq check
'incompatible
)
284 ;; Newer is installed but the requested version is = or < than
285 ;; what the package maintainer says is incompatible, then throw
287 (format "Package %s version is not backward compatible with %s"
290 (t "Inversion version check failed."))))
292 (defun inversion-reverse-test (package oldversion
&rest reserved
)
293 "Test that PACKAGE at OLDVERSION is still compatible.
294 If something like a save file is loaded at OLDVERSION, this
295 test will identify if OLDVERSION is compatible with the current version
297 PACKAGE is a symbol, similar to what is passed to `require'.
298 OLDVERSION is of similar format to return entries of
299 `inversion-decode-version', or a classic version string.
300 RESERVED arguments are kept for a later user.
301 This depends on the symbols `PACKAGE-version' and optionally
302 `PACKAGE-incompatible-version' being defined in PACKAGE.
303 Return nil if everything is ok. Return an error string otherwise."
304 (let ((check (inversion-check-version
305 (inversion-package-version package
)
306 (inversion-package-incompatibility-version package
)
307 oldversion reserved
)))
310 ;; Same version.. Yay!
312 ((eq check
'outdated
)
313 ;; Version is too old!
314 (format "Package %s version %s is not compatible with current version"
316 ((eq check
'incompatible
)
317 ;; Newer is installed but the requested version is = or < than
318 ;; what the package maintainer says is incompatible, then throw
320 (format "Package %s version is not backward compatible with %s"
323 (t "Inversion version check failed."))))
325 (defun inversion-require (package version
&optional file directory
327 "Declare that you need PACKAGE with at least VERSION.
328 PACKAGE might be found in FILE. (See `require'.)
329 Throws an error if VERSION is incompatible with what is installed.
330 Optional argument DIRECTORY is a location where new versions of
331 this tool can be located. If there is a versioning problem and
332 DIRECTORY is provided, inversion will offer to download the file.
333 Optional argument RESERVED is saved for later use."
334 (require package file
)
335 (let ((err (inversion-test package version
)))
338 (inversion-download-package-ask err package directory version
)
340 ;; Return the package symbol that was required.
343 (defun inversion-require-emacs (emacs-ver xemacs-ver
)
344 "Declare that you need either EMACS-VER, or XEMACS-VER.
345 Only checks one based on which kind of Emacs is being run."
346 (let ((err (inversion-test 'emacs
347 (if (featurep 'xemacs
)
354 (defconst inversion-find-data
355 '("(def\\(var\\|const\\)\\s-+%s-%s\\s-+\"\\([^\"]+\\)" 2)
356 "Regexp template and match data index of a version string.")
358 (defun inversion-find-version (package)
359 "Search for the version and incompatible version of PACKAGE.
360 Does not load PACKAGE nor requires that it has been previously loaded.
361 Search in the directories in `load-path' for a PACKAGE.el library.
362 Visit the file found and search for the declarations of variables or
363 constants `PACKAGE-version' and `PACKAGE-incompatible-version'. The
364 value of these variables must be a version string.
366 Return a pair (VERSION-STRING . INCOMPATIBLE-VERSION-STRING) where
367 INCOMPATIBLE-VERSION-STRING can be nil.
368 Return nil when VERSION-STRING was not found."
369 (let* ((file (locate-library (format "%s.el" package
) t
))
370 (tag (car inversion-find-data
))
371 (idx (nth 1 inversion-find-data
))
375 ;; The 3000 is a bit arbitrary, but should cut down on
376 ;; fileio as version info usually is at the very top
377 ;; of a file. After a long commentary could be bad.
378 (insert-file-contents-literally file nil
0 3000)
379 (goto-char (point-min))
380 (when (re-search-forward (format tag package
'version
) nil t
)
381 (setq version
(list (match-string idx
)))
382 (goto-char (point-min))
383 (when (re-search-forward
384 (format tag package
'incompatible-version
) nil t
)
385 (setcdr version
(match-string idx
))))))
388 (defun inversion-add-to-load-path (package minimum
391 "Add the PACKAGE path to `load-path' if necessary.
392 MINIMUM is the minimum version requirement of PACKAGE.
393 Optional argument INSTALLDIR is the base directory where PACKAGE is
394 installed. It defaults to `default-directory'/PACKAGE.
395 SUBDIRS are sub-directories to add to `load-path', following the main
397 (let ((ver (inversion-find-version package
)))
398 ;; If PACKAGE not found or a bad version already in `load-path',
399 ;; prepend the new PACKAGE path, so it will be loaded first.
402 (inversion-check-version (car ver
) (cdr ver
) minimum
)
403 (message "Outdated %s %s shadowed to meet minimum version %s"
404 package
(car ver
) minimum
)
406 (let* ((default-directory
408 (expand-file-name (format "./%s" package
))))
410 (when (file-directory-p default-directory
)
413 (setq subdir
(expand-file-name (car subdirs
))
414 subdirs
(cdr subdirs
))
415 (when (file-directory-p subdir
)
416 ;;(message "%S added to `load-path'" subdir)
417 (add-to-list 'load-path subdir
)))
419 ;;(message "%S added to `load-path'" default-directory)
420 (add-to-list 'load-path default-directory
))
421 ;; We get to this point iff we do not accept or there is no
422 ;; system file. Let's check the version of what we just
423 ;; installed... just to be safe.
424 (let ((newver (inversion-find-version package
)))
426 (error "Failed to find version for newly installed %s"
428 (if (inversion-check-version (car newver
) (cdr newver
) minimum
)
429 (error "Outdated %s %s just installed" package
(car newver
)))
432 ;;; URL and downloading code
434 (defun inversion-locate-package-files (package directory
&optional version
)
435 "Get a list of distributions of PACKAGE from DIRECTORY.
436 DIRECTORY can be an ange-ftp compatible filename, such as:
437 \"/ftp@ftp1.sourceforge.net/pub/sourceforge/PACKAGE\"
438 If it is a URL, wget will be used for download.
439 Optional argument VERSION will restrict the list of available versions
440 to the file matching VERSION exactly, or nil."
441 ;;DIRECTORY should also allow a URL:
442 ;; \"http://ftp1.sourceforge.net/PACKAGE\"
443 ;; but then I can get file listings easily.
444 (if (symbolp package
) (setq package
(symbol-name package
)))
445 (directory-files directory t
447 (concat "^" package
"-" version
"\\>")
450 (defvar inversion-package-common-tails
'( ".tar.gz"
455 "Common distribution mechanisms for Emacs Lisp packages.")
457 (defun inversion-locate-package-files-and-split (package directory
&optional version
)
458 "Use `inversion-locate-package-files' to get a list of PACKAGE files.
459 DIRECTORY is the location where distributions of PACKAGE are.
460 VERSION is an optional argument specifying a version to restrict to.
461 The return list is an alist with the version string in the CAR,
462 and the full path name in the CDR."
463 (if (symbolp package
) (setq package
(symbol-name package
)))
464 (let ((f (inversion-locate-package-files package directory version
))
467 (let* ((file (car f
))
468 (dist (file-name-nondirectory file
))
469 (tails inversion-package-common-tails
)
471 (while (and tails
(not verstring
))
472 (when (string-match (concat (car tails
) "$") dist
)
474 (substring dist
(1+ (length package
)) (match-beginning 0))))
475 (setq tails
(cdr tails
)))
477 (error "Cannot decode version for %s" dist
))
480 (cons verstring file
)
485 (defun inversion-download-package-ask (err package directory version
)
486 "Due to ERR, offer to download PACKAGE from DIRECTORY.
487 The package should have VERSION available for download."
488 (if (symbolp package
) (setq package
(symbol-name package
)))
489 (let ((files (inversion-locate-package-files-and-split
490 package directory version
)))
493 (if (not (y-or-n-p (concat err
": Download update? ")))
495 (let ((dest (read-directory-name (format "Download %s to: "
498 (if (> (length files
) 1)
502 (read-file-name "Version to download: "
507 (file-name-as-directory directory
)
511 (copy-file (cdr (car files
)) dest
))))))
513 ;;; How we upgrade packages in Emacs has yet to be ironed out.
515 ;; (defun inversion-upgrade-package (package &optional directory)
516 ;; "Try to upgrade PACKAGE in DIRECTORY is available."
517 ;; (interactive "sPackage to upgrade: ")
518 ;; (if (stringp package) (setq package (intern package)))
519 ;; (if (not directory)
520 ;; ;; Hope that the package maintainer specified.
521 ;; (setq directory (symbol-value (or (intern-soft
522 ;; (concat (symbol-name package)
525 ;; (concat (symbol-name package)
526 ;; "-directory"))))))
527 ;; (let ((files (inversion-locate-package-files-and-split
528 ;; package directory))
529 ;; (cver (inversion-package-version package))
532 ;; (if (inversion-< cver (inversion-decode-version (car f)))
533 ;; (setq newer (cons f newer))))
540 ;;; inversion.el ends here