Просмотр исходного кода

First pass at writing plugins.edn on install/update/uninstall

Gabriel Horner 3 лет назад
Родитель
Сommit
da6c617a5b

+ 1 - 2
deps.edn

@@ -4,8 +4,7 @@
   rum/rum                               {:mvn/version "0.12.9"}
   datascript/datascript                 {:mvn/version "1.3.8"}
   datascript-transit/datascript-transit {:mvn/version "0.3.0"}
-  borkdude/rewrite-edn                  {:git/url "https://github.com/borkdude/rewrite-edn"
-                                         :sha     "edd87dc7f045f28d7afcbfc44bc0f0a2683dde62"}
+  borkdude/rewrite-edn                  {:mvn/version "0.1.0"}
   funcool/promesa                       {:mvn/version "4.0.2"}
   medley/medley                         {:mvn/version "1.4.0"}
   metosin/reitit-frontend               {:mvn/version "0.3.10"}

+ 1 - 1
src/electron/electron/plugin.cljs

@@ -173,7 +173,7 @@
                            :only-check only-check
                            :payload    (if only-check
                                          (assoc item :latest-version latest-version :latest-notes notes)
-                                         (assoc item :zip dl-url :dst dest))})
+                                         (assoc item :zip dl-url :dst dest :installed-version latest-version))})
 
                     (resolve nil))
 

+ 3 - 1
src/main/frontend/components/plugins.cljs

@@ -5,6 +5,7 @@
             [frontend.context.i18n :refer [t]]
             [frontend.ui :as ui]
             [frontend.handler.ui :as ui-handler]
+            [frontend.handler.plugin-config :as plugin-config]
             [frontend.search :as search]
             [frontend.util :as util]
             [frontend.mixins :as mixins]
@@ -223,7 +224,8 @@
                     {:title      (t :plugin/delete-alert name)
                      :on-confirm (fn [_ {:keys [close-fn]}]
                                    (close-fn)
-                                   (plugin-handler/unregister-plugin id))})]
+                                   (plugin-handler/unregister-plugin id)
+                                   (plugin-config/remove-plugin name))})]
                (state/set-sub-modal! confirm-fn {:center? true}))}
        (t :plugin/uninstall)]]]
 

+ 4 - 1
src/main/frontend/handler.cljs

@@ -28,6 +28,7 @@
             [frontend.handler.user :as user-handler]
             [frontend.handler.repo-config :as repo-config-handler]
             [frontend.handler.global-config :as global-config-handler]
+            [frontend.handler.plugin-config :as plugin-config]
             [frontend.handler.metadata :as metadata-handler]
             [frontend.idb :as idb]
             [frontend.mobile.util :as mobile-util]
@@ -91,7 +92,9 @@
            (->
             (p/do! (repo-config-handler/start {:repo repo})
                    (when (config/global-config-enabled?)
-                     (global-config-handler/start {:repo repo})))
+                     (global-config-handler/start {:repo repo}))
+                   ;; TODO: Is there a better place for this setup?
+                   (plugin-config/start))
             (p/finally
               (fn []
                 ;; install after config is restored

+ 5 - 3
src/main/frontend/handler/plugin.cljs

@@ -6,6 +6,7 @@
             [clojure.walk :as walk]
             [logseq.graph-parser.mldoc :as gp-mldoc]
             [frontend.handler.notification :as notification]
+            [frontend.handler.plugin-config :as plugin-config]
             [camel-snake-kebab.core :as csk]
             [frontend.state :as state]
             [medley.core :as medley]
@@ -209,10 +210,11 @@
 
                              (do                            ;; register new
                                (p/then
-                                 (js/LSPluginCore.register (bean/->js {:key id :url dst}))
-                                 (fn [] (when theme (js/setTimeout #(select-a-plugin-theme id) 300))))
+                                (js/LSPluginCore.register (bean/->js {:key id :url dst}))
+                                (fn [] (when theme (js/setTimeout #(select-a-plugin-theme id) 300))))
+                               (plugin-config/add-or-update-plugin name (:installed-version payload))
                                (notification/show!
-                                 (str (t :plugin/installed) (t :plugins) ": " name) :success)))))
+                                (str (t :plugin/installed) (t :plugins) ": " name) :success)))))
 
                        :error
                        (let [error-code (keyword (string/replace (:error-code payload) #"^[\s\:\[]+" ""))

+ 44 - 0
src/main/frontend/handler/plugin_config.cljs

@@ -0,0 +1,44 @@
+(ns frontend.handler.plugin-config
+  "This ns is a system component that encapsulate the global plugin.edn.
+This component depends on TODO"
+  (:require [frontend.handler.global-config :as global-config-handler]
+            ["path" :as path]
+            [promesa.core :as p]
+            [borkdude.rewrite-edn :as rewrite]
+            [frontend.fs :as fs]
+            [frontend.state :as state]
+            [clojure.pprint :as pprint]))
+
+(defn- plugins-path
+  []
+  (path/join (global-config-handler/global-config-dir) "plugins.edn"))
+
+(defn add-or-update-plugin
+  [plugin-name plugin-version]
+  (p/let [content (fs/read-file "" (plugins-path))
+          updated-content (-> content
+                              rewrite/parse-string
+                              (rewrite/assoc plugin-name {:version plugin-version})
+                              str)]
+         (fs/write-file! nil "" (plugins-path) updated-content {:skip-compare? true})))
+
+(defn remove-plugin
+  [plugin-name]
+  (p/let [content (fs/read-file "" (plugins-path))
+          updated-content (-> content rewrite/parse-string (rewrite/dissoc plugin-name) str)]
+         (fs/write-file! nil "" (plugins-path) updated-content {:skip-compare? true})))
+
+(defn- create-global-config-file-if-not-exists
+  []
+  (let [content (->> (:plugin/installed-plugins @state/state)
+                     vals
+                     (map (fn [{:keys [name version]}]
+                            [name {:version version}]))
+                     (into {})
+                     pprint/pprint
+                     with-out-str)]
+    (fs/create-if-not-exists nil (global-config-handler/global-config-dir) (plugins-path) content)))
+
+(defn start
+  []
+  (create-global-config-file-if-not-exists))