Search in sources :

Example 66 with HumanReadableException

use of com.facebook.buck.util.HumanReadableException 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 67 with HumanReadableException

use of com.facebook.buck.util.HumanReadableException in project buck by facebook.

the class BuildCommand method showOutputs.

private void showOutputs(CommandRunnerParams params, ActionGraphAndResolver actionGraphAndResolver) {
    Optional<DefaultRuleKeyFactory> ruleKeyFactory = Optional.empty();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(actionGraphAndResolver.getResolver());
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    if (showRuleKey) {
        RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(params.getBuckConfig().getKeySeed());
        ruleKeyFactory = Optional.of(new DefaultRuleKeyFactory(fieldLoader, params.getFileHashCache(), pathResolver, ruleFinder));
    }
    params.getConsole().getStdOut().println("The outputs are:");
    for (BuildTarget buildTarget : buildTargets) {
        try {
            BuildRule rule = actionGraphAndResolver.getResolver().requireRule(buildTarget);
            Optional<Path> outputPath = TargetsCommand.getUserFacingOutputPath(pathResolver, rule, showFullOutput, params.getBuckConfig().getBuckOutCompatLink());
            params.getConsole().getStdOut().printf("%s%s%s\n", rule.getFullyQualifiedName(), showRuleKey ? " " + ruleKeyFactory.get().build(rule).toString() : "", showOutput || showFullOutput ? " " + outputPath.map(Object::toString).orElse("") : "");
        } catch (NoSuchBuildTargetException e) {
            throw new HumanReadableException(MoreExceptions.getHumanReadableOrLocalizedMessage(e));
        }
    }
}
Also used : RuleKeyFieldLoader(com.facebook.buck.rules.keys.RuleKeyFieldLoader) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) BuildRule(com.facebook.buck.rules.BuildRule) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 68 with HumanReadableException

use of com.facebook.buck.util.HumanReadableException in project buck by facebook.

the class AbstractCommand method getConfigOverrides.

@Override
public CellConfig getConfigOverrides() {
    CellConfig.Builder builder = CellConfig.builder();
    // Parse command-line config overrides.
    for (Map.Entry<String, String> entry : configOverrides.entrySet()) {
        List<String> key = Splitter.on("//").limit(2).splitToList(entry.getKey());
        RelativeCellName cellName = CellConfig.ALL_CELLS_OVERRIDE;
        String configKey = key.get(0);
        if (key.size() == 2) {
            // path overrides for cells.
            if (key.get(0).length() == 0) {
                cellName = RelativeCellName.ROOT_CELL_NAME;
            } else {
                cellName = RelativeCellName.of(ImmutableSet.of(key.get(0)));
            }
            configKey = key.get(1);
        }
        int separatorIndex = configKey.lastIndexOf('.');
        if (separatorIndex < 0 || separatorIndex == configKey.length() - 1) {
            throw new HumanReadableException("Invalid config override \"%s=%s\" Expected <section>.<field>=<value>.", configKey, entry.getValue());
        }
        String value = entry.getValue();
        // If the value is empty, un-set the config
        if (value == null) {
            value = "";
        }
        // Overrides for locations of transitive children of cells are weird as the order of overrides
        // can affect the result (for example `-c a/b/c.k=v -c a/b//repositories.c=foo` causes an
        // interesting problem as the a/b/c cell gets created as a side-effect of the first override,
        // but the second override wants to change its identity).
        // It's generally a better idea to use the .buckconfig.local mechanism when overriding
        // repositories anyway, so here we simply disallow them.
        String section = configKey.substring(0, separatorIndex);
        if (section.equals("repositories")) {
            throw new HumanReadableException("Overriding repository locations from the command line " + "is not supported. Please place a .buckconfig.local in the appropriate location and " + "use that instead.");
        }
        String field = configKey.substring(separatorIndex + 1);
        builder.put(cellName, section, field, value);
    }
    if (numThreads != null) {
        builder.put(CellConfig.ALL_CELLS_OVERRIDE, "build", "threads", String.valueOf(numThreads));
    }
    if (noCache) {
        builder.put(CellConfig.ALL_CELLS_OVERRIDE, "cache", "mode", "");
    }
    return builder.build();
}
Also used : RelativeCellName(com.facebook.buck.rules.RelativeCellName) HumanReadableException(com.facebook.buck.util.HumanReadableException) CellConfig(com.facebook.buck.config.CellConfig) Map(java.util.Map)

Example 69 with HumanReadableException

use of com.facebook.buck.util.HumanReadableException in project buck by facebook.

the class UnixArchive method getIntFromStringAtRange.

private int getIntFromStringAtRange(int len, CharsetDecoder decoder) {
    String filenameLengthString;
    int offset = buffer.position();
    try {
        filenameLengthString = readStringWithLength(buffer, len, decoder);
    } catch (CharacterCodingException e) {
        throw new HumanReadableException(e, "Unable to read int from buffer (range %d..%d)", offset, offset + len);
    }
    return Integer.parseInt(filenameLengthString.trim());
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 70 with HumanReadableException

use of com.facebook.buck.util.HumanReadableException in project buck by facebook.

the class CxxLibraryDescription method createSharedLibraryInterface.

private static <A extends Arg> BuildRule createSharedLibraryInterface(BuildRuleParams baseParams, BuildRuleResolver resolver, CxxPlatform cxxPlatform) throws NoSuchBuildTargetException {
    BuildTarget baseTarget = baseParams.getBuildTarget();
    Optional<SharedLibraryInterfaceFactory> factory = cxxPlatform.getSharedLibraryInterfaceFactory();
    if (!factory.isPresent()) {
        throw new HumanReadableException("%s: C/C++ platform %s does not support shared library interfaces", baseTarget, cxxPlatform.getFlavor());
    }
    CxxLink sharedLibrary = (CxxLink) resolver.requireRule(baseTarget.withAppendedFlavors(cxxPlatform.getFlavor(), Type.SHARED.getFlavor()));
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    return factory.get().createSharedInterfaceLibrary(baseTarget.withAppendedFlavors(Type.SHARED_INTERFACE.getFlavor(), cxxPlatform.getFlavor()), baseParams, resolver, pathResolver, ruleFinder, sharedLibrary.getSourcePathToOutput());
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Aggregations

HumanReadableException (com.facebook.buck.util.HumanReadableException)195 Path (java.nio.file.Path)79 SourcePath (com.facebook.buck.rules.SourcePath)50 BuildTarget (com.facebook.buck.model.BuildTarget)49 Test (org.junit.Test)46 IOException (java.io.IOException)45 ImmutableList (com.google.common.collect.ImmutableList)39 BuildRule (com.facebook.buck.rules.BuildRule)31 ImmutableSet (com.google.common.collect.ImmutableSet)30 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)29 ImmutableMap (com.google.common.collect.ImmutableMap)26 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)22 Optional (java.util.Optional)21 PathSourcePath (com.facebook.buck.rules.PathSourcePath)20 VisibleForTesting (com.google.common.annotations.VisibleForTesting)18 Map (java.util.Map)18 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)17 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)16 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)15 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)15