diff --git a/bbin b/bbin index d40e271..97517db 100755 --- a/bbin +++ b/bbin @@ -1154,22 +1154,29 @@ (fn [{:keys [src]}] (println "Skipping" src))}) -(defn- printer [cli-opts] - (if (:edn cli-opts) - (fn [k & {:as opts}] (prn (if opts [k opts] [k]))) - (fn [k & {:as opts}] ((get templates k) (assoc opts :cli-opts cli-opts))))) - -(defn backup-path [s] - (str s ".backup-" (inst-ms (util/now)))) - -(defn- create-backup [t] +(defn- printer [log-path cli-opts] + (fn [k & {:as opts}] + (let [cmd (if opts [k opts] [k])] + (when log-path + (spit log-path (prn-str cmd) :append true)) + (if (:edn cli-opts) + (prn cmd) + ((get templates k) (assoc opts :cli-opts cli-opts)))))) + +(defn log-path [s migration-id] + (str s "." migration-id ".log")) + +(defn backup-path [s migration-id] + (str s "." migration-id ".backup")) + +(defn- create-backup [t migration-id] (let [src (str (dirs/legacy-bin-dir)) - dest (backup-path (dirs/legacy-bin-dir))] + dest (backup-path (dirs/legacy-bin-dir) migration-id)] (t :moving {:src src :dest dest}) (fs/move src dest)) (when (fs/exists? (dirs/legacy-jars-dir)) (let [src (str (dirs/legacy-jars-dir)) - dest (backup-path (dirs/legacy-jars-dir))] + dest (backup-path (dirs/legacy-jars-dir) migration-id)] (fs/move src dest) (t :moving {:src src :dest dest})))) @@ -1206,7 +1213,10 @@ (copy-jar))))) (defn migrate-auto [{:keys [overwrite] :as cli-opts}] - (let [t (printer cli-opts)] + (let [migration-id (inst-ms (util/now)) + logp (when (dirs/using-legacy-paths?) + (log-path (dirs/legacy-bin-dir) migration-id)) + t (printer logp cli-opts)] (if-not (dirs/using-legacy-paths?) (t :up-to-date) (let [scripts (scripts/load-scripts (dirs/legacy-bin-dir))] @@ -1227,7 +1237,7 @@ (println) (t :migrating) (println) - (create-backup cli-opts) + (create-backup t migration-id) (println) (t :done) (println)) @@ -1238,7 +1248,7 @@ (t :migrating) (doseq [[script-name confirmed] confirm-results] (copy-script script-name confirmed cli-opts t))) - (create-backup t) + (create-backup t migration-id) (println) (t :done) (println)))) diff --git a/src/babashka/bbin/migrate.clj b/src/babashka/bbin/migrate.clj index cd9fff4..de9282b 100644 --- a/src/babashka/bbin/migrate.clj +++ b/src/babashka/bbin/migrate.clj @@ -60,22 +60,29 @@ (fn [{:keys [src]}] (println "Skipping" src))}) -(defn- printer [cli-opts] - (if (:edn cli-opts) - (fn [k & {:as opts}] (prn (if opts [k opts] [k]))) - (fn [k & {:as opts}] ((get templates k) (assoc opts :cli-opts cli-opts))))) - -(defn backup-path [s] - (str s ".backup-" (inst-ms (util/now)))) - -(defn- create-backup [t] +(defn- printer [log-path cli-opts] + (fn [k & {:as opts}] + (let [cmd (if opts [k opts] [k])] + (when log-path + (spit log-path (prn-str cmd) :append true)) + (if (:edn cli-opts) + (prn cmd) + ((get templates k) (assoc opts :cli-opts cli-opts)))))) + +(defn log-path [s migration-id] + (str s "." migration-id ".log")) + +(defn backup-path [s migration-id] + (str s "." migration-id ".backup")) + +(defn- create-backup [t migration-id] (let [src (str (dirs/legacy-bin-dir)) - dest (backup-path (dirs/legacy-bin-dir))] + dest (backup-path (dirs/legacy-bin-dir) migration-id)] (t :moving {:src src :dest dest}) (fs/move src dest)) (when (fs/exists? (dirs/legacy-jars-dir)) (let [src (str (dirs/legacy-jars-dir)) - dest (backup-path (dirs/legacy-jars-dir))] + dest (backup-path (dirs/legacy-jars-dir) migration-id)] (fs/move src dest) (t :moving {:src src :dest dest})))) @@ -112,7 +119,10 @@ (copy-jar))))) (defn migrate-auto [{:keys [overwrite] :as cli-opts}] - (let [t (printer cli-opts)] + (let [migration-id (inst-ms (util/now)) + logp (when (dirs/using-legacy-paths?) + (log-path (dirs/legacy-bin-dir) migration-id)) + t (printer logp cli-opts)] (if-not (dirs/using-legacy-paths?) (t :up-to-date) (let [scripts (scripts/load-scripts (dirs/legacy-bin-dir))] @@ -133,7 +143,7 @@ (println) (t :migrating) (println) - (create-backup cli-opts) + (create-backup t migration-id) (println) (t :done) (println)) @@ -144,7 +154,7 @@ (t :migrating) (doseq [[script-name confirmed] confirm-results] (copy-script script-name confirmed cli-opts t))) - (create-backup t) + (create-backup t migration-id) (println) (t :done) (println)))) diff --git a/test/babashka/bbin/migrate_test.clj b/test/babashka/bbin/migrate_test.clj index a9c6038..70edda5 100644 --- a/test/babashka/bbin/migrate_test.clj +++ b/test/babashka/bbin/migrate_test.clj @@ -26,11 +26,12 @@ (testing "scenario: no changes needed" (reset-test-dir) - (is (= [[:up-to-date]] - (->> (migrate/migrate :auto {:edn true}) - (with-in-str "") - with-out-str - parse-edn-out)))) + (let [commands [[:up-to-date]]] + (is (= commands + (->> (migrate/migrate :auto {:edn true}) + (with-in-str "") + with-out-str + parse-edn-out))))) (testing "scenario: changes needed, user declines" (reset-test-dir) @@ -39,16 +40,21 @@ (spit "#!/usr/bin/env bb\n(println \"Hello world\")"))] (with-out-str (scripts/install {:script/lib (str (fs/canonicalize test-script))})) (fs/create-dirs (dirs/legacy-bin-dir)) - (let [parsed-script (scripts/parse-script - (slurp (fs/file (dirs/legacy-bin-dir) "hello")))] - (is (= [[:printable-scripts {:scripts {'hello parsed-script}}] - [:found-scripts] - [:prompt-move] - [:canceled]] - (->> (migrate/migrate :auto {:edn true}) - (with-in-str "no\n") - with-out-str - parse-edn-out)))))) + (binding [util/*now* (Instant/ofEpochSecond 123)] + (let [parsed-script (scripts/parse-script + (slurp (fs/file (dirs/legacy-bin-dir) "hello"))) + commands [[:printable-scripts {:scripts {'hello parsed-script}}] + [:found-scripts] + [:prompt-move] + [:canceled]]] + (is (= commands + (->> (migrate/migrate :auto {:edn true}) + (with-in-str "no\n") + with-out-str + parse-edn-out))) + (is (= commands + (parse-edn-out (slurp (migrate/log-path (dirs/legacy-bin-dir) + (inst-ms (util/now))))))))))) (testing "scenario: changes needed, user accepts, no conflicts" (reset-test-dir) @@ -58,21 +64,26 @@ (with-out-str (scripts/install {:script/lib (str (fs/canonicalize test-script))})) (binding [util/*now* (Instant/ofEpochSecond 123)] (let [parsed-script (scripts/parse-script - (slurp (fs/file (dirs/legacy-bin-dir) "hello")))] - (is (= [[:printable-scripts {:scripts {'hello parsed-script}}] - [:found-scripts] - [:prompt-move] - [:migrating] - [:copying {:src (str (fs/file (dirs/legacy-bin-dir) "hello")) - :dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] - [:moving {:src (str (dirs/legacy-bin-dir)) - :dest (migrate/backup-path (dirs/legacy-bin-dir))}] - [:done]] + (slurp (fs/file (dirs/legacy-bin-dir) "hello"))) + commands-1 [[:printable-scripts {:scripts {'hello parsed-script}}] + [:found-scripts] + [:prompt-move] + [:migrating] + [:copying {:src (str (fs/file (dirs/legacy-bin-dir) "hello")) + :dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] + [:moving {:src (str (dirs/legacy-bin-dir)) + :dest (migrate/backup-path (dirs/legacy-bin-dir) (inst-ms (util/now)))}] + [:done]] + commands-2 [[:up-to-date]]] + (is (= commands-1 (->> (migrate/migrate :auto {:edn true}) (with-in-str "yes\n") with-out-str parse-edn-out))) - (is (= [[:up-to-date]] + (is (= commands-1 + (parse-edn-out (slurp (migrate/log-path (dirs/legacy-bin-dir) + (inst-ms (util/now))))))) + (is (= commands-2 (->> (migrate/migrate :auto {:edn true}) (with-in-str "yes\n") with-out-str @@ -88,21 +99,26 @@ (fs/copy (fs/file (dirs/legacy-bin-dir) "hello") (fs/file (dirs/xdg-bin-dir nil) "hello")) (binding [util/*now* (Instant/ofEpochSecond 123)] (let [parsed-script (scripts/parse-script - (slurp (fs/file (dirs/legacy-bin-dir) "hello")))] - (is (= [[:printable-scripts {:scripts {'hello parsed-script}}] - [:found-scripts] - [:prompt-move] - [:confirm-overwrite {:dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] - [:migrating] - [:skipping {:src (str (fs/file (dirs/legacy-bin-dir) "hello"))}] - [:moving {:src (str (dirs/legacy-bin-dir)) - :dest (migrate/backup-path (dirs/legacy-bin-dir))}] - [:done]] + (slurp (fs/file (dirs/legacy-bin-dir) "hello"))) + commands-1 [[:printable-scripts {:scripts {'hello parsed-script}}] + [:found-scripts] + [:prompt-move] + [:confirm-overwrite {:dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] + [:migrating] + [:skipping {:src (str (fs/file (dirs/legacy-bin-dir) "hello"))}] + [:moving {:src (str (dirs/legacy-bin-dir)) + :dest (migrate/backup-path (dirs/legacy-bin-dir) (inst-ms (util/now)))}] + [:done]] + commands-2 [[:up-to-date]]] + (is (= commands-1 (->> (migrate/migrate :auto {:edn true}) (with-in-str "yes\nno\n") with-out-str parse-edn-out))) - (is (= [[:up-to-date]] + (is (= commands-1 + (parse-edn-out (slurp (migrate/log-path (dirs/legacy-bin-dir) + (inst-ms (util/now))))))) + (is (= commands-2 (->> (migrate/migrate :auto {:edn true}) (with-in-str "yes\n") with-out-str @@ -118,22 +134,27 @@ (fs/copy (fs/file (dirs/legacy-bin-dir) "hello") (fs/file (dirs/xdg-bin-dir nil) "hello")) (binding [util/*now* (Instant/ofEpochSecond 123)] (let [parsed-script (scripts/parse-script - (slurp (fs/file (dirs/legacy-bin-dir) "hello")))] - (is (= [[:printable-scripts {:scripts {'hello parsed-script}}] - [:found-scripts] - [:prompt-move] - [:confirm-overwrite {:dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] - [:migrating] - [:copying {:src (str (fs/file (dirs/legacy-bin-dir) "hello")) - :dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] - [:moving {:src (str (dirs/legacy-bin-dir)) - :dest (migrate/backup-path (dirs/legacy-bin-dir))}] - [:done]] + (slurp (fs/file (dirs/legacy-bin-dir) "hello"))) + commands-1 [[:printable-scripts {:scripts {'hello parsed-script}}] + [:found-scripts] + [:prompt-move] + [:confirm-overwrite {:dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] + [:migrating] + [:copying {:src (str (fs/file (dirs/legacy-bin-dir) "hello")) + :dest (str (fs/file (dirs/xdg-bin-dir nil) "hello"))}] + [:moving {:src (str (dirs/legacy-bin-dir)) + :dest (migrate/backup-path (dirs/legacy-bin-dir) (inst-ms (util/now)))}] + [:done]] + commands-2 [[:up-to-date]]] + (is (= commands-1 (->> (migrate/migrate :auto {:edn true}) (with-in-str "yes\nyes\n") with-out-str parse-edn-out))) - (is (= [[:up-to-date]] + (is (= commands-1 + (parse-edn-out (slurp (migrate/log-path (dirs/legacy-bin-dir) + (inst-ms (util/now))))))) + (is (= commands-2 (->> (migrate/migrate :auto {:edn true}) (with-in-str "yes\n") with-out-str