Skip to content

Commit e556b33

Browse files
committed
Make output modes configurable
Added "fix" and "diff" modes
1 parent eef9965 commit e556b33

File tree

6 files changed

+214
-7
lines changed

6 files changed

+214
-7
lines changed

go/tools/gazel/gazel/BUILD

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary", "go_test")
22

3-
go_binary(
4-
name = "gazel",
5-
srcs = ["main.go"],
3+
go_library(
4+
name = "go_default_library",
5+
srcs = [
6+
"diff.go",
7+
"fix.go",
8+
"main.go",
9+
"print.go",
10+
],
611
deps = [
712
"@io_bazel_buildifier//core:go_default_library",
13+
"@io_bazel_buildifier//differ:go_default_library",
814
"//go/tools/gazel/generator:go_default_library",
915
],
1016
)
17+
18+
go_binary(
19+
name = "gazel",
20+
library = ":go_default_library",
21+
)
22+
23+
go_test(
24+
name = "gazel_test",
25+
srcs = ["fix_test.go"],
26+
library = ":go_default_library",
27+
)

go/tools/gazel/gazel/diff.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2016 The Bazel Authors. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package main
17+
18+
import (
19+
"io/ioutil"
20+
"os"
21+
22+
bzl "github.com/bazelbuild/buildifier/core"
23+
"github.com/bazelbuild/buildifier/differ"
24+
)
25+
26+
func diffFile(file *bzl.File) (err error) {
27+
f, err := ioutil.TempFile("", "BUILD")
28+
if err != nil {
29+
return err
30+
}
31+
defer func() {
32+
merr := os.Remove(f.Name())
33+
if err == nil {
34+
err = merr
35+
}
36+
}()
37+
defer f.Close()
38+
if _, err = f.Write(bzl.Format(file)); err != nil {
39+
return err
40+
}
41+
if err := f.Sync(); err != nil {
42+
return err
43+
}
44+
45+
diff := differ.Find()
46+
if _, err := os.Stat(file.Path); os.IsNotExist(err) {
47+
diff.Show(os.DevNull, f.Name())
48+
return nil
49+
}
50+
diff.Show(file.Path, f.Name())
51+
return nil
52+
}

go/tools/gazel/gazel/fix.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright 2016 The Bazel Authors. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package main
17+
18+
import (
19+
"io/ioutil"
20+
21+
bzl "github.com/bazelbuild/buildifier/core"
22+
)
23+
24+
func fixFile(file *bzl.File) error {
25+
// TODO(yugui): Respect exisiting manual configurations as well as possible
26+
return ioutil.WriteFile(file.Path, bzl.Format(file), 0644)
27+
}

go/tools/gazel/gazel/fix_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Copyright 2016 The Bazel Authors. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package main
17+
18+
import (
19+
"io/ioutil"
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
bzl "github.com/bazelbuild/buildifier/core"
25+
)
26+
27+
func TestFixFile(t *testing.T) {
28+
tmpdir := os.Getenv("TEST_TMPDIR")
29+
dir, err := ioutil.TempDir(tmpdir, "")
30+
if err != nil {
31+
t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err)
32+
}
33+
defer os.RemoveAll(dir)
34+
35+
stubFile := &bzl.File{
36+
Path: filepath.Join(dir, "BUILD"),
37+
Stmt: []bzl.Expr{
38+
&bzl.CallExpr{
39+
X: &bzl.LiteralExpr{Token: "foo_rule"},
40+
List: []bzl.Expr{
41+
&bzl.BinaryExpr{
42+
X: &bzl.LiteralExpr{Token: "name"},
43+
Op: "=",
44+
Y: &bzl.StringExpr{Value: "bar"},
45+
},
46+
},
47+
},
48+
},
49+
}
50+
51+
if err := fixFile(stubFile); err != nil {
52+
t.Errorf("fixFile(%#v) failed with %v; want success", stubFile, err)
53+
return
54+
}
55+
56+
buf, err := ioutil.ReadFile(stubFile.Path)
57+
if err != nil {
58+
t.Errorf("ioutil.ReadFile(%q) failed with %v; want success", stubFile.Path, err)
59+
return
60+
}
61+
if got, want := string(buf), bzl.FormatString(stubFile); got != want {
62+
t.Errorf("buf = %q; want %q", got, want)
63+
}
64+
}

go/tools/gazel/gazel/main.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"log"
2424
"os"
25+
"path/filepath"
2526

2627
bzl "github.com/bazelbuild/buildifier/core"
2728
"github.com/bazelbuild/rules_go/go/tools/gazel/generator"
@@ -31,9 +32,16 @@ var (
3132
rulesGoRepo = flag.String("rules_go_repo", generator.DefaultRulesGoRepo, "repository name of rules_go")
3233
goPrefix = flag.String("go_prefix", "", "go_prefix of the target workspace")
3334
repoRoot = flag.String("repo_root", "", "path to a directory which corresponds to go_prefix")
35+
mode = flag.String("mode", "print", "print, fix or diff")
3436
)
3537

36-
func run(dirs []string) error {
38+
var modeFromName = map[string]func(*bzl.File) error{
39+
"print": printFile,
40+
"fix": fixFile,
41+
"diff": diffFile,
42+
}
43+
44+
func run(dirs []string, emit func(*bzl.File) error) error {
3745
g, err := generator.New(*repoRoot, *goPrefix)
3846
if err != nil {
3947
return err
@@ -46,7 +54,8 @@ func run(dirs []string) error {
4654
return err
4755
}
4856
for _, f := range files {
49-
if _, err := os.Stdout.Write(bzl.Format(f)); err != nil {
57+
f.Path = filepath.Join(*repoRoot, f.Path)
58+
if err := emit(f); err != nil {
5059
return err
5160
}
5261
}
@@ -68,6 +77,11 @@ It takes a list of paths to Go package directories.
6877
It recursively traverses its subpackages.
6978
All the directories must be under the directory specified in -repo_root.
7079
80+
There are several modes of gazel.
81+
In print mode, gazel prints reconciled BUILD files to stdout.
82+
In fix mode, gazel creates BUILD files or updates existing ones.
83+
In diff mode, gazel shows diff.
84+
7185
FLAGS:
7286
`)
7387
flag.PrintDefaults()
@@ -89,7 +103,13 @@ func main() {
89103
// TODO(yugui): Guess repoRoot at the same time as goPrefix
90104
*repoRoot = flag.Arg(0)
91105
}
92-
if err := run(flag.Args()); err != nil {
106+
107+
emit := modeFromName[*mode]
108+
if emit == nil {
109+
log.Fatalf("unrecognized mode %s", *mode)
110+
}
111+
112+
if err := run(flag.Args(), emit); err != nil {
93113
log.Fatal(err)
94114
}
95115
}

go/tools/gazel/gazel/print.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright 2016 The Bazel Authors. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package main
17+
18+
import (
19+
"os"
20+
21+
bzl "github.com/bazelbuild/buildifier/core"
22+
)
23+
24+
func printFile(f *bzl.File) (err error) {
25+
_, err = os.Stdout.Write(bzl.Format(f))
26+
return err
27+
}

0 commit comments

Comments
 (0)