Skip to content

Commit

Permalink
feat: add 'repoman template-post-clone --rm-go-binary'
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed May 27, 2021
1 parent 3329958 commit 611ae16
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func (p *project) showDiff() error {
}

func (p *project) openPR(branchName string, title string) error {
logger.Debug("opening a PR", zap.String("branch", branchName), zap.String("title", title))
initMoulBotEnv()
script := `
main() {
Expand Down
66 changes: 66 additions & 0 deletions template-post-clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"go.uber.org/multierr"
Expand Down Expand Up @@ -49,8 +52,71 @@ func doTemplatePostCloneOnce(_ context.Context, path string) error {
}
}

// rm go binary
if opts.TemplatePostClone.RemoveGoBinary {
logger.Debug("remove go binary", zap.String("project", project.Path))
// git rm main*.go
err := project.Git.workTree.RemoveGlob("main*.go")
if err != nil {
return fmt.Errorf("rm main*.go: %w", err)
}

// patch Makefile
{
path := filepath.Join(project.Git.Root, "Makefile")
content, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("read Makefile: %w", err)
}
content = regexp.MustCompile(`(?m)^(DOCKER_IMAGE|GOBINS|NPM_PACKAGES) .=.*\n`).ReplaceAll(content, []byte(""))
content = regexp.MustCompile(`(?ms)^generate:.*.PHONY: generate\n\n`).ReplaceAll(content, []byte(""))
err = ioutil.WriteFile(path, content, 0)
if err != nil {
return fmt.Errorf("write file: %q: %w", path, err)
}
if _, err := project.Git.workTree.Add("Makefile"); err != nil {
return fmt.Errorf("git add %q: %w", path, err)
}
}

// remove files
{
for _, filename := range []string{"Dockerfile", ".goreleaser.yml", ".github/workflows/docker.yml"} {
_, err := project.Git.workTree.Remove(filename)
if err != nil {
return fmt.Errorf("git rm %q: %w", filename, err)
}
}
}

// perform various tasks
{
script := `
main() {
make generate go.depaware-update
git add AUTHORS README.md depaware.txt
make tidy
git add go.mod go.sum
git status
}
main
`
cmd := exec.Command("/bin/sh", "-xec", script)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Dir = project.Path
cmd.Env = os.Environ()

err := cmd.Run()
if err != nil {
return fmt.Errorf("standard script execution failed: %w", err)
}
}
}

// find and replace
{
logger.Debug("patch files to remove template strings", zap.String("project", project.Path))
visit := func(path string, info fs.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walk dir: %q: %w", path, err)
Expand Down

0 comments on commit 611ae16

Please sign in to comment.