feat(14.3-01): add summer new with a starter route and relative go.work
- build.NewApp writes sm-<name>-app: go.mod with a relative framework
replace, go.work, summer.yaml, config/ and a starter plugin
- make:plugin gains an opt-in starter route (GET /{$}) in the same
routes.go template; plain make:plugin output is unchanged
- plugin:add writes go.work use paths relative to the go.work directory
- bonfire accepts new as a bare tool command name
This commit is contained in:
@@ -40,6 +40,14 @@ type stubData struct {
|
||||
GoVersion string
|
||||
Toolchain string
|
||||
Replace string
|
||||
// StarterRoute renders routes.go with a GET / route (summer new).
|
||||
StarterRoute bool
|
||||
}
|
||||
|
||||
// pluginOptions tunes the scaffold makePlugin writes. The zero value is the
|
||||
// plain make:plugin output.
|
||||
type pluginOptions struct {
|
||||
StarterRoute bool
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -50,6 +58,10 @@ const (
|
||||
|
||||
// MakePlugin scaffolds a compiling plugin module under plugins/<name>.
|
||||
func MakePlugin(ctx context.Context, startDir, id string) (string, error) {
|
||||
return makePlugin(ctx, startDir, id, pluginOptions{})
|
||||
}
|
||||
|
||||
func makePlugin(ctx context.Context, startDir, id string, opts pluginOptions) (string, error) {
|
||||
if err := ValidatePluginID(id); err != nil {
|
||||
return "", fmt.Errorf("build: %w", err)
|
||||
}
|
||||
@@ -89,6 +101,8 @@ func MakePlugin(ctx context.Context, startDir, id string) (string, error) {
|
||||
GoVersion: goVersionLine,
|
||||
Toolchain: toolchainLine,
|
||||
Replace: replace,
|
||||
|
||||
StarterRoute: opts.StarterRoute,
|
||||
}
|
||||
if err := writePluginScaffold(pluginDir, data); err != nil {
|
||||
return "", err
|
||||
@@ -319,14 +333,10 @@ 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)
|
||||
rel, err := relModulePath(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
|
||||
}
|
||||
@@ -342,13 +352,13 @@ func ensureGoWork(ctx context.Context, appDir string, m Manifest, pluginDir stri
|
||||
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 {
|
||||
if err := runGoWorkUse(ctx, gw, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return runGoWorkUse(ctx, appDir, gw, pluginDir)
|
||||
return runGoWorkUse(ctx, gw, pluginDir)
|
||||
}
|
||||
|
||||
func writeGoWork(path string) error {
|
||||
@@ -359,9 +369,32 @@ func writeGoWork(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runGoWorkUse(ctx context.Context, appDir, goWork, dir string) error {
|
||||
cmd := exec.CommandContext(ctx, "go", "work", "use", dir)
|
||||
cmd.Dir = appDir
|
||||
// relModulePath returns target relative to base as a slash-separated local
|
||||
// module path: "./x", "../x", "." or "..". Go reads a replace or use path
|
||||
// without a ./ or ../ prefix as a module path, so the prefix is required.
|
||||
func relModulePath(base, target string) (string, error) {
|
||||
rel, err := filepath.Rel(base, target)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
if rel != "." && rel != ".." && !strings.HasPrefix(rel, "../") && !filepath.IsAbs(rel) && !strings.HasPrefix(rel, "./") {
|
||||
rel = "./" + rel
|
||||
}
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
// runGoWorkUse adds dir to goWork with a path relative to the go.work
|
||||
// directory, so a committed go.work never carries a machine-specific
|
||||
// absolute path.
|
||||
func runGoWorkUse(ctx context.Context, goWork, dir string) error {
|
||||
workDir := filepath.Dir(goWork)
|
||||
arg := dir
|
||||
if rel, err := relModulePath(workDir, dir); err == nil {
|
||||
arg = rel
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "go", "work", "use", arg)
|
||||
cmd.Dir = workDir
|
||||
applyGoWorkFile(cmd, goWork)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user