use of com.facebook.buck.query.QueryExpression 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.QueryExpression 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.QueryExpression in project buck by facebook.
the class QueryCommand method runMultipleQuery.
/**
* Evaluate multiple queries in a single `buck query` run. Usage:
* buck query <query format> <input1> <input2> <...> <inputN>
*/
static int runMultipleQuery(CommandRunnerParams params, BuckQueryEnvironment env, ListeningExecutorService executor, String queryFormat, List<String> inputsFormattedAsBuildTargets, boolean generateJsonOutput) throws IOException, InterruptedException, QueryException {
if (inputsFormattedAsBuildTargets.isEmpty()) {
params.getBuckEventBus().post(ConsoleEvent.severe("Specify one or more input targets after the query expression format"));
return 1;
}
// Do an initial pass over the query arguments and parse them into their expressions so we can
// preload all the target patterns from every argument in one go, as doing them one-by-one is
// really inefficient.
Set<String> targetLiterals = new LinkedHashSet<>();
for (String input : inputsFormattedAsBuildTargets) {
String query = queryFormat.replace("%s", input);
QueryExpression expr = QueryExpression.parse(query, env);
expr.collectTargetPatterns(targetLiterals);
}
env.preloadTargetPatterns(targetLiterals, executor);
// Now execute the query on the arguments one-by-one.
TreeMultimap<String, QueryTarget> queryResultMap = TreeMultimap.create();
for (String input : inputsFormattedAsBuildTargets) {
String query = queryFormat.replace("%s", input);
ImmutableSet<QueryTarget> queryResult = env.evaluateQuery(query, executor);
queryResultMap.putAll(input, queryResult);
}
LOG.debug("Printing out the following targets: " + queryResultMap);
if (generateJsonOutput) {
CommandHelper.printJSON(params, queryResultMap);
} else {
CommandHelper.printToConsole(params, queryResultMap);
}
return 0;
}
use of com.facebook.buck.query.QueryExpression in project buck by facebook.
the class QueryUtils method resolveDepQuery.
public static Stream<BuildRule> resolveDepQuery(BuildRuleParams params, Query query, BuildRuleResolver resolver, TargetGraph targetGraph) {
BuildTarget target = params.getBuildTarget();
Set<BuildTarget> declaredDeps = params.getDeclaredDeps().get().stream().map(BuildRule::getBuildTarget).collect(Collectors.toSet());
GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(Optional.of(resolver), Optional.of(targetGraph), params.getCellRoots(), BuildTargetPatternParser.forBaseName(target.getBaseName()), declaredDeps);
ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService();
try {
QueryExpression parsedExp = QueryExpression.parse(query.getQuery(), env);
Set<QueryTarget> queryTargets = parsedExp.eval(env, executorService);
return queryTargets.stream().map(queryTarget -> {
Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
return resolver.getRule(((QueryBuildTarget) queryTarget).getBuildTarget());
});
} catch (QueryException e) {
throw new RuntimeException("Error parsing/executing query from deps for " + target, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Error executing query from deps for " + target, e);
}
}
use of com.facebook.buck.query.QueryExpression in project buck by facebook.
the class QueryUtils method extractBuildTargets.
public static Stream<BuildTarget> extractBuildTargets(CellPathResolver cellPathResolver, BuildTargetPatternParser<BuildTargetPattern> parserPattern, Query query) throws QueryException {
GraphEnhancementQueryEnvironment env = new GraphEnhancementQueryEnvironment(Optional.empty(), Optional.empty(), cellPathResolver, parserPattern, ImmutableSet.of());
ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService();
QueryExpression parsedExp = QueryExpression.parse(query.getQuery(), env);
List<String> targetLiterals = new ArrayList<>();
parsedExp.collectTargetPatterns(targetLiterals);
return targetLiterals.stream().flatMap(pattern -> {
try {
return env.getTargetsMatchingPattern(pattern, executorService).stream();
} catch (Exception e) {
throw new RuntimeException("Error parsing target expression", e);
}
}).map(queryTarget -> {
Preconditions.checkState(queryTarget instanceof QueryBuildTarget);
return ((QueryBuildTarget) queryTarget).getBuildTarget();
});
}
Aggregations