-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffold.go
63 lines (57 loc) · 1.63 KB
/
scaffold.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package plugin
import (
"context"
"embed"
"os"
"path"
"path/filepath"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/plush/v4"
"github.com/pkg/errors"
"github.com/spellshape/cli/spellshape/pkg/cmdrunner/exec"
"github.com/spellshape/cli/spellshape/pkg/cmdrunner/step"
"github.com/spellshape/cli/spellshape/pkg/gocmd"
"github.com/spellshape/cli/spellshape/pkg/xgenny"
)
//go:embed template/*
var fsPluginSource embed.FS
// Scaffold generates a plugin structure under dir/path.Base(moduleName).
func Scaffold(dir, moduleName string, sharedHost bool) (string, error) {
var (
name = filepath.Base(moduleName)
finalDir = path.Join(dir, name)
g = genny.New()
template = xgenny.NewEmbedWalker(
fsPluginSource,
"template",
finalDir,
)
)
if _, err := os.Stat(finalDir); err == nil {
// finalDir already exists, don't overwrite stuff
return "", errors.Errorf("dir %q already exists, abort scaffolding", finalDir)
}
if err := g.Box(template); err != nil {
return "", errors.WithStack(err)
}
ctx := plush.NewContext()
ctx.Set("ModuleName", moduleName)
ctx.Set("Name", name)
ctx.Set("SharedHost", sharedHost)
g.Transformer(xgenny.Transformer(ctx))
r := genny.WetRunner(ctx)
err := r.With(g)
if err != nil {
return "", errors.WithStack(err)
}
if err := r.Run(); err != nil {
return "", errors.WithStack(err)
}
// FIXME(tb) we need to disable sumdb to get the branch version of CLI
// because our git history is too fat.
opt := exec.StepOption(step.Env("GOSUMDB=off"))
if err := gocmd.ModTidy(context.TODO(), finalDir, opt); err != nil {
return "", errors.WithStack(err)
}
return finalDir, nil
}