use of com.facebook.buck.query.QueryException in project buck by facebook.
the class QueryMacroExpander method extractTargets.
private Stream<BuildTarget> extractTargets(BuildTarget target, CellPathResolver cellNames, Optional<BuildRuleResolver> resolver, T input) {
String queryExpression = CharMatcher.anyOf("\"'").trimFrom(input.getQuery().getQuery());
final GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(resolver, targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
try {
QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
HashSet<String> targetLiterals = new HashSet<>();
parsedExp.collectTargetPatterns(targetLiterals);
return targetLiterals.stream().flatMap(pattern -> {
try {
return env.getTargetsMatchingPattern(pattern, executorService).stream();
} catch (Exception e) {
throw new HumanReadableException(e, "Error parsing target expression %s for target %s", pattern, target);
}
}).map(queryTarget -> {
Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
return ((QueryBuildTarget) queryTarget).getBuildTarget();
});
} catch (QueryException e) {
throw new HumanReadableException("Error executing query in macro for target %s", target, e);
}
}
use of com.facebook.buck.query.QueryException in project buck by facebook.
the class QueryMacroExpander method resolveQuery.
Stream<QueryTarget> resolveQuery(BuildTarget target, CellPathResolver cellNames, final BuildRuleResolver resolver, String queryExpression) throws MacroException {
GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(Optional.of(resolver), targetGraph, cellNames, BuildTargetPatternParser.forBaseName(target.getBaseName()), ImmutableSet.of());
try {
QueryExpression parsedExp = QueryExpression.parse(queryExpression, env);
Set<QueryTarget> queryTargets = parsedExp.eval(env, executorService);
return queryTargets.stream();
} catch (QueryException e) {
throw new MacroException("Error parsing/executing query from macro", e);
} catch (InterruptedException e) {
throw new MacroException("Error executing query", e);
}
}
use of com.facebook.buck.query.QueryException in project buck by facebook.
the class QueryTargetTranslator method translateTargets.
@Override
public Optional<Query> translateTargets(CellPathResolver cellPathResolver, BuildTargetPatternParser<BuildTargetPattern> pattern, TargetNodeTranslator translator, Query query) {
// Extract all build targets from the original query string.
ImmutableList<BuildTarget> targets;
try {
targets = QueryUtils.extractBuildTargets(cellPathResolver, pattern, query).collect(MoreCollectors.toImmutableList());
} catch (QueryException e) {
throw new RuntimeException("Error parsing/executing query from deps", e);
}
// If there's no targets, bail early.
if (targets.isEmpty()) {
return Optional.empty();
}
// A pattern matching all of the build targets in the query string.
Pattern targetsPattern = Pattern.compile(targets.stream().map(Object::toString).map(Pattern::quote).collect(Collectors.joining("|")));
// Build a new query string from the original by translating all build targets.
String queryString = query.getQuery();
Matcher matcher = targetsPattern.matcher(queryString);
StringBuilder builder = new StringBuilder();
int lastEnd = 0;
while (matcher.find()) {
builder.append(queryString.substring(lastEnd, matcher.start()));
BuildTarget target = BuildTargetParser.INSTANCE.parse(matcher.group(), pattern, cellPathResolver);
Optional<BuildTarget> translated = translator.translate(cellPathResolver, pattern, target);
builder.append(translated.orElse(target).getFullyQualifiedName());
lastEnd = matcher.end();
}
builder.append(queryString.substring(lastEnd, queryString.length()));
String newQuery = builder.toString();
return queryString.equals(newQuery) ? Optional.empty() : Optional.of(Query.of(newQuery));
}
use of com.facebook.buck.query.QueryException in project buck by facebook.
the class QueryCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
if (arguments.isEmpty()) {
params.getBuckEventBus().post(ConsoleEvent.severe("Must specify at least the query expression"));
return 1;
}
try (CommandThreadManager pool = new CommandThreadManager("Query", getConcurrencyLimit(params.getBuckConfig()));
PerBuildState parserState = new PerBuildState(params.getParser(), params.getBuckEventBus(), pool.getExecutor(), params.getCell(), getEnableParserProfiling(), SpeculativeParsing.of(true), /* ignoreBuckAutodepsFiles */
false)) {
BuckQueryEnvironment env = BuckQueryEnvironment.from(params, parserState, getEnableParserProfiling());
ListeningExecutorService executor = pool.getExecutor();
return formatAndRunQuery(params, env, executor);
} catch (QueryException | BuildFileParseException e) {
throw new HumanReadableException(e);
}
}
use of com.facebook.buck.query.QueryException in project buck by facebook.
the class BuckQueryEnvironment method getFileOwners.
@Override
public ImmutableSet<QueryTarget> getFileOwners(ImmutableList<String> files, ListeningExecutorService executor) throws InterruptedException, QueryException {
try {
BuildFileTree buildFileTree = Preconditions.checkNotNull(buildFileTrees.get(rootCell));
OwnersReport report = ownersReportBuilder.build(buildFileTree, executor, files);
return getTargetsFromTargetNodes(report.owners.keySet());
} catch (BuildFileParseException | IOException e) {
throw new QueryException(e, "Could not parse build targets.\n%s", e.getMessage());
}
}
Aggregations