Example #1
0
  @Test
  public void testBasicProjectCommand() throws Exception {
    BuildRuleResolver ruleResolver = new BuildRuleResolver();

    BuildTarget javaLibraryTargetName = BuildTargetFactory.newInstance("//javasrc:java-library");
    DefaultJavaLibraryRule javaLibraryRule =
        ruleResolver.buildAndAddToIndex(
            DefaultJavaLibraryRule.newJavaLibraryRuleBuilder(
                    new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(javaLibraryTargetName)
                .addSrc("javasrc/JavaLibrary.java"));

    String projectConfigTargetName = "//javasrc:project-config";
    ProjectConfigRule ruleConfig =
        ruleResolver.buildAndAddToIndex(
            ProjectConfigRule.newProjectConfigRuleBuilder(new FakeAbstractBuildRuleBuilderParams())
                .setBuildTarget(BuildTargetFactory.newInstance(projectConfigTargetName))
                .setSrcTarget(Optional.of(javaLibraryTargetName)));

    BuckConfig buckConfig =
        createBuckConfig(
            Joiner.on("\n").join("[project]", "initial_targets = " + javaLibraryTargetName));

    ProjectCommandForTest command = new ProjectCommandForTest();
    command.createPartialGraphCallReturnValues.push(
        createGraphFromBuildRules(ImmutableList.<BuildRule>of(ruleConfig)));

    command.runCommandWithOptions(createOptions(buckConfig));

    assertTrue(command.createPartialGraphCallReturnValues.isEmpty());

    // The PartialGraph comprises build config rules.
    RawRulePredicate projectConfigPredicate = command.createPartialGraphCallPredicates.get(0);
    checkPredicate(projectConfigPredicate, EMPTY_PARSE_DATA, javaLibraryRule, false);
    checkPredicate(projectConfigPredicate, EMPTY_PARSE_DATA, ruleConfig, true);

    BuildCommandOptions buildOptions = command.buildCommandOptions;
    MoreAsserts.assertContainsOne(
        buildOptions.getArguments(), javaLibraryTargetName.getFullyQualifiedName());
  }
Example #2
0
  @Override
  @SuppressWarnings("PMD.PrematureDeclaration")
  int runCommandWithOptionsInternal(BuildCommandOptions options) throws IOException {
    // Set the logger level based on the verbosity option.
    Verbosity verbosity = console.getVerbosity();
    Logging.setLoggingLevelForVerbosity(verbosity);

    // Create artifact cache to initialize Cassandra connection, if appropriate.
    ArtifactCache artifactCache = getArtifactCache();

    try {
      buildTargets = getBuildTargets(options.getArgumentsFormattedAsBuildTargets());
    } catch (NoSuchBuildTargetException e) {
      console.printBuildFailureWithoutStacktrace(e);
      return 1;
    }

    if (buildTargets.isEmpty()) {
      console.printBuildFailure("Must specify at least one build target.");

      // If there are aliases defined in .buckconfig, suggest that the user
      // build one of them. We show the user only the first 10 aliases.
      ImmutableSet<String> aliases = options.getBuckConfig().getAliases();
      if (!aliases.isEmpty()) {
        console
            .getStdErr()
            .println(
                String.format(
                    "Try building one of the following targets:\n%s",
                    Joiner.on(' ').join(Iterators.limit(aliases.iterator(), 10))));
      }
      return 1;
    }

    getBuckEventBus().post(BuildEvent.started(buildTargets));

    // Parse the build files to create a DependencyGraph.
    DependencyGraph dependencyGraph;
    try {
      dependencyGraph =
          getParser()
              .parseBuildFilesForTargets(
                  buildTargets, options.getDefaultIncludes(), getBuckEventBus());
    } catch (BuildTargetException | BuildFileParseException e) {
      console.printBuildFailureWithoutStacktrace(e);
      return 1;
    }

    // Create and execute the build.
    build =
        options.createBuild(
            options.getBuckConfig(),
            dependencyGraph,
            getProjectFilesystem(),
            getAndroidDirectoryResolver(),
            artifactCache,
            console,
            getBuckEventBus(),
            Optional.<TargetDevice>absent(),
            getCommandRunnerParams().getPlatform());
    int exitCode = 0;
    try {
      exitCode = executeBuildAndPrintAnyFailuresToConsole(build, console);
    } finally {
      // Shutdown the Executor Service once the build completes.
      // Note: we need to use shutdown() instead of shutdownNow() to ensure that tasks submitted to
      // the Execution Service are completed.
      build.getStepRunner().getListeningExecutorService().shutdown();
    }

    getBuckEventBus().post(BuildEvent.finished(buildTargets, exitCode));

    if (exitCode != 0) {
      return exitCode;
    }

    return 0;
  }