Skip to content

Commit

Permalink
Add arc plugin to plugin store
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed May 7, 2024
1 parent 551ea53 commit 421cad0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
72 changes: 63 additions & 9 deletions Wox/plugin/system/wpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (w *WPMPlugin) GetMetadata() plugin.Metadata {
Command: "dev.remove",
Description: "Remove local Wox plugin, followed by a directory",
},
{
Command: "dev.reload",
Description: "Reload all dev plugins",
},
},
SupportedOS: []string{
"Windows",
Expand Down Expand Up @@ -131,6 +135,20 @@ func (w *WPMPlugin) GetMetadata() plugin.Metadata {
func (w *WPMPlugin) Init(ctx context.Context, initParams plugin.InitParams) {
w.api = initParams.API

w.reloadAllDevPlugins(ctx)

util.Go(ctx, "reload dev plugins in dist", func() {
// must delay reload, because host env is not ready when system plugin init
time.Sleep(time.Second * 5)

newCtx := util.NewTraceContext()
for _, lp := range w.localPlugins {
w.reloadLocalDistPlugin(newCtx, lp.metadata, "reload after startup")
}
})
}

func (w *WPMPlugin) reloadAllDevPlugins(ctx context.Context) {
var localPluginDirs []LocalPlugin
unmarshalErr := json.Unmarshal([]byte(w.api.GetSetting(ctx, localPluginDirectoriesKey)), &localPluginDirs)
if unmarshalErr != nil {
Expand All @@ -156,15 +174,6 @@ func (w *WPMPlugin) Init(ctx context.Context, initParams plugin.InitParams) {
for _, directory := range w.localPluginDirectories {
w.loadDevPlugin(ctx, directory)
}

util.Go(ctx, "reload dev plugins in dist", func() {
// must delay reload, because host env is not ready when system plugin init
time.Sleep(time.Second * 5)
newCtx := util.NewTraceContext()
for _, lp := range w.localPlugins {
w.reloadLocalDistPlugin(newCtx, lp.metadata, "reload after startup")
}
})
}

func (w *WPMPlugin) loadDevPlugin(ctx context.Context, pluginDirectory string) {
Expand All @@ -180,6 +189,24 @@ func (w *WPMPlugin) loadDevPlugin(ctx context.Context, pluginDirectory string) {
metadata: metadata,
}

// check if plugin is already loaded
existingLocalPlugin, exist := lo.Find(w.localPlugins, func(lp localPlugin) bool {
return lp.metadata.Metadata.Id == metadata.Metadata.Id
})
if exist {
w.api.Log(ctx, plugin.LogLevelInfo, "plugin already loaded, unload first")
if existingLocalPlugin.watcher != nil {
closeWatcherErr := existingLocalPlugin.watcher.Close()
if closeWatcherErr != nil {
w.api.Log(ctx, plugin.LogLevelError, fmt.Sprintf("Failed to close watcher: %s", closeWatcherErr.Error()))
}
}

w.localPlugins = lo.Filter(w.localPlugins, func(lp localPlugin, _ int) bool {
return lp.metadata.Metadata.Id != metadata.Metadata.Id
})
}

// watch dist directory changes and auto reload plugin
distDirectory := path.Join(pluginDirectory, "dist")
if _, statErr := os.Stat(distDirectory); statErr == nil {
Expand Down Expand Up @@ -238,6 +265,10 @@ func (w *WPMPlugin) Query(ctx context.Context, query plugin.Query) []plugin.Quer
return w.removeDevCommand(ctx, query)
}

if query.Command == "dev.reload" {
return w.reloadDevCommand(ctx)
}

if query.Command == "dev.list" {
return w.listDevCommand(ctx)
}
Expand Down Expand Up @@ -457,6 +488,29 @@ func (w *WPMPlugin) listDevCommand(ctx context.Context) []plugin.QueryResult {
})
}

func (w *WPMPlugin) reloadDevCommand(ctx context.Context) []plugin.QueryResult {
return []plugin.QueryResult{
{
Title: "Reload all dev plugins",
Icon: wpmIcon,
Actions: []plugin.QueryResultAction{
{
Name: "Reload",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
w.reloadAllDevPlugins(ctx)
util.Go(ctx, "reload dev plugins in dist", func() {
newCtx := util.NewTraceContext()
for _, lp := range w.localPlugins {
w.reloadLocalDistPlugin(newCtx, lp.metadata, "reload after user action")
}
})
},
},
},
},
}
}

func (w *WPMPlugin) addDevCommand(ctx context.Context, query plugin.Query) []plugin.QueryResult {
w.api.Log(ctx, plugin.LogLevelInfo, "Please choose a directory to add local plugin")
pluginDirectories := plugin.GetPluginManager().GetUI().PickFiles(ctx, share.PickFilesParams{IsDirectory: true})
Expand Down
17 changes: 17 additions & 0 deletions plugin-store.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,22 @@
],
"DateCreated": "2024-02-23 19:09:00",
"DateUpdated": "2024-04-23 05:02:15"
},
{
"Id": "84cb8c8f-1156-489d-a490-6d04fbc85d52",
"Name": "Arc",
"Author": "Wox-launcher",
"Version": "0.0.1",
"MinWoxVersion": "2.0.0",
"Runtime": "nodejs",
"Description": "Search arc tabs and spaces",
"IconUrl": "https://github.com/Wox-launcher/Wox.Plugin.Arc/main/images/app.png",
"Website": "https://github.com/Wox-launcher/Wox.Plugin.Arc",
"DownloadUrl": "https://github.com/Wox-launcher/Wox.Plugin.Arc/releases/latest/download/wox.plugin.arc.wox",
"ScreenshotUrls": [
"https://raw.githubusercontent.com/Wox-launcher/Wox.Plugin.DeepL/main/assets/snapshot.png"
],
"DateCreated": "2024-05-07 13:02:00",
"DateUpdated": "2024-05-07 13:02:00"
}
]

0 comments on commit 421cad0

Please sign in to comment.