use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.
the class AuditRulesCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
ProjectFilesystem projectFilesystem = params.getCell().getFilesystem();
try (ProjectBuildFileParser parser = params.getCell().createBuildFileParser(new ConstructorArgMarshaller(new DefaultTypeCoercerFactory(params.getObjectMapper())), params.getConsole(), params.getBuckEventBus(), /* ignoreBuckAutodepsFiles */
false)) {
PrintStream out = params.getConsole().getStdOut();
for (String pathToBuildFile : getArguments()) {
if (!json) {
// Print a comment with the path to the build file.
out.printf("# %s\n\n", pathToBuildFile);
}
// Resolve the path specified by the user.
Path path = Paths.get(pathToBuildFile);
if (!path.isAbsolute()) {
Path root = projectFilesystem.getRootPath();
path = root.resolve(path);
}
// Parse the rules from the build file.
List<Map<String, Object>> rawRules;
try {
rawRules = parser.getAll(path);
} catch (BuildFileParseException e) {
throw new HumanReadableException(e);
}
// Format and print the rules from the raw data, filtered by type.
final ImmutableSet<String> types = getTypes();
Predicate<String> includeType = type -> types.isEmpty() || types.contains(type);
printRulesToStdout(params, rawRules, includeType);
}
} catch (BuildFileParseException e) {
throw new HumanReadableException("Unable to create parser");
}
return 0;
}
use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.
the class ProjectBuildFileParserPoolTest method closesCreatedParsers.
@Test
public void closesCreatedParsers() throws Exception {
final int parsersCount = 4;
final AtomicInteger parserCount = new AtomicInteger(0);
Cell cell = EasyMock.createMock(Cell.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(parsersCount));
final CountDownLatch createParserLatch = new CountDownLatch(parsersCount);
try (ProjectBuildFileParserPool parserPool = new ProjectBuildFileParserPool(parsersCount, input -> {
parserCount.incrementAndGet();
final ProjectBuildFileParser parser = EasyMock.createMock(ProjectBuildFileParser.class);
try {
EasyMock.expect(parser.getAllRulesAndMetaRules(EasyMock.anyObject(Path.class))).andAnswer(() -> {
createParserLatch.countDown();
createParserLatch.await();
return ImmutableList.of();
}).anyTimes();
parser.close();
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
@Override
public Void answer() throws Throwable {
parserCount.decrementAndGet();
return null;
}
});
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
EasyMock.replay(parser);
return parser;
})) {
Futures.allAsList(scheduleWork(cell, parserPool, executorService, parsersCount * 2)).get();
assertThat(parserCount.get(), Matchers.is(4));
} finally {
executorService.shutdown();
}
// Parser shutdown is async.
for (int i = 0; i < 10; ++i) {
if (parserCount.get() == 0) {
break;
}
Thread.sleep(100);
}
assertThat(parserCount.get(), Matchers.is(0));
}
use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.
the class ProjectBuildFileParserPoolTest method createMockParser.
private ProjectBuildFileParser createMockParser(IAnswer<ImmutableList<Map<String, Object>>> parseFn) {
ProjectBuildFileParser mock = EasyMock.createMock(ProjectBuildFileParser.class);
try {
EasyMock.expect(mock.getAllRulesAndMetaRules(EasyMock.anyObject(Path.class))).andAnswer(parseFn).anyTimes();
mock.close();
EasyMock.expectLastCall().andVoid().once();
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
EasyMock.replay(mock);
return mock;
}
use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.
the class ProjectBuildFileParserTest method whenSubprocessReturnsWarningThenConsoleEventPublished.
@Test
public void whenSubprocessReturnsWarningThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
// This test depends on unix utilities that don't exist on Windows.
assumeTrue(Platform.detect() != Platform.WINDOWS);
TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
final List<ConsoleEvent> consoleEvents = new ArrayList<>();
final List<WatchmanDiagnosticEvent> watchmanDiagnosticEvents = new ArrayList<>();
class EventListener {
@Subscribe
public void on(ConsoleEvent consoleEvent) {
consoleEvents.add(consoleEvent);
}
@Subscribe
public void on(WatchmanDiagnosticEvent event) {
watchmanDiagnosticEvents.add(event);
}
}
EventListener eventListener = new EventListener();
buckEventBus.register(eventListener);
try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessWithWarning(buckEventBus, "This is a warning", "parser")) {
buildFileParser.initIfNeeded();
buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
}
assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Warning raised by BUCK file parser: This is a warning")));
assertThat("Should not receive any watchman diagnostic events", watchmanDiagnosticEvents, Matchers.empty());
}
use of com.facebook.buck.json.ProjectBuildFileParser in project buck by facebook.
the class ProjectBuildFileParserTest method whenSubprocessPrintsWarningToStderrThenConsoleEventPublished.
@Test
public void whenSubprocessPrintsWarningToStderrThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
// This test depends on unix utilities that don't exist on Windows.
assumeTrue(Platform.detect() != Platform.WINDOWS);
TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
final List<ConsoleEvent> consoleEvents = new ArrayList<>();
class EventListener {
@Subscribe
public void onConsoleEvent(ConsoleEvent consoleEvent) {
consoleEvents.add(consoleEvent);
}
}
EventListener eventListener = new EventListener();
buckEventBus.register(eventListener);
try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessAndPrintsToStderr(buckEventBus)) {
buildFileParser.initIfNeeded();
buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
}
assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Warning raised by BUCK file parser: Don't Panic!")));
}
Aggregations