Search in sources :

Example 1 with ProjectBuildFileParser

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;
}
Also used : Path(java.nio.file.Path) SortedSet(java.util.SortedSet) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) MoreStrings(com.facebook.buck.util.MoreStrings) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Lists(com.google.common.collect.Lists) Argument(org.kohsuke.args4j.Argument) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) Escaper(com.facebook.buck.util.Escaper) BuckPyFunction(com.facebook.buck.rules.BuckPyFunction) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) HumanReadableException(com.facebook.buck.util.HumanReadableException) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) List(java.util.List) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Predicate(com.google.common.base.Predicate) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PrintStream(java.io.PrintStream) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) DefaultTypeCoercerFactory(com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory) BuildFileParseException(com.facebook.buck.json.BuildFileParseException) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) HumanReadableException(com.facebook.buck.util.HumanReadableException) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ProjectBuildFileParser

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));
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Cell(com.facebook.buck.rules.Cell) Test(org.junit.Test)

Example 3 with ProjectBuildFileParser

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;
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ProjectBuildFileParser

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());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with ProjectBuildFileParser

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!")));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ProjectBuildFileParser (com.facebook.buck.json.ProjectBuildFileParser)13 Test (org.junit.Test)9 FakeClock (com.facebook.buck.timing.FakeClock)8 BuckEventBus (com.facebook.buck.event.BuckEventBus)5 ArrayList (java.util.ArrayList)5 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)4 WatchmanDiagnosticEvent (com.facebook.buck.io.WatchmanDiagnosticEvent)2 BuildFileParseException (com.facebook.buck.json.BuildFileParseException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuckPyFunction (com.facebook.buck.rules.BuckPyFunction)1 Cell (com.facebook.buck.rules.Cell)1 ConstructorArgMarshaller (com.facebook.buck.rules.ConstructorArgMarshaller)1 DefaultTypeCoercerFactory (com.facebook.buck.rules.coercer.DefaultTypeCoercerFactory)1 Escaper (com.facebook.buck.util.Escaper)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1