pluginsdk/plugin/topo_sort.go
Alex Dunmow b6d40ed8ac feat: bootstrap block/pluginsdk — the proto-first plugin SDK (P1)
Bootstrap the plugin-facing SDK module so the fleet migration (P2) becomes a
mechanical block/core/X -> block/pluginsdk/X import rewrite.

- abi/proto/v1: the single source-of-truth ABI proto tree, copied from the cms
  authoring source (cms/backend/abi/proto/v1) with go_package retargeted to
  git.dev.alexdunmow.com/block/pluginsdk/abi/v1;abiv1. Kills the hand-kept
  cms/core proto duplication (audit gap 4).
- abi/v1: generated Go bindings (buf generate); byte-identical to core's abi/v1
  save the embedded go_package path.
- plugin/ (registration + DI surface), plugin/wasmguest/** (transport shim,
  caps stubs, bnwasm db driver, testdata fixtures + golden .pb), and the
  guest-facing type packages: blocks (+builtin/shared/tags), templates
  (+pongo/bn), auth, settings, content, gating, crypto, rbac, video, ai,
  subscriptions, menus, datasources. Internal imports rewritten core ->
  pluginsdk; zero block/core references remain.
- README/AGENTS(+CLAUDE symlink)/Makefile: proto is the contract, Go is one
  binding; no replace directives; templates/bn is a synced copy authored in cms.

Spec: cms docs/superpowers/specs/2026-07-07-proto-first-plugin-sdk-design.md

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 10:51:00 +08:00

65 lines
1.5 KiB
Go

package plugin
import "fmt"
// TopologicalSort orders plugins so that dependencies come before dependents.
// Returns error on circular dependencies or missing required dependencies.
func TopologicalSort(plugins []PluginRegistration) ([]PluginRegistration, error) {
byName := make(map[string]*PluginRegistration, len(plugins))
for i := range plugins {
byName[plugins[i].Name] = &plugins[i]
}
for _, p := range plugins {
for _, dep := range p.Dependencies {
if dep.Required {
if _, ok := byName[dep.Plugin]; !ok {
return nil, fmt.Errorf("plugin %q requires %q, but it is not available", p.Name, dep.Plugin)
}
}
}
}
inDegree := make(map[string]int, len(plugins))
dependents := make(map[string][]string)
for _, p := range plugins {
if _, ok := inDegree[p.Name]; !ok {
inDegree[p.Name] = 0
}
for _, dep := range p.Dependencies {
if _, ok := byName[dep.Plugin]; ok {
inDegree[p.Name]++
dependents[dep.Plugin] = append(dependents[dep.Plugin], p.Name)
}
}
}
var queue []string
for _, p := range plugins {
if inDegree[p.Name] == 0 {
queue = append(queue, p.Name)
}
}
var result []PluginRegistration
for len(queue) > 0 {
name := queue[0]
queue = queue[1:]
result = append(result, *byName[name])
for _, dep := range dependents[name] {
inDegree[dep]--
if inDegree[dep] == 0 {
queue = append(queue, dep)
}
}
}
if len(result) != len(plugins) {
return nil, fmt.Errorf("circular dependency detected among plugins")
}
return result, nil
}