-
Notifications
You must be signed in to change notification settings - Fork 4
/
BazelSubscriberApp.java
102 lines (85 loc) · 4.46 KB
/
BazelSubscriberApp.java
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
package com.salesforce.bazel.app.subscriber;
import java.io.File;
import com.salesforce.bazel.sdk.bep.BazelBuildEventSubscriber;
import com.salesforce.bazel.sdk.bep.BazelBuildEventsPollingFileStream;
import com.salesforce.bazel.sdk.bep.event.BEPProgressEvent;
import com.salesforce.bazel.sdk.path.FSPathHelper;
/**
* This app uses the Bazel Java SDK to monitor builds in a Bazel workspace on your machine. It hooks into build activity
* using the Bazel Build Events Protocol (BEP), which allows this app to see various build events like build start,
* build finish, build errors, test errors, etc. This could be used to power customized notifications to the user, or a
* team of users.
* <p>
* Because this app integrates with Bazel using BEP, this app will work with command line builds but also with IDE
* builds.
* <p>
* <b>NOTE:</b> This app requires a configuration change to your Bazel workspace.
* To be clear, this change is made to the workspace you want to subscribe to, not the
* bazel-java-sdk workspace.
* You must add the following lines to your .bazelrc file to enable BEP:<br/>
* build --build_event_json_file bep_build.json<br/>
* test --build_event_json_file bep_test.json<br/>
* <p>
* The listener for the events simply prints out each event, but this can be easily customized for other use cases. See
* ExampleBazelEventSubscriber.java
* <p>
* Build this app:
* <p>
* bazel build //examples:BazelSubscriberApp_deploy.jar
* <p>
* Usage:
* <ul>
* <li>Update your Bazel workspace .bazelrc to generate the json files, as described above.</li>
* <li>Build this app: bazel build //examples:BazelSubscriberApp_deploy.jar</li>
* <li>Usage: java -jar bazel-bin/examples/BazelSubscriberApp_deploy.jar [path to BEP file that is written to by your build]</li>
* <li>Example: java -jar bazel-bin/examples/BazelSubscriberApp_deploy.jar /home/mbenioff/dev/my-bazel-ws/bep_build.json</li>
* <li>Run your build (e.g. bazel build //...) in the workspace that is writing to the BEP file</li>
* <li>Console will write each event published by the build</li>
* </ul>
*/
public class BazelSubscriberApp {
private static File bepFile;
public static void main(String[] args) {
setup(args);
// BazelBuildEventsPollingFileStream will monitor the files that you add for updates, and reparse them
// when they change. If you just want a single pass of an existing file, the BazelBuildEventsFileStream
// is what you want.
// parseOnStart tells the streamer to parse the contents of the file(s) at startup, which may not be what you
// want because the user may have last run their build a week ago so the events are stale, but for this
// demo it makes sense
boolean parseOnStart = true;
BazelBuildEventsPollingFileStream bepStream = new BazelBuildEventsPollingFileStream();
// add one or both of the BEP files to be monitored by the poller
//bepStream.addFileToMonitor(buildBEPFile, parseOnStart);
bepStream.addFileToMonitor(bepFile, parseOnStart);
// implement your subscriber how you like it
BazelBuildEventSubscriber exampleSubscriber = new ExampleBazelEventSubscriber();
// subscribe to all events with this form:
bepStream.subscribe(exampleSubscriber);
// or filter the events to receive only the ones you want:
//Set<String> filterEventTypes = new HashSet<>();
//filterEventTypes.add(BEPProgressEvent.NAME);
//boolean matchLastMessage = true;
//bepStream.subscribe(exampleSubscriber, filterEventTypes, matchLastMessage);
// start polling for changes to the BEP files; any new event that is added to the file
// will be sent to the subscriber(s) you added to the stream above
bepStream.activateStream();
}
// HELPERS
private static void setup(String[] args) {
// parse args
if (args.length < 1) {
throw new IllegalArgumentException(
"Usage: java -jar BazelBuildApp_deploy.jar [BEP file path]");
}
String bepFilename = args[0];
bepFile = new File(bepFilename);
bepFile = FSPathHelper.getCanonicalFileSafely(bepFile);
if (!bepFile.exists()) {
throw new IllegalArgumentException(
"Bazel workspace directory does not exist. Usage: java -jar BazelBuildApp_deploy.jar [BEP file path]");
}
// setup options
BEPProgressEvent.includeStdOutErrInToString(false);
}
}