/
scaffold.go
141 lines (122 loc) · 3.98 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2021 Mya Pitzeruse
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package commands
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"github.com/mjpitz/myago/cmd/em/internal/scaffold"
"github.com/mjpitz/myago/flagset"
"github.com/mjpitz/myago/vfs"
"github.com/mjpitz/myago/zaputil"
)
const scaffoldHelpTemplate = `
Features:
{{- range $feature, $files := .features }}
- {{ $feature }}
{{- end }}
Aliases:
{{- range $alias, $targets := .aliases }}
- {{ $alias }}: {{ join $targets ", " }}
{{- end }}
`
type ScaffoldConfig struct {
Mkdir bool `json:"mkdir" usage:"specify if we should make the target project directory"`
License string `json:"license" usage:"specify which license should be applied to the project" default:"agpl3"`
Features *cli.StringSlice `json:"features" usage:"specify the features to generate"`
}
var (
scaffoldConfig = &ScaffoldConfig{}
Scaffold = &cli.Command{
Name: "scaffold",
Usage: "Scaffold out a new project or add onto an existing one.",
UsageText: flagset.ExampleString(
"em scaffold [options] <name>",
"em scaffold features # will output a list of features and aliases",
"em scaffold --mkdir --license mpl --features init <name>",
"em scaffold --mkdir --license mpl --features init --features bin <name>",
),
Flags: flagset.ExtractPrefix("em", scaffoldConfig),
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
return fmt.Errorf("name not specified")
}
name := ctx.Args().Get(0)
if name == "features" {
return template.Must(
template.New("scaffold-help").
Funcs(map[string]interface{}{
"join": func(elems []string, sep string) string {
return strings.Join(elems, sep)
},
}).
Parse(scaffoldHelpTemplate),
).Execute(ctx.App.Writer, map[string]interface{}{
"features": scaffold.FilesByFeature,
"aliases": scaffold.FeatureAliases,
})
}
if scaffoldConfig.Mkdir {
zaputil.Extract(ctx.Context).Info("making directory")
if err := os.MkdirAll(name, 0755); err != nil {
return errors.Wrap(err, "failed to make project directory")
}
if err := os.Chdir(name); err != nil {
return errors.Wrap(err, "failed to change into directory")
}
}
zaputil.Extract(ctx.Context).Info("rendering files")
files := scaffold.Template(
scaffold.Data{
Name: name,
License: scaffoldConfig.License,
Features: scaffoldConfig.Features.Value(),
},
).Render(ctx.Context)
zaputil.Extract(ctx.Context).Info("writing files")
afs := vfs.Extract(ctx.Context)
for _, file := range files {
dir := filepath.Dir(file.Name)
_ = afs.MkdirAll(dir, 0755)
if exists, _ := afero.Exists(afs, file.Name); exists {
// don't overwrite existing files
continue
}
zaputil.Extract(ctx.Context).Info("writing file", zap.String("file", file.Name))
err := afero.WriteFile(afs, file.Name, file.Contents, 0644)
if err != nil {
return err
}
}
if scaffoldConfig.Mkdir {
if exists, _ := afero.Exists(afs, "go.mod"); exists {
_, err := exec.Command("go", "mod", "tidy").CombinedOutput()
if err != nil {
return err
}
}
}
return nil
},
HideHelpCommand: true,
}
)