-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen.go
80 lines (71 loc) · 2.12 KB
/
gen.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package launchr
import (
"os"
"strings"
"github.com/launchrctl/launchr/internal/launchr"
)
func (app *appImpl) gen() error {
var err error
config := GenerateConfig{
WorkDir: ".",
BuildDir: ".",
}
isRelease := false
app.cmd.RunE = func(cmd *Command, _ []string) error {
// Don't show usage help on a runtime error.
cmd.SilenceUsage = true
// Set absolute paths.
config.WorkDir = launchr.MustAbs(config.WorkDir)
config.BuildDir = launchr.MustAbs(config.BuildDir)
// Change working directory to the selected.
err = os.Chdir(config.WorkDir)
if err != nil {
return err
}
// Call generate functions on plugins.
for _, p := range launchr.GetPluginByType[GeneratePlugin](app.pluginMngr) {
if !isRelease && strings.HasPrefix(p.K.GetPackagePath(), PkgPath) {
// Skip core packages if not requested.
// Implemented for development of plugins to prevent generating of main.go.
continue
}
err = p.V.Generate(config)
if err != nil {
return err
}
}
return nil
}
// Do not fail if some flags change in future builds.
flags := app.cmd.Flags()
flags.ParseErrorsWhitelist.UnknownFlags = true
// Working directory flag is helpful because "go run" can't be run outside
// a project directory and use all its go.mod dependencies.
flags.StringVar(&config.WorkDir, "work-dir", config.WorkDir, "Working directory")
flags.StringVar(&config.BuildDir, "build-dir", config.BuildDir, "Build directory where the files will be generated")
flags.BoolVar(&isRelease, "release", isRelease, "Generate core plugins and main.go")
return app.cmd.Execute()
}
// Generate runs generation of included plugins.
func (app *appImpl) Generate() int {
// Do not discover actions on generate.
var err error
if err = app.init(); err != nil {
Term().Error().Println(err)
return 125
}
if err = app.gen(); err != nil {
Term().Error().Println(err)
return 125
}
return 0
}
// Gen generates application specific build files and returns os exit code.
func Gen() int {
launchr.IsGen = true
return newApp().Generate()
}
// GenAndExit runs the generation and exits with a result code.
func GenAndExit() {
os.Exit(Gen())
}