Skip to content

Commit

Permalink
Write log file during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
rads committed Sep 19, 2023
1 parent 7f2e36a commit c3ca74a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 77 deletions.
38 changes: 24 additions & 14 deletions bbin
Expand Up @@ -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}))))

Expand Down Expand Up @@ -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))]
Expand All @@ -1227,7 +1237,7 @@
(println)
(t :migrating)
(println)
(create-backup cli-opts)
(create-backup t migration-id)
(println)
(t :done)
(println))
Expand All @@ -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))))
Expand Down
38 changes: 24 additions & 14 deletions src/babashka/bbin/migrate.clj
Expand Up @@ -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}))))

Expand Down Expand Up @@ -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))]
Expand All @@ -133,7 +143,7 @@
(println)
(t :migrating)
(println)
(create-backup cli-opts)
(create-backup t migration-id)
(println)
(t :done)
(println))
Expand All @@ -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))))
Expand Down
119 changes: 70 additions & 49 deletions test/babashka/bbin/migrate_test.clj
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c3ca74a

Please sign in to comment.