P1 of docs/subsystems/plugin-runtime.md. P0 (3ccbe6bc1) gave built-in plugins a manifest, plugins.yml and a switch; their pages were still app.js's own code. Now the window has a kernel a plugin draws its page through, and the Gateway page moved into plugins/gateway to prove it. What changed: - internal/gui/assets/kernel.js (new): window.magpie. A plugin registers with magpie.plugin(id, setup); ctx.slots.add("page", part) puts its markup where index.html's <template data-slot="page"> is. The shell calls its hooks through magpie.call, behind an error boundary: a hook that throws, or whose promise fails, is logged and its page shows "This page stopped working", what broke and Try again, while every other page goes on. ctx.ui is the kit (button, switch, the app menu, a settings row) and ui.form draws a plugin's settings from their schema. ctx.config / onConfig carry the settings, from boot.js before the first paint and from Plugins › Built-in after. - internal/pluginrt/schema.go (new): a plugin's settings are a Go struct (Manifest.Settings); SchemaOf makes the JSON Schema the form is drawn from, and Validate is what SetConfig (config.go) takes into plugins.yml. Compose drops a hand-edited value the schema refuses. - plugins/gateway: the Gateway page as a plugin, Essential and in use while a gateway key is there. One setting, callsPage (10/20/50/100, 20 by default). Its client is plugins/gateway/{keys.js,client.js}, moved out of app.js (and caller-keys.js) unchanged in behaviour. - internal/gui: builtins.go takes POST {id, config} beside {id, on} and lists each plugin's schema and config; boot.js carries pluginConfig; /plugins/<id>/… files are served. Plugins › Built-in draws a plugin's settings rows under its switch. - Docs: plugin-runtime.md has "The window's kernel (P1)"; gui-shell.md, subsystems/README.md and tests/README.md updated. What was run: - go test ./... (sandbox HOME, pinned caches): all packages ok; gofmt -l clean; go vet on darwin, GOOS=linux -tags nogui and GOOS=windows. - New tests: TestSchemaOf, TestValidate, TestSetConfig, TestBuiltinConfig, TestBuiltinClientsLoaded; plugin-kernel.test.cjs (a page that throws breaks only itself and Try again draws it, at 1100 and 420 in en, zh, zh-TW, ja, de; callsPage from boot.js and from Plugins › Built-in), both engines. - Broken by hand, each turned its test red: the boundary's catch removed; Try again not putting the page's markup back; the Gateway page's onConfig not re-reading callsPage; boot.js's pluginConfig ignored; SetConfig skipping Validate. - The full GUI suite in Chromium and WebKit (node --test *.test.cjs). One red was this change's own. providers-fetching-retry lifts providersWhileFetching out of app.js, which now asks providersPage, and the lift lacked it. The test now lifts that too, and gains "a poll under the Gateway page, a plugin's page that reads with Providers, reads". That case goes red with providersPage dropped from the poll's guard. - The reds that were on origin/maina75de4c21too are fixed in their own commits after this one. Two of them came from P0's Built-in tab: nav-sub-unroll and plugin-check-github-limit.
158 lines
4.9 KiB
Go
158 lines
4.9 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"github.com/yetone/magpie/internal/pluginrt"
|
||
)
|
||
|
||
// Plugins › Built-in (Lxxyx on X: "功能太多了"): magpie's built-in plugins
|
||
// (internal/pluginrt, plugins/), each with a switch kept in plugins.yml.
|
||
// Off, a plugin's parts leave the window: its page leaves the sidebar and
|
||
// the header's tabs, its sections leave the pages they are on, its rows
|
||
// leave Settings and Settings' search, and a link to it opens nothing. The
|
||
// page hides whatever is marked data-feature="<id>" while the plugin isn't
|
||
// active.
|
||
//
|
||
// One in use stays on: its switch can't be turned off, and its row says
|
||
// what uses it. A new install starts with the Essentials preset; one that
|
||
// ran before has them all on until the user says otherwise.
|
||
// TestBuiltinsTranslated asks each string of every plugin of every
|
||
// language.
|
||
|
||
// builtinJSON is a built-in plugin as the page is told it: whether it
|
||
// shows (On: active, which plugins.yml or its use makes it), the user's
|
||
// own choice, and what of it is in use.
|
||
type builtinJSON struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
Sub string `json:"sub"`
|
||
On bool `json:"on"`
|
||
Chosen bool `json:"chosen"`
|
||
Source pluginrt.Source `json:"source"`
|
||
State pluginrt.State `json:"state"`
|
||
// Why is the reason it isn't running, when it is on and isn't
|
||
Why string `json:"why,omitempty"`
|
||
InUse int `json:"inUse"`
|
||
Use string `json:"use,omitempty"` // One or Many, by InUse
|
||
Parts []pluginrt.Part `json:"parts,omitempty"`
|
||
// Schema is its settings' (pluginrt/schema.go), which the page draws
|
||
// a form of; Config their values now
|
||
Schema *pluginrt.Schema `json:"schema,omitempty"`
|
||
Config map[string]any `json:"config,omitempty"`
|
||
// Broken is plugins.yml's error when it can't be read: every
|
||
// plugin runs as it did, and no switch can be changed
|
||
Broken string `json:"broken,omitempty"`
|
||
}
|
||
|
||
// builtinsState composes plugins.yml again, brings the runtime to it, and
|
||
// says every built-in plugin, in their order.
|
||
func builtinsState() []builtinJSON {
|
||
cs, err := pluginrt.Refresh()
|
||
byID := map[string]pluginrt.Composed{}
|
||
for _, c := range cs {
|
||
byID[c.ID] = c
|
||
}
|
||
st := map[string]pluginrt.Status{}
|
||
if rt := pluginrt.Running(); rt != nil {
|
||
for _, s := range rt.Statuses() {
|
||
st[s.ID] = s
|
||
}
|
||
}
|
||
out := []builtinJSON{}
|
||
for _, m := range pluginrt.Registered() {
|
||
c, s := byID[m.ID], st[m.ID]
|
||
j := builtinJSON{ID: m.ID, Name: m.Name, Sub: m.Sub, On: s.State == pluginrt.Active, Chosen: c.Chosen, Source: c.Source, State: s.State, Why: s.Why, InUse: c.InUse, Parts: s.Parts, Schema: m.Schema(), Config: c.Config}
|
||
if err != nil {
|
||
j.Broken, j.Chosen = err.Error(), j.On
|
||
}
|
||
switch {
|
||
case j.InUse == 1:
|
||
j.Use = m.One
|
||
case j.InUse > 1:
|
||
j.Use = m.Many
|
||
}
|
||
out = append(out, j)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// builtinsOff are the ids of the plugins the window leaves out now, for
|
||
// boot.js to hide them before the page paints.
|
||
func builtinsOff(list []builtinJSON) []string {
|
||
out := []string{}
|
||
for _, b := range list {
|
||
if !b.On {
|
||
out = append(out, b.ID)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// builtinConfig is each plugin's settings the window shows, by id, for
|
||
// boot.js to hand the page's plugins before they draw.
|
||
func builtinConfig(list []builtinJSON) map[string]map[string]any {
|
||
out := map[string]map[string]any{}
|
||
for _, b := range list {
|
||
if b.On && b.Schema != nil {
|
||
out[b.ID] = b.Config
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// builtinRoutes: GET /api/plugins/builtin is every built-in plugin; POST
|
||
// {"id", "on"} switches one, kept in plugins.yml as the user's own choice,
|
||
// and {"id", "config"} sets its settings, only as their schema allows.
|
||
// One in use is refused off.
|
||
func builtinRoutes(mux *http.ServeMux) {
|
||
mux.HandleFunc("GET /api/plugins/builtin", func(rw http.ResponseWriter, r *http.Request) {
|
||
writeJSON(rw, builtinsState())
|
||
})
|
||
mux.HandleFunc("POST /api/plugins/builtin", func(rw http.ResponseWriter, r *http.Request) {
|
||
var in struct {
|
||
ID string `json:"id"`
|
||
On *bool `json:"on"`
|
||
Config map[string]any `json:"config"`
|
||
}
|
||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
var b *builtinJSON
|
||
for _, x := range builtinsState() {
|
||
if x.ID == in.ID {
|
||
b = &x
|
||
break
|
||
}
|
||
}
|
||
switch {
|
||
case b == nil:
|
||
fail(rw, fmt.Errorf("no built-in plugin %q", in.ID))
|
||
return
|
||
case b.Broken != "":
|
||
fail(rw, fmt.Errorf("%s", b.Broken))
|
||
return
|
||
case (in.On == nil) == (in.Config == nil):
|
||
fail(rw, fmt.Errorf("say on or config"))
|
||
return
|
||
case in.On != nil && !*in.On && b.InUse > 0:
|
||
fail(rw, fmt.Errorf("%s is in use and stays on", b.Name))
|
||
return
|
||
}
|
||
var err error
|
||
if in.On != nil {
|
||
err = pluginrt.Set(in.ID, *in.On)
|
||
} else {
|
||
err = pluginrt.SetConfig(in.ID, in.Config)
|
||
}
|
||
if err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
writeJSON(rw, builtinsState())
|
||
})
|
||
}
|