Skip to content

Commit

Permalink
Add a script to bump API and release versions. (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
landrito authored Oct 19, 2018
1 parent 278ffdf commit a418744
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 40 deletions.
173 changes: 173 additions & 0 deletions util/cmd/bump_version/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/googleapis/gapic-showcase/util"
"github.com/spf13/cobra"
)

const CURRENT_API = "v1alpha2"
const CURRENT_RELEASE = "0.0.7"

func main() {
var bumpMajor, bumpMinor, bumpPatch bool
var newApi string

// This is definitely overkill, I am just lazy and didn't want to write the
// code to parse the args. ¯\_(ツ)_/¯
cmd := &cobra.Command{
Short: "Utility script to bump the API and realease versions in all relevant files.",
Run: func(cmd *cobra.Command, args []string) {
if newApi == "" && !bumpMajor && !bumpMinor && !bumpPatch {
return
}

if bumpMajor || bumpMinor || bumpPatch {
if !oneof(bumpMajor, bumpMinor, bumpPatch) {
log.Fatalf("Expected only one of --major, --minor, and --patch.")
}
versions := strings.Split(CURRENT_RELEASE, ".")

atoi := func(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
log.Fatal(err)
}
return i
}

major := atoi(versions[0])
minor := atoi(versions[1])
patch := atoi(versions[2])

if bumpMajor {
major += 1
minor = 0
patch = 0
}
if bumpMinor {
minor += 1
patch = 0
}
if bumpPatch {
patch += 1
}
replace(CURRENT_RELEASE, fmt.Sprintf("%d.%d.%d", major, minor, patch))
}

if newApi != "" && CURRENT_API != newApi {
replace(CURRENT_API, newApi)
util.CompileProtos(newApi)
}
},
}

cmd.Flags().BoolVarP(
&bumpMajor,
"major",
"",
false,
"Pass this flag to bump the major version")
cmd.Flags().BoolVarP(
&bumpMinor,
"minor",
"",
false,
"Pass this flag to bump the minor version")
cmd.Flags().BoolVarP(
&bumpPatch,
"patch",
"",
false,
"Pass this flag to bump the patch version")
cmd.Flags().StringVarP(
&newApi,
"api",
"a",
"",
"The new API version to set.")

if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func replace(old, new string) {
showcaseDir := filepath.Join(
os.Getenv("GOPATH"),
"src",
"github.com",
"googleapis",
"gapic-showcase")

filetypes := []string{".go", ".md", ".yml"}
err := filepath.Walk(showcaseDir, replacer(filetypes, old, new))
if err != nil {
log.Fatalf("%v", err)
}
}

func replacer(filetypes []string, old, new string) filepath.WalkFunc {
return func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}

matched := false
for _, t := range filetypes {
matched = matched || strings.HasSuffix(path, t)
}
if !matched {
return nil
}

oldBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("%v", err)
}

newBytes := bytes.Replace(oldBytes, []byte(old), []byte(new), -1)
if !bytes.Equal(oldBytes, newBytes) {
if err = ioutil.WriteFile(path, newBytes, 0); err != nil {
log.Fatalf("%v", err)
}
}
return nil
}
}

func oneof(bs ...bool) bool {
t := 0
for _, b := range bs {
if b {
t += 1
}
}
return t == 1
}
23 changes: 23 additions & 0 deletions util/cmd/compile_protos/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/googleapis/gapic-showcase/util"
)

func main() {
util.CompileProtos("v1alpha2")
}
38 changes: 13 additions & 25 deletions util/release/main.go → util/cmd/release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"strings"
"sync"

"github.com/googleapis/gapic-showcase/util"
)

func main() {
Expand All @@ -31,6 +33,8 @@ func main() {
teardown()
}

// TODO(landrito): theres a lot of similar things happening here in and in the
// compile protos command. Consolodate the repeated code into a base util file.
func setup() {
distDir := filepath.Join(showcaseDir(), "dist")
if err := os.RemoveAll(distDir); err != nil {
Expand Down Expand Up @@ -58,7 +62,7 @@ func teardown() {

func stageProtos() {
// Get proto dependencies
execute(
util.Execute(
"git",
"clone",
"-b",
Expand All @@ -85,7 +89,7 @@ func stageProtos() {
}

for _, f := range files {
execute("cp", f, protoDest)
util.Execute("cp", f, protoDest)
}
}

Expand Down Expand Up @@ -116,14 +120,14 @@ func compileDescriptors() {
"-o",
filepath.Join(showcaseDir(), "dist", fmt.Sprintf("gapic-showcase-%s.desc", version())),
}
execute(append(command, files...)...)
util.Execute(append(command, files...)...)
}

func tarProtos() {
protoDir := filepath.Join(showcaseDir(), "tmp", "api-common-protos")
output := filepath.Join(showcaseDir(), "dist", fmt.Sprintf("gapic-showcase-%s-protos.tar.gz", version()))

execute("tar", "-zcf", output, "-C", protoDir, "google")
util.Execute("tar", "-zcf", output, "-C", protoDir, "google")
}

func compileBinaries() {
Expand All @@ -132,7 +136,7 @@ func compileBinaries() {

// Mousetrap is a windows dependency that is not implicitly got since
// we only get the linux dependencies.
execute("go", "get", "github.com/mitchellh/gox", "github.com/inconshreveable/mousetrap")
util.Execute("go", "get", "github.com/mitchellh/gox", "github.com/inconshreveable/mousetrap")

osArchs := []string{
"windows/amd64",
Expand All @@ -141,7 +145,7 @@ func compileBinaries() {
"linux/arm",
}
for _, osArch := range osArchs {
execute(
util.Execute(
"gox",
fmt.Sprintf("-osarch=%s", osArch),
"-output",
Expand All @@ -154,7 +158,7 @@ func compileBinaries() {
// The windows binaries are suffixed with '.exe'. This allows us to create
// tarballs of the executables whether or not they contain a suffix.
files, _ := filepath.Glob(filepath.Join(dir, "gapic-showcase*"))
execute(
util.Execute(
"tar",
"-zcf",
filepath.Join(outputDir, filepath.Base(dir)+".tar.gz"),
Expand All @@ -164,22 +168,6 @@ func compileBinaries() {
}
}

func execute(args ...string) {
log.Print("Executing: ", strings.Join(args, " "))
if output, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil {
log.Fatalf("%s", output)
}
}

func executeInDir(dir string, args ...string) {
log.Printf("Executing in %s: %s", dir, strings.Join(args, " "))
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = dir
if output, err := cmd.CombinedOutput(); err != nil {
log.Fatalf("%s", output)
}
}

var sOnce sync.Once
var showcaseDirMemo string

Expand All @@ -202,8 +190,8 @@ var versionMemo string

func version() string {
verOnce.Do(func() {
executeInDir(showcaseDir(), "go", "get")
executeInDir(showcaseDir(), "go", "install")
util.ExecuteInDir(showcaseDir(), "go", "get")
util.ExecuteInDir(showcaseDir(), "go", "install")
version, err := exec.Command("gapic-showcase", "--version").Output()
if err != nil {
log.Fatal("Failed getting showcase version")
Expand Down
21 changes: 6 additions & 15 deletions util/compile_protos/main.go → util/compile_protos.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package util

import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

func main() {
func CompileProtos(version string) {
// Check if protoc is installed.
if err := exec.Command("protoc", "--version").Run(); err != nil {
log.Fatal("Error: 'protoc' is expected to be installed on the path.")
Expand All @@ -41,7 +40,7 @@ func main() {
log.Fatalf("Error, could not remove path %s: %v", tmpDir, err)
}

execute(
Execute(
"git",
"clone",
"-b",
Expand All @@ -57,7 +56,7 @@ func main() {
"api-common-protos",
"google",
"showcase",
"v1alpha2")
version)
if err := os.MkdirAll(protoDest, 0755); err != nil {
log.Fatalf("Error, could not make path %s: %v", protoDest, err)
}
Expand All @@ -68,7 +67,7 @@ func main() {
}

for _, f := range files {
execute("cp", f, protoDest)
Execute("cp", f, protoDest)
}

// Compile protos
Expand All @@ -83,13 +82,5 @@ func main() {
"--go_out=plugins=grpc:" + gopath + "/src",
"--proto_path=" + filepath.Join(showcaseDir, "tmp", "api-common-protos"),
}
execute(append(command, files...)...)
os.Exit(0)
}

func execute(args ...string) {
log.Print("Executing: ", strings.Join(args, " "))
if output, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil {
log.Fatalf("%s", output)
}
Execute(append(command, files...)...)
}
Loading

0 comments on commit a418744

Please sign in to comment.