fix(build): scaffold plugins under plugins/<vendor>/<name> (quick-261028-pq8)
- make:plugin writes plugins/<vendor>/<name> with module <app>/plugins/<vendor>/<name>; the package name stays <name> - make:* and the models-leaf check look in summer.yaml, then plugins/<vendor>/<name>, then the older plugins/<name> - summer new puts its starter plugin at plugins/golem15/<name> - refuse a vendor directory that is a legacy plugin module and a second copy of a plugin that already sits in plugins/<name> - help text and docs, with an upgrading note: moving is optional
This commit is contained in:
@@ -58,7 +58,9 @@ const (
|
||||
toolchainLine = "toolchain go1.27.0"
|
||||
)
|
||||
|
||||
// MakePlugin scaffolds a compiling plugin module under plugins/<name>.
|
||||
// MakePlugin scaffolds a compiling plugin module under plugins/<vendor>/<name>
|
||||
// for the ID <vendor>.<name>; its module path is the application module
|
||||
// followed by /plugins/<vendor>/<name>, and its Go package is <name>.
|
||||
func MakePlugin(ctx context.Context, startDir, id string) (string, error) {
|
||||
return makePlugin(ctx, startDir, id, pluginOptions{})
|
||||
}
|
||||
@@ -73,24 +75,39 @@ func makePlugin(ctx context.Context, startDir, id string, opts pluginOptions) (s
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
name := pluginName(id)
|
||||
pluginDir := filepath.Join(appDir, "plugins", name)
|
||||
vendor, name := pluginVendor(id), pluginName(id)
|
||||
pluginDir := filepath.Join(appDir, "plugins", vendor, name)
|
||||
if !underRoot(appDir, pluginDir) {
|
||||
return "", fmt.Errorf("build: plugin path %q escapes the app root", pluginDir)
|
||||
}
|
||||
addArg := "plugins/" + vendor + "/" + name
|
||||
if _, err := os.Stat(pluginDir); err == nil {
|
||||
return "", bonfire.WithHint(fmt.Errorf("build: plugin directory %s already exists", pluginDir),
|
||||
"summer make:plugin creates a new directory under plugins/ and never overwrites one; register an existing plugin with plugin:add.",
|
||||
"{bin} plugin:add "+filepath.Join("plugins", name))
|
||||
"{bin} plugin:add "+addArg)
|
||||
} else if !os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("build: stat %s: %w", pluginDir, err)
|
||||
}
|
||||
// A legacy plugins/<vendor> module would swallow the new directory, and a
|
||||
// legacy plugins/<name> with this ID is the same plugin.
|
||||
vendorDir := filepath.Join(appDir, "plugins", vendor)
|
||||
if _, err := os.Stat(filepath.Join(vendorDir, "go.mod")); err == nil {
|
||||
return "", bonfire.WithHint(fmt.Errorf("build: %s is a plugin module, not a vendor directory", vendorDir),
|
||||
"summer make:plugin writes plugins/<vendor>/<name>; plugins/"+vendor+" already holds a plugin in the older plugins/<name> layout. Move that plugin first, or choose another vendor.",
|
||||
"{bin} make:plugin <vendor>."+name)
|
||||
}
|
||||
legacyDir := filepath.Join(appDir, "plugins", name)
|
||||
if got, err := pluginIDFromGo(filepath.Join(legacyDir, "plugin.go")); err == nil && got == id {
|
||||
return "", bonfire.WithHint(fmt.Errorf("build: plugin %s already exists in %s", id, legacyDir),
|
||||
"summer make:plugin never creates a second copy of a plugin; register the existing one with plugin:add.",
|
||||
"{bin} plugin:add plugins/"+name)
|
||||
}
|
||||
|
||||
m, err := LoadManifest(filepath.Join(appDir, manifestName))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
modPath := m.Module + "/plugins/" + name
|
||||
modPath := m.Module + "/" + addArg
|
||||
if err := validateModulePath(modPath); err != nil {
|
||||
return "", fmt.Errorf("build: plugin module: %w", err)
|
||||
}
|
||||
@@ -197,6 +214,44 @@ func pluginName(id string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
// pluginVendor returns the vendor part of a plugin ID, before the dot.
|
||||
func pluginVendor(id string) string {
|
||||
vendor, _, _ := strings.Cut(id, ".")
|
||||
return vendor
|
||||
}
|
||||
|
||||
// pluginDirCandidates lists the default directories of a plugin that
|
||||
// summer.yaml does not place, in lookup order: plugins/<vendor>/<name>, the
|
||||
// layout make:plugin writes, then the older plugins/<name>.
|
||||
func pluginDirCandidates(appDir, id string) []string {
|
||||
return []string{
|
||||
filepath.Join(appDir, "plugins", pluginVendor(id), pluginName(id)),
|
||||
filepath.Join(appDir, "plugins", pluginName(id)),
|
||||
}
|
||||
}
|
||||
|
||||
// defaultPluginDir returns the first of pluginDirCandidates that is an
|
||||
// existing directory, or "" when none is. A candidate outside the app root is
|
||||
// an error.
|
||||
func defaultPluginDir(appDir, id string) (string, error) {
|
||||
for _, dir := range pluginDirCandidates(appDir, id) {
|
||||
if !underRoot(appDir, dir) {
|
||||
return "", fmt.Errorf("build: plugin path %q escapes the app root", dir)
|
||||
}
|
||||
st, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
return "", fmt.Errorf("build: stat %s: %w", dir, err)
|
||||
}
|
||||
if st.IsDir() {
|
||||
return dir, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func writePluginScaffold(pluginDir string, data stubData) error {
|
||||
if err := os.MkdirAll(pluginDir, 0o755); err != nil {
|
||||
return fmt.Errorf("build: create plugin dir: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user