branch: elpa/vm
commit c0243a1903a48ab85d2ad2c3a9e0c3ec0f350485
Author: Mark Diekhans <ma...@ucsc.edu>
Commit: Mark Diekhans <ma...@ucsc.edu>

    switch to storing version and commit in a generate .el instead of text 
files to parse.  Includes untest code to get version from package manager if 
installed as a package
---
 .gitignore         | 13 ++++++-----
 lisp/Makefile.in   | 19 ++++++++--------
 lisp/vm-version.el | 63 +++++++++++++++++++++++++++++++-----------------------
 lisp/vm.el         |  8 ++++++-
 4 files changed, 58 insertions(+), 45 deletions(-)

diff --git a/.gitignore b/.gitignore
index 104bfff898..af79030249 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-s*.elc
+*.elc
 TAGS
 configure.lineno
 ChangeLog
@@ -36,12 +36,6 @@ config.status
 configure
 release
 snapshot
-info/version.texinfo
-lisp/version.txt
-/lisp/vm-autoloads.el
-/vm-autoloads.el
-/vm-pkg.el
-lisp/vm-cus-load.el
 *.orig
 *.rej
 *.saved
@@ -49,4 +43,9 @@ package-info
 _pkg.el
 commit.msg
 
+/info/version.texinfo
+/lisp/vm-autoloads.el
+/lisp/vm-version-conf.el
+/lisp/vm-cus-load.el
+
 .emacs.bak/
diff --git a/lisp/Makefile.in b/lisp/Makefile.in
index c4dcdbc257..1c3e0cc4ec 100644
--- a/lisp/Makefile.in
+++ b/lisp/Makefile.in
@@ -59,6 +59,7 @@ SOURCES += vm-virtual.el
 SOURCES += vm-window.el
 SOURCES += vm-w3m.el
 SOURCES += vm-w3.el
+SOURCES += vm-version-conf.el
 
 SOURCES += vcard.el
 SOURCES += tapestry.el
@@ -127,14 +128,15 @@ $(OBJECTS): $(AUTOLOADS)
 install: install-el install-elc install-aux
 
 ##############################################################################
-vm-version.elc: vm-version.el version.txt
-
-version.txt:
-       echo "$(PACKAGE_VERSION)" > $@.tmp
+# Create file with version and commit
+vm-version-conf.el: Makefile
+       echo ";;; Generated file do not commit " > $@.tmp
+       echo '(defconst vm-version-config "'"$(PACKAGE_VERSION)"'")' >> $@.tmp
        if [ -d "$(GIT_DIR)" ]; then \
-               $(GIT) --git-dir="$(GIT_DIR)" rev-parse HEAD >> $@.tmp; \
+               commit=`$(GIT) --git-dir="$(GIT_DIR)" rev-parse HEAD`; \
+               echo '(defconst vm-version-commit-config "'"$${commit}"'")' >> 
$@.tmp ; \
        else \
-               echo "unknown" >> $@.tp; \
+               echo '(defconst vm-version-commit-config "unknown")' >> $@.tmp 
; \
        fi
        mv -f $@.tmp $@
 
@@ -169,9 +171,6 @@ vm-cus-load.el: $(SOURCES:%=@srcdir@/%)
 # XEmacs#s auto-autoloads and custom-load file
 auto-autoloads.el: $(SOURCES:%=@srcdir@/%)
        -$(RM) -f $@
-#      (build_dir=`pwd`; cd "@srcdir@"; \
-#       $(EMACS_PROG) $(FLAGS) -l autoload \
-#              -f vm-built-autoloads "@abs_builddir@/$@" "`pwd`")
        $(EMACS_COMP) \
                 -eval "$(AUTOLOAD_PACKAGE_NAME)" \
                 -eval "$(AUTOLOAD_FILE)" \
@@ -255,7 +254,7 @@ Makefile: @srcdir@/Makefile.in
 
 ##############################################################################
 clean:
-       -$(RM) -f version.txt *.elc vm-autoloads.el auto-autoloads.el 
custom-load.el
+       -$(RM) -f vm-version-conf.el *.elc vm-autoloads.el auto-autoloads.el 
custom-load.el
 
 distclean: clean
        -$(RM) -f Makefile vm-cus-load.el
diff --git a/lisp/vm-version.el b/lisp/vm-version.el
index 0fece92b4f..dd28db7643 100644
--- a/lisp/vm-version.el
+++ b/lisp/vm-version.el
@@ -21,50 +21,59 @@
 ;;; Code:
 
 (require 'vm-macro)
+(require 'package)
 
 ;; Don't use vm-device-type here because it may not not be loaded yet.
 (declare-function device-type "vm-xemacs" ())
 (declare-function device-matching-specifier-tag-list "vm-xemacs" ())
 
-(defun vm-read-version-file (file-name line-number)
-  "Read the a line from of FILE-NAME, remove all whitespace, and return it as 
a string.
-Returns \"undefined\" if the file cannot be read."
-  (let ((file-path (expand-file-name
-                    file-name
-                    (and load-file-name (file-name-directory 
load-file-name)))))
-    (condition-case nil
-        (with-temp-buffer
-          (insert-file-contents-literally file-path)
-          (goto-char (point-min))
-          (forward-line (1- line-number))
-          (replace-regexp-in-string "\\s-" "" ; Remove all whitespace
-                                    (buffer-substring-no-properties
-                                      (line-beginning-position) 
(line-end-position))))
-      (file-error "undefined"))))
-
-(defconst vm-version (vm-read-version-file "version.txt" 1)
-  "Version number of VM.")
+(defun vm--version-info-from-conf ()
+  "Return version and commit from vm-version-conf.el if it exists."
+  (when (ignore-errors (load "vm-version-conf"))
+    (list vm-version-config vm-version-commit-config)))
+
+(defun vm--commit-from-package (pkg)
+  "Get commit hash from PKG, whether VC-installed or archive-installed."
+  (let ((desc (package-get-descriptor pkg)))
+    (or (when (package-vc-p desc)
+          (package-vc-commit desc))
+        (alist-get :commit (package-desc-extras desc)))))
+
+(defun vm--version-info-from-package ()
+  "Return version and commit if VM is loaded from a package."
+  (let ((package-version (vm-get-package-version)))
+    (if package-version
+        (list package-version (vm--commit-from-package 'vm))
+      (list nil nil))))
+
+;; Define vm-version and vm-version-commit
+(let ((version-info (or (vm--version-info-from-conf)
+                        (vm--version-info-from-package)
+                        (list nil nil))))
+  (defconst vm-version (nth 0 version-info)
+    "Version number of VM.")
+  (defconst vm-version-commit (nth 1 version-info)
+    "Git commit number of VM.")
+  (unless vm-version
+    (warn "Can't obtain vm-version from package or vm-version-conf.el"))
+  (unless vm-version-commit
+    (warn "Can't obtain vm-version-commit from package or 
vm-version-conf.el")))
 
 (defun vm-version ()
   "Return the value of the variable `vm-version'."
   (interactive)
   (when (vm-interactive-p)
-    (or (and (stringp vm-version)
-            (string-match "[0-9]" vm-version))
-       (error "Cannot determine VM version!"))
+    (unless vm-version
+      (warn "VM version was not discovered when VM was loaded"))
     (message "VM version is: %s" vm-version))
   vm-version)
 
-(defconst vm-version-commit (vm-read-version-file "version.txt" 2)
-  "git commit number of VM.")
-
 (defun vm-version-commit ()
   "Return the value of the variable `vm-version-commit'."
   (interactive)
   (when (vm-interactive-p)
-    (or (and (stringp vm-version-commit)
-            (string-match "[a-f0-9]+" vm-version-commit))
-       (error "Cannot determine VM commit!"))
+    (unless vm-version-commit
+      (warn "VM commit was not discovered when VM was loaded"))
     (message "VM commit is: %s" vm-version-commit))
   vm-version-commit)
 
diff --git a/lisp/vm.el b/lisp/vm.el
index f80436248f..9c2a098eba 100644
--- a/lisp/vm.el
+++ b/lisp/vm.el
@@ -56,7 +56,7 @@
 (require 'vm-sort)
 (require 'vm-reply)
 (eval-when-compile (require 'cl-lib))
-
+(require 'package)
 
 (defvar enable-multibyte-characters)
 
@@ -1675,4 +1675,10 @@ draft messages."
 (autoload 'tapestry-nullify-tapestry-elements "tapestry")
 (autoload 'tapestry-remove-frame-parameters "tapestry")
 
+(defun vm-get-package-version ()
+  "Return version of VM if it was installed as a package"
+  ;; N.B. this must be in this file, as package-get-version wants
+  ;; to be called from the file containg the `Version:' header.
+  (package-get-version))
+
 ;;; vm.el ends here

Reply via email to