Skip to content

Commit

Permalink
First pass at writing plugins.edn on install/update/uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
logseq-cldwalker committed Oct 17, 2022
1 parent 2cb0c8f commit da6c617
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
3 changes: 1 addition & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
2 changes: 1 addition & 1 deletion src/electron/electron/plugin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 3 additions & 1 deletion src/main/frontend/components/plugins.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)]]]

Expand Down
5 changes: 4 additions & 1 deletion src/main/frontend/handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/main/frontend/handler/plugin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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\:\[]+" ""))
Expand Down
44 changes: 44 additions & 0 deletions src/main/frontend/handler/plugin_config.cljs
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit da6c617

Please sign in to comment.