Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding go version and std_package_list to releaser #1415

Merged
merged 6 commits into from
Jan 14, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions tools/releaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
"github.com/bazelbuild/bazel-gazelle/rule"
Expand All @@ -29,6 +30,8 @@ import (
"os/exec"
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
)

Expand All @@ -43,13 +46,17 @@ func main() {

func run(ctx context.Context, stderr *os.File) error {
var (
verbose bool
verbose bool
goVersion string
repoRoot string
)

flag.BoolVar(&verbose, "verbose", false, "increase verbosity")
flag.BoolVar(&verbose, "v", false, "increase verbosity (shorthand)")
flag.StringVar(&goVersion, "go_version", "", "go version for go.mod")
flag.StringVar(&repoRoot, "repo_root", os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "root directory of Gazelle repo")
flag.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), `usage: bazel run //tools/releaser
fmt.Fprint(flag.CommandLine.Output(), `usage: bazel run //tools/releaser -- -go_version <version>

This utility is intended to handle many of the steps to release a new version.

Expand All @@ -59,10 +66,21 @@ This utility is intended to handle many of the steps to release a new version.

flag.Parse()

workspacePath := path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "WORKSPACE")
depsPath := path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "deps.bzl")
versionParts := strings.Split(goVersion, ".")
if len(versionParts) < 2 {
flag.Usage()
return errors.New("please provide a valid Go version")
}
if minorVersion, err := strconv.Atoi(versionParts[1]); err != nil {
return fmt.Errorf("%q is not a valid Go version", goVersion)
} else if minorVersion > 0 {
versionParts[1] = strconv.Itoa(minorVersion - 1)
}

workspacePath := path.Join(repoRoot, "WORKSPACE")
depsPath := path.Join(repoRoot, "deps.bzl")
_tmpBzl := "tmp.bzl"
tmpBzlPath := path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), _tmpBzl)
tmpBzlPath := path.Join(repoRoot, _tmpBzl)

if verbose {
fmt.Println("Running initial go update commands")
Expand All @@ -72,13 +90,13 @@ This utility is intended to handle many of the steps to release a new version.
args []string
}{
{cmd: "go", args: []string{"get", "-t", "-u", "./..."}},
{cmd: "go", args: []string{"mod", "tidy", "-compat=1.16"}},
{cmd: "go", args: []string{"mod", "tidy", "-go", goVersion, "-compat", strings.Join(versionParts, ".")}},
{cmd: "go", args: []string{"mod", "vendor"}},
{cmd: "find", args: []string{"vendor", "-name", "BUILD.bazel", "-delete"}},
}
for _, c := range initialCommands {
cmd := exec.CommandContext(ctx, c.cmd, c.args...)
cmd.Dir = os.Getenv("BUILD_WORKSPACE_DIRECTORY")
cmd.Dir = repoRoot
if out, err := cmd.CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
Expand Down Expand Up @@ -176,14 +194,19 @@ This utility is intended to handle many of the steps to release a new version.
fmt.Println("Running final gazelle run, and copying some language specific build files.")
}
cmd = exec.CommandContext(ctx, "bazel", "run", "//:gazelle")
cmd.Dir = os.Getenv("BUILD_WORKSPACE_DIRECTORY")
cmd.Dir = repoRoot
if out, err := cmd.CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
}

cmd = exec.CommandContext(ctx, "bazel", "build", "//language/proto:known_imports", "//language/proto:known_proto_imports", "//language/proto:known_go_imports")
cmd.Dir = os.Getenv("BUILD_WORKSPACE_DIRECTORY")
cmd = exec.CommandContext(ctx, "bazel", "build",
"//language/go:std_package_list",
"//language/proto:known_go_imports",
"//language/proto:known_imports",
"//language/proto:known_proto_imports",
)
cmd.Dir = repoRoot
if out, err := cmd.CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
Expand All @@ -193,20 +216,25 @@ This utility is intended to handle many of the steps to release a new version.
dest, src string
}{
{
dest: path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "language/proto/known_imports.go"),
src: path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "bazel-bin/language/proto/known_imports.go"),
dest: "language/proto/known_imports.go",
src: "bazel-bin/language/proto/known_imports.go",
},
{
dest: "language/proto/known_proto_imports.go",
src: "bazel-bin/language/proto/known_proto_imports.go",
},
{
dest: path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "language/proto/known_proto_imports.go"),
src: path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "bazel-bin/language/proto/known_proto_imports.go"),
dest: "language/proto/known_go_imports.go",
src: "bazel-bin/language/proto/known_go_imports.go",
},
{
dest: path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "language/proto/known_go_imports.go"),
src: path.Join(os.Getenv("BUILD_WORKSPACE_DIRECTORY"), "bazel-bin/language/proto/known_go_imports.go"),
dest: "language/go/std_package_list.go",
src: "bazel-bin/language/go/std_package_list.go",
},
}
for _, c := range copies {
if err := copyHelper(
if err := copyFile(
repoRoot,
c.dest,
c.src,
); err != nil {
Expand All @@ -220,11 +248,17 @@ This utility is intended to handle many of the steps to release a new version.
return nil
}

func copyHelper(destPath, srcPath string) error {
func copyFile(repoRoot, destPath, srcPath string) error {
if !filepath.IsAbs(destPath) {
destPath = path.Join(repoRoot, destPath)
}
dest, err := os.Create(destPath)
if err != nil {
return err
}
if !filepath.IsAbs(srcPath) {
srcPath = path.Join(repoRoot, srcPath)
}
src, err := os.Open(srcPath)
if err != nil {
return err
Expand Down