Skip to content

Commit

Permalink
Fix bug with certain plugins and misc cleanup
Browse files Browse the repository at this point in the history
- Plugins that used effects weren't loading correctly e.g.
  logseq-power-plugin
- Cleaned up translation, docs and some fn naming
  • Loading branch information
logseq-cldwalker committed Oct 17, 2022
1 parent 441a19f commit 2685cd1
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 43 deletions.
2 changes: 1 addition & 1 deletion bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{:git/url "https://github.com/babashka/spec.alpha"
:sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}
metosin/malli
{:mvn/version "0.8.9"}
{:mvn/version "0.9.1"}
logseq/bb-tasks
#_{:local/root "../bb-tasks"}
{:git/url "https://github.com/logseq/bb-tasks"
Expand Down
4 changes: 2 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
datascript-transit/datascript-transit {:mvn/version "0.3.0"}
;; TODO: bump to mvn/version when released
borkdude/rewrite-edn {:git/url "https://github.com/borkdude/rewrite-edn"
:sha "80f246139b1a43b6f2cbab329521d060ee7c1b7b"}
:sha "80f246139b1a43b6f2cbab329521d060ee7c1b7b"}
funcool/promesa {:mvn/version "4.0.2"}
medley/medley {:mvn/version "1.4.0"}
metosin/reitit-frontend {:mvn/version "0.3.10"}
Expand All @@ -31,7 +31,7 @@
instaparse/instaparse {:mvn/version "1.4.10"}
org.clojars.mmb90/cljs-cache {:mvn/version "0.1.4"}
logseq/graph-parser {:local/root "deps/graph-parser"}
metosin/malli {:mvn/version "0.8.9"}}
metosin/malli {:mvn/version "0.9.1"}}

:aliases {:cljs {:extra-paths ["src/dev-cljs/" "src/test/" "src/electron/"]
:extra-deps {org.clojure/clojurescript {:mvn/version "1.11.54"}
Expand Down
6 changes: 4 additions & 2 deletions docs/dev-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ aren't readable.

## Data validation and generation

We use both [spec](https://github.com/clojure/spec.alpha) and [malli](https://github.com/metosin/malli) for data validation and (and generation someday). malli has the advantage that its schema
is data and can be used for additional purposes. See plugin-config for an example.
We use both [spec](https://github.com/clojure/spec.alpha) and
[malli](https://github.com/metosin/malli) for data validation and (and
generation someday). malli has the advantage that its schema is data and can be
used for additional purposes. See plugin-config for an example.

Specs should go under `src/main/frontend/spec/` and be compatible with clojure
and clojurescript. See `frontend.spec.storage` for an example.
Expand Down
1 change: 1 addition & 0 deletions scripts/src/logseq/tasks/malli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[clojure.edn :as edn]))

(defn validate-plugins-edn
"Validate a plugins.edn file"
[file]
(if-let [errors (->> file
slurp
Expand Down
10 changes: 7 additions & 3 deletions src/electron/electron/plugin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
{:response-transform
(fn [res]
(if (= 404 (.-status res))
;; Fall back to fetching the latest For these rare
;; cases, previous logseq versions did not store the
;; Fall back to fetching the latest for these rare
;; cases. Previous logseq versions did not store the
;; plugin's git tag required to correctly install it
(let [repo' (some-> repo (string/trim) (string/replace #"^/+(.+?)/+$" "$1"))
api #(str "https://api.github.com/repos/" repo' "/" %)]
Expand Down Expand Up @@ -158,8 +158,12 @@
(emit :lsp-installed {:status :error :payload e})
(throw e))))

;; repo is a github repo, not a logseq repo
(defn install-or-update!
"Default behavior is to install the latest version of a given repo. Item map
includes the following keys:
* :only-check - When set to true, this only fetches the latest version without installing
* :plugin-action - When set to 'install', installs the specific :version given
* :repo - A github repo, not a logseq repo, e.g. user/repo"
[{:keys [version repo only-check plugin-action] :as item}]
(if repo
(let [coerced-version (and version (. semver coerce version))
Expand Down
15 changes: 7 additions & 8 deletions src/main/frontend/components/plugins.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@
[{:title [:span.flex.items-center (ui/icon "world") (t :settings-page/network-proxy)]
:options {:on-click #(state/pub-event! [:go/proxy-settings agent-opts])}}]

[{:title [:span.flex.items-center (ui/icon "arrow-down-circle") (t :plugin/install-from-file)]
:options {:on-click plugin-config-handler/open-sync-modal}}]
[{:title [:span.flex.items-center (ui/icon "arrow-down-circle") (t :plugin.install-from-file/menu-title)]
:options {:on-click plugin-config-handler/open-replace-plugins-modal}}]

(when (state/developer-mode?)
[{:hr true}
Expand Down Expand Up @@ -804,11 +804,10 @@
< rum/reactive
[plugins]
[:div.cp__plugins-fom-file
[:h1.mb-4.text-2xl.p-1 "Install plugins from plugins.edn"]
[:h1.mb-4.text-2xl.p-1 (t :plugin.install-from-file/title)]
(if (seq plugins)
[:div
[:div.mb-2.text-xl (util/format "The following %s plugin(s) will replace your plugins:"
(count (:install plugins)))]
[:div.mb-2.text-xl (t :plugin.install-from-file/notice)]
;; lists
[:ul
(for [it (:install plugins)
Expand All @@ -817,16 +816,16 @@
{:key k}
[:label.flex-1
{:for k}
[:strong.px-3 (str (:name it) " " (:version it))]]])]
[:strong.px-3 (str (name (:id it)) " " (:version it))]]])]

;; actions
[:div.pt-5
(ui/button [:span "Install"]
(ui/button [:span (t :plugin/install)]
:on-click #(do
(plugin-config-handler/replace-plugins plugins)
(state/close-sub-modal! "ls-plugins-from-file-modal")))]]
;; all done
[:div.py-4 [:strong.text-xl "\uD83C\uDF89 All synced!"]])])
[:div.py-4 [:strong.text-xl (str "\uD83C\uDF89 " (t :plugin.install-from-file/success))]])])


(defn open-select-theme!
Expand Down
7 changes: 5 additions & 2 deletions src/main/frontend/dicts.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
:plugin/title "Title"
:plugin/all "All"
:plugin/unpacked "Unpacked"
:plugin/delete-alert "Are you sure uninstall plugin [{1}]?"
:plugin/delete-alert "Are you sure you want to uninstall the plugin [{1}]?"
:plugin/open-settings "Open settings"
:plugin/open-package "Open package"
:plugin/load-unpacked "Load unpacked plugin"
Expand All @@ -377,7 +377,10 @@
:plugin/marketplace-tips "If the plugin doesn't work correctly when first installed, try to restart Logseq."
:plugin/up-to-date "It's up to date"
:plugin/custom-js-alert "Found the custom.js file, is it allowed to execute? (If you don't understand the content of this file, it is recommended not to allow execution, which has certain security risks.)"
:plugin/install-from-file "Install from plugins.edn"
:plugin.install-from-file/menu-title "Install from plugins.edn"
:plugin.install-from-file/title "Install plugins from plugins.edn"
:plugin.install-from-file/notice "The following plugins will replace your plugins:"
:plugin.install-from-file/success "All plugins installed!"

:pdf/copy-ref "Copy ref"
:pdf/copy-text "Copy text"
Expand Down
27 changes: 12 additions & 15 deletions src/main/frontend/handler/plugin_config.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns frontend.handler.plugin-config
"This system component encapsulates the global plugin.edn and depends on the
global-config component. This component is only enabled? if both the
global-config and plugin components are enabled"
global-config and plugin components are enabled. plugin.edn is automatically updated
when a plugin is installed, updated or removed"
(:require [frontend.handler.global-config :as global-config-handler]
["path" :as path]
[promesa.core :as p]
Expand All @@ -20,6 +21,7 @@
[lambdaisland.glogi :as log]))

(defn plugin-config-path
"Full path to plugins.edn"
[]
(path/join (global-config-handler/global-config-dir) "plugins.edn"))

Expand Down Expand Up @@ -58,26 +60,21 @@
"Given installed plugins state and plugins from plugins.edn,
returns map of plugins to install and uninstall"
[installed-plugins edn-plugins]
;; :name is removed from comparison because it isn't used for reproducible builds
;; and is just for display purposes
(let [installed-plugins-set (->> installed-plugins
vals
(map #(-> (select-keys % common-plugin-keys)
(assoc :id (keyword (:id %)))
(dissoc :name)))
(assoc :id (keyword (:id %)))))
set)
edn-plugins-set (->> edn-plugins
(map (fn [[k v]] (-> v
(assoc :id k)
(dissoc :name))))
(map (fn [[k v]] (assoc v :id k)))
set)]
(if (= installed-plugins-set edn-plugins-set)
{}
{:install (mapv #(assoc % :plugin-action "install")
(set/difference edn-plugins-set installed-plugins-set))
:uninstall (vec (set/difference installed-plugins-set edn-plugins-set))})))

(defn open-sync-modal
(defn open-replace-plugins-modal
[]
(state/pub-event! [:go/plugins])
(p/catch
Expand Down Expand Up @@ -106,24 +103,24 @@ returns map of plugins to install and uninstall"
(plugin-common-handler/unregister-plugin (name (:id plugin))))
(log/info :install-plugins (:install plugins))
(doseq [plugin (:install plugins)]
(plugin-common-handler/install-marketplace-plugin plugin)))
(plugin-common-handler/install-marketplace-plugin
;; Add :name so that install notifications are readable
(assoc plugin :name (name (:id plugin))))))

(defn setup-install-listener!
"Sets up a listener for the lsp-installed event to update plugins.edn"
[]
(let [listener (fn listener [_ e]
(when-let [{:keys [status payload only-check]} (bean/->clj e)]
(when (and (= status "completed") (not only-check))
(let [{:keys [name title theme]} payload
;; Same defaults as plugin/setup-install-listener!
name (or title name "Untitled")]
(let [{:keys [theme effect]} payload]
(add-or-update-plugin
(assoc payload
:version (:installed-version payload)
:effect (boolean effect)
;; Manual install doesn't have theme field but
;; plugin.edn requires this field
:theme (if (some? theme) theme false)
:name name))))))]
:theme (boolean theme)))))))]
(js/window.apis.addListener "lsp-installed" listener)))

(defn start
Expand Down
3 changes: 1 addition & 2 deletions src/main/frontend/modules/shortcut/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@

:ui/install-plugins-from-file {:binding false
:inactive (not (config/plugin-config-enabled?))
;; TODO: Remove dev convenience
:fn (fn [] (plugin-config-handler/open-sync-modal))}
:fn plugin-config-handler/open-replace-plugins-modal}

:editor/toggle-open-blocks {:binding "t o"
:fn editor-handler/toggle-open!}
Expand Down
6 changes: 3 additions & 3 deletions src/main/frontend/schema/handler/plugin_config.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
; The plugin keys should not be changed between releases without a migration plan
; for existing config files
(def Plugin
":repo and :version determine the correct plugin to install. :theme and
:effect are needed for the install process to work correctly"
[:map
[:name
[:and {:gen/fmap '(partial str "Name ")}
string?]]
[:version
[:and
{:gen/fmap '(fn [_] (apply str (interpose "." (repeatedly 3 (fn [] (rand-int 10))))))}
string?]]
[:repo
[:and {:gen/fmap '(partial str "github-user/")}
string?]]
[:effect boolean?]
[:theme boolean?]])

(def Plugins-edn
Expand Down
10 changes: 5 additions & 5 deletions src/test/frontend/handler/plugin_config_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

(deftest-async add-or-update-plugin
(let [dir (create-global-config-dir)
plugin-to-add {:id :foo :name "Foo" :repo "some-user/foo" :version "v0.9.0"}
plugin-to-add {:id :foo :repo "some-user/foo" :version "v0.9.0"}
body (pr-str (mg/generate plugin-config-schema/Plugins-edn {:size 10}))]
(fs-node/writeFileSync (plugin-config-handler/plugin-config-path) body)

Expand Down Expand Up @@ -61,7 +61,7 @@

(p/finally #(delete-global-config-dir dir)))))

(deftest-async open-sync-modal-malformed-edn
(deftest-async open-replace-plugins-modal-malformed-edn
(let [dir (create-global-config-dir)
error-message (atom nil)]
(fs-node/writeFileSync (plugin-config-handler/plugin-config-path) "{:id {}")
Expand All @@ -70,12 +70,12 @@
[notification/show! (fn [msg _] (reset! error-message msg))]
(->
(p/do!
(plugin-config-handler/open-sync-modal)
(plugin-config-handler/open-replace-plugins-modal)
(is (string/starts-with? @error-message "Malformed plugins.edn")
"User sees correct notification"))
(p/finally #(delete-global-config-dir dir))))))

(deftest-async open-sync-modal-invalid-edn
(deftest-async open-replace-plugins-modal-invalid-edn
(let [dir (create-global-config-dir)
error-message (atom nil)]
;; Missing a couple plugin keys
Expand All @@ -86,7 +86,7 @@
[notification/show! (fn [msg _] (reset! error-message msg))]
(->
(p/do!
(plugin-config-handler/open-sync-modal)
(plugin-config-handler/open-replace-plugins-modal)
(is (string/starts-with? @error-message "Invalid plugins.edn")
"User sees correct notification"))
(p/finally #(delete-global-config-dir dir))))))
Expand Down

0 comments on commit 2685cd1

Please sign in to comment.