Files
summercms/internal/build/scaffold.go
Jakub Zych 9b1a25dc94 feat(01-03): add make:plugin and plugin:add scaffolding
- Scaffold a compiling vendor.plugin module with go.mod, plugin.go and config/
- Register the module once in summer.yaml, go.work and the app go.mod
- Keep summer build on the same generate-and-compile path and print elapsed time
2026-09-16 13:47:27 +02:00

477 lines
13 KiB
Go

package build
import (
"bytes"
"context"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
const (
frameworkModule = "git.golem15.com/golem15/summercms"
goVersionLine = "go 1.27.0"
toolchainLine = "toolchain go1.27.0"
)
// MakePlugin scaffolds a compiling plugin module under plugins/<name>.
func MakePlugin(ctx context.Context, startDir, id string) (string, error) {
if err := ValidatePluginID(id); err != nil {
return "", fmt.Errorf("build: %w", err)
}
appDir, err := findAppDir(startDir)
if err != nil {
return "", err
}
name := pluginName(id)
pluginDir := filepath.Join(appDir, "plugins", name)
if !underRoot(appDir, pluginDir) {
return "", fmt.Errorf("build: plugin path %q escapes the app root", pluginDir)
}
if _, err := os.Stat(pluginDir); err == nil {
return "", fmt.Errorf("build: plugin directory %s already exists", pluginDir)
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("build: stat %s: %w", pluginDir, err)
}
m, err := LoadManifest(filepath.Join(appDir, manifestName))
if err != nil {
return "", err
}
modPath := m.Module + "/plugins/" + name
if err := validateModulePath(modPath); err != nil {
return "", fmt.Errorf("build: plugin module: %w", err)
}
if err := os.MkdirAll(filepath.Join(pluginDir, "config"), 0o755); err != nil {
return "", fmt.Errorf("build: create plugin dir: %w", err)
}
if err := os.WriteFile(filepath.Join(pluginDir, "config", ".gitkeep"), []byte{}, 0o644); err != nil {
return "", fmt.Errorf("build: write config placeholder: %w", err)
}
src, err := pluginGoSource(name, id)
if err != nil {
return "", err
}
if err := os.WriteFile(filepath.Join(pluginDir, "plugin.go"), src, 0o644); err != nil {
return "", fmt.Errorf("build: write plugin.go: %w", err)
}
replace, err := frameworkReplaceFor(pluginDir, appDir)
if err != nil {
return "", err
}
if err := os.WriteFile(filepath.Join(pluginDir, "go.mod"), pluginGoMod(modPath, replace), 0o644); err != nil {
return "", fmt.Errorf("build: write go.mod: %w", err)
}
if err := runGo(ctx, pluginDir, "mod", "tidy"); err != nil {
return "", err
}
if err := ensureToolchain(filepath.Join(pluginDir, "go.mod")); err != nil {
return "", err
}
return pluginDir, nil
}
// AddPlugin registers a local plugin module in summer.yaml, go.work and the app go.mod.
func AddPlugin(ctx context.Context, startDir, pluginDir string) error {
appDir, err := findAppDir(startDir)
if err != nil {
return err
}
if pluginDir == "" {
return fmt.Errorf("build: plugin directory is empty")
}
if !filepath.IsAbs(pluginDir) {
pluginDir = filepath.Join(startDir, pluginDir)
}
pluginDir, err = filepath.Abs(pluginDir)
if err != nil {
return fmt.Errorf("build: resolve plugin directory: %w", err)
}
if !underRoot(appDir, pluginDir) {
return fmt.Errorf("build: plugin directory %q is outside the app root", pluginDir)
}
modPath, err := readModulePath(filepath.Join(pluginDir, "go.mod"))
if err != nil {
return err
}
if err := validateModulePath(modPath); err != nil {
return fmt.Errorf("build: plugin module: %w", err)
}
id, err := pluginIDFromGo(filepath.Join(pluginDir, "plugin.go"))
if err != nil {
return err
}
if err := ValidatePluginID(id); err != nil {
return fmt.Errorf("build: %w", err)
}
manifestPath := filepath.Join(appDir, manifestName)
m, err := LoadManifest(manifestPath)
if err != nil {
return err
}
already := false
for _, p := range m.Plugins {
if p.ID == id && p.Module == modPath {
already = true
continue
}
if p.ID == id {
return fmt.Errorf("build: plugin id %q is already registered as %s", id, p.Module)
}
if p.Module == modPath {
return fmt.Errorf("build: plugin module %q is already registered as %s", modPath, p.ID)
}
}
if !already {
m.Plugins = append(m.Plugins, Plugin{ID: id, Module: modPath})
}
if err := editAppModule(ctx, appDir, pluginDir, modPath); err != nil {
return err
}
if err := ensureGoWork(ctx, appDir, m, pluginDir); err != nil {
return err
}
if already {
return nil
}
return SaveManifest(manifestPath, m)
}
func pluginName(id string) string {
_, name, _ := strings.Cut(id, ".")
return name
}
func pluginGoSource(pkg, id string) ([]byte, error) {
var b strings.Builder
b.WriteString("package " + pkg + "\n\n")
b.WriteString("import (\n")
b.WriteString("\t\"git.golem15.com/golem15/summercms/backpack\"\n")
b.WriteString("\t\"git.golem15.com/golem15/summercms/party\"\n")
b.WriteString(")\n\n")
fmt.Fprintf(&b, "// Plugin is the %s plugin.\n", id)
b.WriteString("type Plugin struct{}\n\n")
fmt.Fprintf(&b, "func (p *Plugin) ID() string { return %s }\n", strconv.Quote(id))
b.WriteString("func (p *Plugin) Requires() []string { return nil }\n\n")
b.WriteString("func (p *Plugin) Register(*backpack.App) error { return nil }\n")
b.WriteString("func (p *Plugin) Boot(*backpack.App) error { return nil }\n\n")
b.WriteString("func init() {\n")
b.WriteString("\tparty.Register(&Plugin{})\n")
b.WriteString("}\n")
formatted, err := format.Source([]byte(b.String()))
if err != nil {
return nil, fmt.Errorf("build: format plugin.go: %w", err)
}
return formatted, nil
}
func pluginGoMod(modPath, replace string) []byte {
var b strings.Builder
fmt.Fprintf(&b, "module %s\n\n", modPath)
b.WriteString(goVersionLine + "\n\n")
b.WriteString(toolchainLine + "\n\n")
fmt.Fprintf(&b, "require %s v0.0.0\n", frameworkModule)
if replace != "" {
fmt.Fprintf(&b, "\nreplace %s => %s\n", frameworkModule, replace)
}
return []byte(b.String())
}
func frameworkReplaceFor(pluginDir, appDir string) (string, error) {
_, replaces, err := parseGoMod(filepath.Join(appDir, "go.mod"))
if err != nil {
return "", err
}
appReplace, ok := replaces[frameworkModule]
if !ok || appReplace == "" {
return "", nil
}
target := appReplace
if filepath.IsAbs(appReplace) {
return filepath.ToSlash(appReplace), nil
}
target = filepath.Join(appDir, target)
target, err = filepath.Abs(target)
if err != nil {
return "", fmt.Errorf("build: plugin replace: %w", err)
}
rel, err := filepath.Rel(pluginDir, target)
if err != nil {
return filepath.ToSlash(target), nil
}
return filepath.ToSlash(rel), nil
}
func pluginIDFromGo(path string) (string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return "", fmt.Errorf("build: parse %s: %w", path, err)
}
var id string
ast.Inspect(file, func(n ast.Node) bool {
fn, ok := n.(*ast.FuncDecl)
if !ok || fn.Name == nil || fn.Name.Name != "ID" || fn.Recv == nil || fn.Body == nil {
return true
}
for _, stmt := range fn.Body.List {
ret, ok := stmt.(*ast.ReturnStmt)
if !ok || len(ret.Results) != 1 {
continue
}
lit, ok := ret.Results[0].(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
continue
}
s, err := strconv.Unquote(lit.Value)
if err == nil {
id = s
}
}
return true
})
if id == "" {
return "", fmt.Errorf("build: %s does not return a plugin id", path)
}
return id, nil
}
func editAppModule(ctx context.Context, appDir, pluginDir, modPath string) error {
if err := runGo(ctx, appDir, "mod", "edit", "-require="+modPath+"@v0.0.0"); err != nil {
return err
}
rel, err := filepath.Rel(appDir, pluginDir)
if err != nil {
return fmt.Errorf("build: plugin replace: %w", err)
}
rel = filepath.ToSlash(rel)
if rel != ".." && !strings.HasPrefix(rel, "../") && !filepath.IsAbs(rel) && !strings.HasPrefix(rel, "./") {
rel = "./" + rel
}
if err := runGo(ctx, appDir, "mod", "edit", "-replace="+modPath+"="+rel); err != nil {
return err
}
return ensureToolchain(filepath.Join(appDir, "go.mod"))
}
func ensureGoWork(ctx context.Context, appDir string, m Manifest, pluginDir string) error {
gw := findGoWork(appDir)
if gw == "" {
if err := writeGoWork(filepath.Join(appDir, "go.work")); err != nil {
return err
}
gw = filepath.Join(appDir, "go.work")
for _, p := range m.Plugins {
if dir := localModuleDir(appDir, p.Module); dir != "" && dir != pluginDir {
if err := runGoWorkUse(ctx, appDir, gw, dir); err != nil {
return err
}
}
}
}
return runGoWorkUse(ctx, appDir, gw, pluginDir)
}
func writeGoWork(path string) error {
body := goVersionLine + "\n\n" + toolchainLine + "\n\nuse (\n\t.\n)\n"
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
return fmt.Errorf("build: write go.work: %w", err)
}
return nil
}
func runGoWorkUse(ctx context.Context, appDir, goWork, dir string) error {
cmd := exec.CommandContext(ctx, "go", "work", "use", dir)
cmd.Dir = appDir
applyGoWorkFile(cmd, goWork)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("build: go work use: %w\n%s", err, out)
}
return ensureToolchain(goWork)
}
func localModuleDir(appDir, module string) string {
_, replaces, err := parseGoMod(filepath.Join(appDir, "go.mod"))
if err != nil {
return ""
}
r, ok := replaces[module]
if !ok || r == "" {
return ""
}
if filepath.IsAbs(r) {
return r
}
return filepath.Clean(filepath.Join(appDir, r))
}
func readModulePath(path string) (string, error) {
module, _, err := parseGoMod(path)
if err != nil {
return "", err
}
if module == "" {
return "", fmt.Errorf("build: %s has no module path", path)
}
return module, nil
}
func parseGoMod(path string) (string, map[string]string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", nil, fmt.Errorf("build: read %s: %w", path, err)
}
module := ""
replaces := make(map[string]string)
inReplace := false
for _, raw := range strings.Split(string(data), "\n") {
line := strings.TrimSpace(raw)
if i := strings.Index(line, "//"); i >= 0 {
line = strings.TrimSpace(line[:i])
}
if line == "" {
continue
}
if module == "" && strings.HasPrefix(line, "module ") {
module = strings.TrimSpace(strings.TrimPrefix(line, "module "))
continue
}
if line == "replace (" {
inReplace = true
continue
}
if inReplace {
if line == ")" {
inReplace = false
continue
}
if old, new, ok := splitReplace(line); ok {
replaces[old] = new
}
continue
}
if strings.HasPrefix(line, "replace ") {
if old, new, ok := splitReplace(strings.TrimPrefix(line, "replace ")); ok {
replaces[old] = new
}
}
}
return module, replaces, nil
}
func splitReplace(s string) (old, new string, ok bool) {
parts := strings.SplitN(s, "=>", 2)
if len(parts) != 2 {
return "", "", false
}
oldFields := strings.Fields(strings.TrimSpace(parts[0]))
newFields := strings.Fields(strings.TrimSpace(parts[1]))
if len(oldFields) == 0 || len(newFields) == 0 {
return "", "", false
}
return oldFields[0], newFields[0], true
}
func ensureToolchain(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("build: read %s: %w", path, err)
}
if bytes.Contains(data, []byte(toolchainLine)) {
return nil
}
lines := strings.Split(string(data), "\n")
out := make([]string, 0, len(lines)+2)
inserted := false
for _, line := range lines {
out = append(out, line)
if !inserted && strings.HasPrefix(strings.TrimSpace(line), "go ") {
out = append(out, "", toolchainLine)
inserted = true
}
}
if !inserted {
out = append([]string{toolchainLine, ""}, out...)
}
if err := os.WriteFile(path, []byte(strings.Join(out, "\n")), 0o644); err != nil {
return fmt.Errorf("build: write %s: %w", path, err)
}
return nil
}
func underRoot(root, path string) bool {
rel, err := filepath.Rel(root, path)
if err != nil {
return false
}
if rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
return false
}
return true
}
func findGoWork(start string) string {
dir := start
for {
candidate := filepath.Join(dir, "go.work")
if st, err := os.Stat(candidate); err == nil && !st.IsDir() {
return candidate
}
parent := filepath.Dir(dir)
if parent == dir {
return ""
}
dir = parent
}
}
func runGo(ctx context.Context, dir string, args ...string) error {
cmd := exec.CommandContext(ctx, "go", args...)
cmd.Dir = dir
applyGoWork(cmd, dir)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("build: go %s: %w\n%s", strings.Join(args, " "), err, out)
}
return nil
}
func applyGoWork(cmd *exec.Cmd, dir string) {
if gw := findGoWork(dir); gw != "" {
applyGoWorkFile(cmd, gw)
return
}
applyGoWorkFile(cmd, "off")
}
func applyGoWorkFile(cmd *exec.Cmd, goWork string) {
env := os.Environ()
filtered := make([]string, 0, len(env)+2)
for _, e := range env {
if strings.HasPrefix(e, "GOWORK=") || strings.HasPrefix(e, "GOFLAGS=") {
continue
}
filtered = append(filtered, e)
}
filtered = append(filtered, "GOWORK="+goWork)
if goWork == "off" {
filtered = append(filtered, "GOFLAGS=-mod=mod")
}
cmd.Env = filtered
}