From c169eef161f506de8c4996f009e25599b6b869a6 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 18 Mar 2025 17:57:52 +0100 Subject: [PATCH] :recycle: Remove tokens lib migrations from file migrations --- common/src/app/common/files/migrations.cljc | 32 +--- common/src/app/common/types/tokens_lib.cljc | 141 ++++++++++++------ .../common_tests/types/tokens_lib_test.cljc | 2 +- 3 files changed, 100 insertions(+), 75 deletions(-) diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index b4f908e56..dd848bc54 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -29,7 +29,6 @@ [app.common.types.file :as ctf] [app.common.types.shape :as cts] [app.common.types.shape.shadow :as ctss] - [app.common.types.tokens-lib :as ctob] [app.common.uuid :as uuid] [clojure.set :as set] [cuerdas.core :as str])) @@ -1226,32 +1225,7 @@ (update :pages-index update-vals update-container) (update :components update-vals update-container)))) -(defmethod migrate-data "Ensure hidden theme" - [data _] - (letfn [(update-tokens-lib [tokens-lib] - (let [hidden-theme (ctob/get-hidden-theme tokens-lib)] - (if (nil? hidden-theme) - (ctob/add-theme tokens-lib (ctob/make-hidden-token-theme)) - tokens-lib)))] - (if (contains? data :tokens-lib) - (update data :tokens-lib update-tokens-lib) - data))) - -(defmethod migrate-data "Add token theme id" - [data _] - (letfn [(update-tokens-lib [tokens-lib] - (let [themes (ctob/get-themes tokens-lib)] - (reduce (fn [lib theme] - (if (:id theme) - lib - (ctob/update-theme lib (:group theme) (:name theme) #(assoc % :id (str (uuid/next)))))) - tokens-lib - themes)))] - (if (contains? data :tokens-lib) - (update data :tokens-lib update-tokens-lib) - data))) - -(defmethod migrate-data "Remove tokens from groups" +(defmethod migrate-data "0001-remove-tokens-from-groups" [data _] (letfn [(update-object [object] (cond-> object @@ -1320,6 +1294,4 @@ "legacy-65" "legacy-66" "legacy-67" - "Ensure hidden theme" - "Add token theme id" - "Remove tokens from groups"])) + "0001-remove-tokens-from-groups"])) diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index b8db6467a..6bcfe3606 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -1381,27 +1381,32 @@ Will return a value that matches this schema: (def ^:private check-active-themes (sm/check-fn schema:active-themes :hint "expected valid active themes")) +(defn- ensure-hidden-theme + "A helper that is responsible to ensure that the hidden theme always + exists on the themes data structure" + [themes] + (update themes hidden-token-theme-group + (fn [data] + (if (contains? data hidden-token-theme-name) + data + (d/oassoc data hidden-token-theme-name (make-hidden-token-theme)))))) + +;; NOTE: is possible that ordered map is not the most apropriate +;; data structure and maybe we need a specific that allows us an +;; easy way to reorder it, or just store inside Tokens data +;; structure the data and the order separately as we already do +;; with pages and pages-index. (defn make-tokens-lib "Create an empty or prepopulated tokens library." - ([] - ;; NOTE: is possible that ordered map is not the most apropriate - ;; data structure and maybe we need a specific that allows us an - ;; easy way to reorder it, or just store inside Tokens data - ;; structure the data and the order separately as we already do - ;; with pages and pages-index. - (make-tokens-lib :sets (d/ordered-map) - :themes (d/ordered-map) - :active-themes #{hidden-token-theme-path})) - - ([& {:keys [sets themes active-themes]}] - (let [active-themes (d/nilv active-themes #{hidden-token-theme-path}) - themes (if (empty? themes) - (update themes hidden-token-theme-group d/oassoc hidden-token-theme-name (make-hidden-token-theme)) - themes)] - (TokensLib. - (check-token-sets sets) - (check-token-themes themes) - (check-active-themes active-themes))))) + [& {:keys [sets themes active-themes]}] + (let [sets (or sets (d/ordered-map)) + themes (-> (or themes (d/ordered-map)) + (ensure-hidden-theme)) + active-themes (or active-themes #{hidden-token-theme-path})] + (TokensLib. + (check-token-sets sets) + (check-token-themes themes) + (check-active-themes active-themes)))) (defn ensure-tokens-lib [tokens-lib] @@ -1444,6 +1449,71 @@ Will return a value that matches this schema: :wfn #(into {} %) :rfn #(map->Token %)}) +#?(:clj + (defn- read-tokens-lib-v1-0 + "Reads the first version of tokens lib, now completly obsolete" + [r] + (let [;; Migrate sets tree without prefix to new format + prev-sets (->> (fres/read-object! r) + (tree-seq d/ordered-map? vals) + (filter (partial instance? TokenSet))) + + sets (-> (reduce add-set (make-tokens-lib) prev-sets) + (deref) + (:sets)) + + _set-groups (fres/read-object! r) + themes (fres/read-object! r) + active-themes (fres/read-object! r)] + (->TokensLib sets themes active-themes)))) + +#?(:clj + (defn- read-tokens-lib-v1-1 + "Reads the tokens lib data structure and ensures that hidden + theme exists and adds missing ID on themes" + [r] + (let [sets (fres/read-object! r) + themes (fres/read-object! r) + active-themes (fres/read-object! r) + + ;; Ensure we have at least a hidden theme + themes + (ensure-hidden-theme themes) + + ;; Ensure we add an :id field for each existing theme + themes + (reduce (fn [result group-id] + (update result group-id + (fn [themes] + (reduce (fn [themes theme-id] + (update themes theme-id + (fn [theme] + (if (get theme :id) + theme + (assoc theme :id (str (uuid/next))))))) + themes + (keys themes))))) + themes + (keys themes))] + + (->TokensLib sets themes active-themes)))) + +#?(:clj + (defn- write-tokens-lib + [n w ^TokensLib o] + (fres/write-tag! w n 3) + (fres/write-object! w (.-sets o)) + (fres/write-object! w (.-themes o)) + (fres/write-object! w (.-active-themes o)))) + +#?(:clj + (defn- read-tokens-lib + [r] + (let [sets (fres/read-object! r) + themes (fres/read-object! r) + active-themes (fres/read-object! r)] + (->TokensLib sets themes active-themes)))) + #?(:clj (fres/add-handlers! {:name "penpot/token/v1" @@ -1473,32 +1543,15 @@ Will return a value that matches this schema: (let [obj (fres/read-object! r)] (map->TokenTheme obj)))} + ;; LEGACY TOKENS LIB READERS (with migrations) {:name "penpot/tokens-lib/v1" - :rfn (fn [r] - (let [;; Migrate sets tree without prefix to new format - prev-sets (->> (fres/read-object! r) - (tree-seq d/ordered-map? vals) - (filter (partial instance? TokenSet))) - - ;; FIXME: wtf we usind deref here? - sets (-> (reduce add-set (make-tokens-lib) prev-sets) - (deref) - (:sets)) - - _set-groups (fres/read-object! r) - themes (fres/read-object! r) - active-themes (fres/read-object! r)] - (->TokensLib sets themes active-themes)))} + :rfn read-tokens-lib-v1-0} {:name "penpot/tokens-lib/v1.1" + :rfn read-tokens-lib-v1-1} + + ;; CURRENT TOKENS LIB READER & WRITTER + {:name "penpot/tokens-lib/v1.2" :class TokensLib - :wfn (fn [n w o] - (fres/write-tag! w n 3) - (fres/write-object! w (.-sets o)) - (fres/write-object! w (.-themes o)) - (fres/write-object! w (.-active-themes o))) - :rfn (fn [r] - (let [sets (fres/read-object! r) - themes (fres/read-object! r) - active-themes (fres/read-object! r)] - (->TokensLib sets themes active-themes)))})) + :wfn write-tokens-lib + :rfn read-tokens-lib})) diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index ba9cb5f12..7269b3a86 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -213,7 +213,7 @@ (t/is (= (ctob/set-count tokens-lib) 0)))) (t/deftest make-invalid-tokens-lib - (let [params {:sets nil :themes nil}] + (let [params {:sets {} :themes {}}] (t/is (thrown-with-msg? #?(:cljs js/Error :clj Exception) #"expected valid token sets" (ctob/make-tokens-lib params)))))