forked from bazelbuild/bazel-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (138 loc) · 3.76 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// 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
//
// http://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 (
"flag"
"fmt"
"os"
"strings"
"time"
)
var Version = "Development"
var overrideableBazelFlags []string = []string{
"--test_output=",
"--config=",
"--curses=no",
"--output_groups=",
"--keep_going",
"-k",
"--strategy",
"--define",
"--override_repository",
"--features=",
"--test_arg",
"--test_filter",
}
var debounceDuration = flag.Duration("debounce", 100*time.Millisecond, "Debounce duration")
var logToFile = flag.String("log_to_file", "-", "Log iBazel stderr to a file instead of os.Stderr")
func usage() {
fmt.Fprintf(os.Stderr, `iBazel - Version %s
A file watcher for Bazel. Whenever a source file used in a specified
target, run, build, or test the specified targets.
Usage:
ibazel [flags] build|test|run targets...
Example:
ibazel test //path/to/my/testing:target
ibazel test //path/to/my/testing/targets/...
ibazel run //path/to/my/runnable:target -- --arguments --for_your=binary
ibazel build //path/to/my/buildable:target
Supported Bazel flags:
%s
To add to this list, edit
https://github.com/bazelbuild/bazel-watcher/blob/master/ibazel/main.go
iBazel flags:
`, Version, strings.Join(overrideableBazelFlags, "\n "))
flag.PrintDefaults()
}
func isOverrideableBazelFlag(arg string) bool {
for _, overrideable := range overrideableBazelFlags {
if strings.HasPrefix(arg, overrideable) {
return true
}
}
return false
}
func parseArgs(in []string) (targets, bazelArgs, args []string) {
afterDoubleDash := false
for _, arg := range in {
if afterDoubleDash {
// Put it in the extra args section if we are after a double dash.
args = append(args, arg)
} else {
// Check to see if this token is a double dash.
if arg == "--" {
afterDoubleDash = true
continue
}
// Check to see if this flag is on the bazel whitelist of flags.
if isOverrideableBazelFlag(arg) {
bazelArgs = append(bazelArgs, arg)
continue
}
// If none of those things then it's probably a target.
targets = append(targets, arg)
}
}
return
}
// main entrypoint for IBazel.
func main() {
flag.Usage = usage
flag.Parse()
if *logToFile != "-" {
var err error
logFile, err := os.OpenFile(*logToFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
os.Stderr = logFile
}
if len(flag.Args()) < 2 {
usage()
return
}
command := strings.ToLower(flag.Args()[0])
args := flag.Args()[1:]
i, err := New()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating iBazel: %s\n", err)
os.Exit(1)
}
i.SetDebounceDuration(*debounceDuration)
defer i.Cleanup()
// increase the number of files that this process can
// have open.
err = setUlimit()
if err != nil {
fmt.Fprint(os.Stderr, "error setting higher file descriptor limit for this process: ", err)
}
handle(i, command, args)
}
func handle(i *IBazel, command string, args []string) {
targets, bazelArgs, args := parseArgs(args)
i.SetBazelArgs(bazelArgs)
switch command {
case "build":
i.Build(targets...)
case "test":
i.Test(targets...)
case "run":
// Run only takes one argument
i.Run(targets[0], args)
default:
fmt.Fprintf(os.Stderr, "Asked me to perform %s. I don't know how to do that.", command)
usage()
return
}
}