Search in sources :

Example 16 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class MavenCoordinatesMacroExpanderTest method testMissingBuildRule.

@Test
public void testMissingBuildRule() throws NoSuchBuildTargetException {
    BuildTarget target = BuildTargetFactory.newInstance("//:java");
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    MacroHandler macroHandler = new MacroHandler(ImmutableMap.of("maven_coords", expander));
    try {
        macroHandler.expand(target, createCellRoots(filesystem), resolver, "$(maven_coords //:foo)");
        fail("Expected MacroException; Rule does not exist");
    } catch (MacroException e) {
        assertTrue("Expected MacroException that indicates target does not exist", e.getMessage().contains("no rule //:foo"));
    }
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) MacroException(com.facebook.buck.model.MacroException) Test(org.junit.Test)

Example 17 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class MavenCoordinatesMacroExpanderTest method testHasMavenCoordinatesBuildRuleMissingCoordinates.

@Test
public void testHasMavenCoordinatesBuildRuleMissingCoordinates() throws Exception {
    BuildRule rule = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//test:no-mvn")).build(resolver);
    try {
        expander.getMavenCoordinates(rule);
        fail("Expected MacroException; Rule does not contain maven coordinates");
    } catch (MacroException e) {
        assertTrue("Expected MacroException that indicates target does not have maven coordinates", e.getMessage().contains("does not have maven coordinates"));
    }
}
Also used : FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) MacroException(com.facebook.buck.model.MacroException) Test(org.junit.Test)

Example 18 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class PrebuiltCxxLibraryDescription method getBuildRules.

private ImmutableList<BuildRule> getBuildRules(BuildTarget target, CellPathResolver cellNames, BuildRuleResolver ruleResolver, Iterable<String> paramValues) {
    ImmutableList.Builder<BuildRule> builder = ImmutableList.builder();
    MacroHandler macroHandler = getMacroHandler(Optional.empty());
    for (String p : paramValues) {
        try {
            builder.addAll(macroHandler.extractBuildTimeDeps(target, cellNames, ruleResolver, p));
        } catch (MacroException e) {
            throw new HumanReadableException(e, "%s : %s in \"%s\"", target, e.getMessage(), p);
        }
    }
    return builder.build();
}
Also used : MacroHandler(com.facebook.buck.rules.macros.MacroHandler) ImmutableList(com.google.common.collect.ImmutableList) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildRule(com.facebook.buck.rules.BuildRule) MacroException(com.facebook.buck.model.MacroException)

Example 19 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class WorkerToolDescription method createBuildRule.

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, final BuildRuleParams params, final BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    BuildRule rule = resolver.requireRule(args.exe);
    if (!(rule instanceof BinaryBuildRule)) {
        throw new HumanReadableException("The 'exe' argument of %s, %s, needs to correspond to a " + "binary rule, such as sh_binary().", params.getBuildTarget(), args.exe.getFullyQualifiedName());
    }
    Function<String, com.facebook.buck.rules.args.Arg> toArg = MacroArg.toMacroArgFunction(MACRO_HANDLER, params.getBuildTarget(), params.getCellRoots(), resolver);
    final ImmutableList<com.facebook.buck.rules.args.Arg> workerToolArgs = args.getStartupArgs().stream().map(toArg::apply).collect(MoreCollectors.toImmutableList());
    ImmutableMap<String, String> expandedEnv = ImmutableMap.copyOf(FluentIterable.from(args.env.entrySet()).transform(input -> {
        try {
            return Maps.immutableEntry(input.getKey(), MACRO_HANDLER.expand(params.getBuildTarget(), params.getCellRoots(), resolver, input.getValue()));
        } catch (MacroException e) {
            throw new HumanReadableException(e, "%s: %s", params.getBuildTarget(), e.getMessage());
        }
    }));
    int maxWorkers;
    if (args.maxWorkers.isPresent()) {
        // negative or zero: unlimited number of worker processes
        maxWorkers = args.maxWorkers.get() < 1 ? Integer.MAX_VALUE : args.maxWorkers.get();
    } else {
        // default is 1 worker process (for backwards compatibility)
        maxWorkers = 1;
    }
    return new DefaultWorkerTool(params, (BinaryBuildRule) rule, workerToolArgs, expandedEnv, maxWorkers, args.persistent.orElse(buckConfig.getBooleanValue(CONFIG_SECTION, CONFIG_PERSISTENT_KEY, false)));
}
Also used : BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) CellPathResolver(com.facebook.buck.rules.CellPathResolver) Either(com.facebook.buck.model.Either) ClasspathMacroExpander(com.facebook.buck.rules.macros.ClasspathMacroExpander) BuildRule(com.facebook.buck.rules.BuildRule) BuckConfig(com.facebook.buck.cli.BuckConfig) ImmutableList(com.google.common.collect.ImmutableList) FluentIterable(com.google.common.collect.FluentIterable) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ImplicitDepsInferringDescription(com.facebook.buck.rules.ImplicitDepsInferringDescription) Map(java.util.Map) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MacroArg(com.facebook.buck.rules.args.MacroArg) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) MacroException(com.facebook.buck.model.MacroException) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Maps(com.google.common.collect.Maps) ExecutableMacroExpander(com.facebook.buck.rules.macros.ExecutableMacroExpander) MacroExpander(com.facebook.buck.rules.macros.MacroExpander) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) Optional(java.util.Optional) LocationMacroExpander(com.facebook.buck.rules.macros.LocationMacroExpander) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) MacroHandler(com.facebook.buck.rules.macros.MacroHandler) Description(com.facebook.buck.rules.Description) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) HumanReadableException(com.facebook.buck.util.HumanReadableException) MacroArg(com.facebook.buck.rules.args.MacroArg) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) MacroException(com.facebook.buck.model.MacroException)

Example 20 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class OutputToFileExpander method expand.

@Override
public String expand(BuildTarget target, CellPathResolver cellNames, BuildRuleResolver resolver, ImmutableList<String> input) throws MacroException {
    try {
        String expanded;
        if (delegate instanceof MacroExpanderWithCustomFileOutput) {
            expanded = ((MacroExpanderWithCustomFileOutput) delegate).expandForFile(target, cellNames, resolver, input);
        } else {
            expanded = delegate.expand(target, cellNames, resolver, input);
        }
        Optional<BuildRule> rule = resolver.getRuleOptional(target);
        if (!rule.isPresent()) {
            throw new MacroException(String.format("no rule %s", target));
        }
        ProjectFilesystem filesystem = rule.get().getProjectFilesystem();
        Path tempFile = createTempFile(filesystem, target, input.get(0));
        filesystem.writeContentsToPath(expanded, tempFile);
        return "@" + filesystem.resolve(tempFile);
    } catch (IOException e) {
        throw new MacroException("Unable to create file to hold expanded results", e);
    }
}
Also used : Path(java.nio.file.Path) BuildRule(com.facebook.buck.rules.BuildRule) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) IOException(java.io.IOException) MacroException(com.facebook.buck.model.MacroException)

Aggregations

MacroException (com.facebook.buck.model.MacroException)20 BuildRule (com.facebook.buck.rules.BuildRule)10 Test (org.junit.Test)9 MacroHandler (com.facebook.buck.rules.macros.MacroHandler)8 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)7 HumanReadableException (com.facebook.buck.util.HumanReadableException)7 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)6 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)6 BuildTarget (com.facebook.buck.model.BuildTarget)5 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)4 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)4 ImmutableList (com.google.common.collect.ImmutableList)4 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)3 WorkerMacroExpander (com.facebook.buck.rules.macros.WorkerMacroExpander)3 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)2 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)2 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)2 ExecutableMacroExpander (com.facebook.buck.rules.macros.ExecutableMacroExpander)2