Improve self-upgrading.

There's no reason not to parse again the command line with the newly
loaded code actually, so be sure to do the self-upgrade dance first
thing and recurse to the pgloader::main function (with a guard).
This commit is contained in:
Dimitri Fontaine 2014-05-03 15:22:34 +02:00
parent f34017d023
commit 6e58db2994
2 changed files with 23 additions and 10 deletions

View File

@ -74,13 +74,19 @@ If you want to test the current repository version (or any checkout really),
it's possible to clone the sources then load them with an older pgloader it's possible to clone the sources then load them with an older pgloader
release: release:
$ git clone https://github.com/dimitri/pgloader.git /tmp/pgloader $ /usr/bin/pgloader --version
$ /usr/bin/pgloader --self-upgrade /tmp/pgloader myfile.load pgloader version "3.0.99"
compiled with SBCL 1.1.17
Here, the code from the *git clone* will be used at run-time. Of course, $ git clone https://github.com/dimitri/pgloader.git /tmp/pgloader
code that has changed before the self-upgrade mechanism is executed will $ /usr/bin/pgloader --self-upgrade /tmp/pgloader --version
have no change to get run again. That only includes command line options Self-upgrading from sources at "/Users/dim/dev/pgloader/"
processing, though. pgloader version "3.0.fecae2c"
compiled with SBCL 1.1.17
Here, the code from the *git clone* will be used at run-time. Self-upgrade
is done first, then the main program entry point is called again with the
new coded loaded in.
## The pgloader.lisp script ## The pgloader.lisp script

View File

@ -82,6 +82,9 @@
(command-line-arguments:show-option-help *opt-spec*) (command-line-arguments:show-option-help *opt-spec*)
(when quit (uiop:quit))) (when quit (uiop:quit)))
(defvar *self-upgraded-already* nil
"Keep track if we did reload our own source code already.")
(defun self-upgrade (namestring) (defun self-upgrade (namestring)
"Load pgloader sources at PATH-TO-PGLOADER-SOURCES." "Load pgloader sources at PATH-TO-PGLOADER-SOURCES."
(let ((pgloader-pathname (uiop:directory-exists-p (let ((pgloader-pathname (uiop:directory-exists-p
@ -97,7 +100,8 @@
asdf:*central-registry*))) asdf:*central-registry*)))
(format t "Self-upgrading from sources at ~s~%" (format t "Self-upgrading from sources at ~s~%"
(uiop:native-namestring pgloader-pathname)) (uiop:native-namestring pgloader-pathname))
(asdf:load-system :pgloader))) (with-output-to-string (*standard-output*)
(asdf:operate 'asdf:load-op :pgloader :verbose nil))))
(condition (c) (condition (c)
(format t "Fatal: ~a~%" c))))) (format t "Fatal: ~a~%" c)))))
@ -118,6 +122,13 @@
root-dir self-upgrade) root-dir self-upgrade)
options options
;; First thing: Self Upgrade?
(when self-upgrade
(unless *self-upgraded-already*
(self-upgrade self-upgrade)
(let ((*self-upgraded-already* t))
(main argv))))
;; First care about the root directory where pgloader is supposed to ;; First care about the root directory where pgloader is supposed to
;; output its data logs and reject files ;; output its data logs and reject files
(let ((root-dir-truename (probe-file root-dir))) (let ((root-dir-truename (probe-file root-dir)))
@ -159,10 +170,6 @@
(format t "~%~%")) (format t "~%~%"))
(uiop:quit)) (uiop:quit))
;; Self Upgrade?
(when self-upgrade
(self-upgrade self-upgrade))
(when load (when load
(loop for filename in load (loop for filename in load
do (load (compile-file filename :verbose nil :print nil)))) do (load (compile-file filename :verbose nil :print nil))))