use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class FineGrainedJavaDependencySuggester method extractProvidedSymbolInfoFromSourceFile.
/**
* Extracts the features from {@code src} and updates the collections accordingly.
*/
private void extractProvidedSymbolInfoFromSourceFile(SourcePath src, JavaFileParser javaFileParser, Multimap<String, String> providedSymbolToRequiredSymbols, Map<String, PathSourcePath> providedSymbolToSrc) {
if (!(src instanceof PathSourcePath)) {
return;
}
PathSourcePath path = (PathSourcePath) src;
ProjectFilesystem filesystem = path.getFilesystem();
Optional<String> contents = filesystem.readFileIfItExists(path.getRelativePath());
if (!contents.isPresent()) {
throw new RuntimeException(String.format("Could not read file '%s'", path.getRelativePath()));
}
JavaFileParser.JavaFileFeatures features = javaFileParser.extractFeaturesFromJavaCode(contents.get());
// If there are multiple provided symbols, that is because there are inner classes. Choosing
// the shortest name will effectively select the top-level type.
String providedSymbol = Iterables.getFirst(features.providedSymbols, /* defaultValue */
null);
if (providedSymbol == null) {
console.getStdErr().printf("%s cowardly refuses to provide any types.\n", path.getRelativePath());
return;
}
providedSymbolToSrc.put(providedSymbol, path);
providedSymbolToRequiredSymbols.putAll(providedSymbol, features.requiredSymbols);
providedSymbolToRequiredSymbols.putAll(providedSymbol, features.exportedSymbols);
}
use of com.facebook.buck.io.ProjectFilesystem 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;
}
use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class BuckQueryEnvironment method getBuildFiles.
@Override
public ImmutableSet<QueryTarget> getBuildFiles(Set<QueryTarget> targets) throws QueryException {
final ProjectFilesystem cellFilesystem = rootCell.getFilesystem();
final Path rootPath = cellFilesystem.getRootPath();
Preconditions.checkState(rootPath.isAbsolute());
ImmutableSet.Builder<QueryTarget> builder = ImmutableSet.builder();
for (QueryTarget target : targets) {
Preconditions.checkState(target instanceof QueryBuildTarget);
BuildTarget buildTarget = ((QueryBuildTarget) target).getBuildTarget();
Cell cell = rootCell.getCell(buildTarget);
if (!buildFileTrees.containsKey(cell)) {
LOG.info("Creating a new filesystem-backed build file tree for %s", cell.getRoot());
buildFileTrees.put(cell, new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()));
}
BuildFileTree buildFileTree = Preconditions.checkNotNull(buildFileTrees.get(cell));
Optional<Path> path = buildFileTree.getBasePathOfAncestorTarget(buildTarget.getBasePath());
Preconditions.checkState(path.isPresent());
Path buildFilePath = MorePaths.relativize(rootPath, cell.getFilesystem().resolve(path.get()).resolve(cell.getBuildFileName()));
Preconditions.checkState(cellFilesystem.exists(buildFilePath));
builder.add(QueryFileTarget.of(buildFilePath));
}
return builder.build();
}
use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class ArtifactCaches method createHttpArtifactCache.
private static ArtifactCache createHttpArtifactCache(HttpCacheEntry cacheDescription, final String hostToReportToRemote, final BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem, ListeningExecutorService httpWriteExecutorService, ArtifactCacheBuckConfig config, NetworkCacheFactory factory, boolean distributedBuildModeEnabled) {
// Setup the default client to use.
OkHttpClient.Builder storeClientBuilder = new OkHttpClient.Builder();
storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(chain.request().newBuilder().addHeader("X-BuckCache-User", stripNonAscii(System.getProperty("user.name", "<unknown>"))).addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote)).build()));
int timeoutSeconds = cacheDescription.getTimeoutSeconds();
setTimeouts(storeClientBuilder, timeoutSeconds);
storeClientBuilder.connectionPool(new ConnectionPool(/* maxIdleConnections */
(int) config.getThreadPoolSize(), /* keepAliveDurationMs */
config.getThreadPoolKeepAliveDurationMillis(), TimeUnit.MILLISECONDS));
// The artifact cache effectively only connects to a single host at a time. We should allow as
// many concurrent connections to that host as we allow threads.
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost((int) config.getThreadPoolSize());
storeClientBuilder.dispatcher(dispatcher);
final ImmutableMap<String, String> readHeaders = cacheDescription.getReadHeaders();
final ImmutableMap<String, String> writeHeaders = cacheDescription.getWriteHeaders();
// If write headers are specified, add them to every default client request.
if (!writeHeaders.isEmpty()) {
storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), writeHeaders).build()));
}
OkHttpClient storeClient = storeClientBuilder.build();
// For fetches, use a client with a read timeout.
OkHttpClient.Builder fetchClientBuilder = storeClient.newBuilder();
setTimeouts(fetchClientBuilder, timeoutSeconds);
// If read headers are specified, add them to every read client request.
if (!readHeaders.isEmpty()) {
fetchClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), readHeaders).build()));
}
fetchClientBuilder.networkInterceptors().add((chain -> {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), buckEventBus)).build();
}));
OkHttpClient fetchClient = fetchClientBuilder.build();
HttpService fetchService;
HttpService storeService;
switch(config.getLoadBalancingType()) {
case CLIENT_SLB:
HttpLoadBalancer clientSideSlb = config.getSlbConfig().createClientSideSlb(new DefaultClock(), buckEventBus, new CommandThreadFactory("ArtifactCaches.HttpLoadBalancer", SLB_THREAD_PRIORITY));
fetchService = new RetryingHttpService(buckEventBus, new LoadBalancedService(clientSideSlb, fetchClient, buckEventBus), config.getMaxFetchRetries());
storeService = new LoadBalancedService(clientSideSlb, storeClient, buckEventBus);
break;
case SINGLE_SERVER:
URI url = cacheDescription.getUrl();
fetchService = new SingleUriService(url, fetchClient);
storeService = new SingleUriService(url, storeClient);
break;
default:
throw new IllegalArgumentException("Unknown HttpLoadBalancer type: " + config.getLoadBalancingType());
}
String cacheName = cacheDescription.getName().map(input -> "http-" + input).orElse("http");
boolean doStore = cacheDescription.getCacheReadMode().isDoStore();
return factory.newInstance(NetworkCacheArgs.builder().setThriftEndpointPath(config.getHybridThriftEndpoint()).setCacheName(cacheName).setRepository(config.getRepository()).setScheduleType(config.getScheduleType()).setFetchClient(fetchService).setStoreClient(storeService).setDoStore(doStore).setProjectFilesystem(projectFilesystem).setBuckEventBus(buckEventBus).setHttpWriteExecutorService(httpWriteExecutorService).setErrorTextTemplate(cacheDescription.getErrorMessageFormat()).setDistributedBuildModeEnabled(distributedBuildModeEnabled).build());
}
use of com.facebook.buck.io.ProjectFilesystem in project buck by facebook.
the class BuildCommand method processSuccessfulBuild.
private int processSuccessfulBuild(CommandRunnerParams params, ActionAndTargetGraphs graphs) throws IOException {
if (showOutput || showFullOutput || showRuleKey) {
showOutputs(params, graphs.actionGraph);
}
if (outputPathForSingleBuildTarget != null) {
BuildTarget loneTarget = Iterables.getOnlyElement(graphs.getTargetGraphForLocalBuild().getBuildTargets());
BuildRule rule = graphs.actionGraph.getResolver().getRule(loneTarget);
if (!rule.outputFileCanBeCopied()) {
params.getConsole().printErrorText(String.format("%s does not have an output that is compatible with `buck build --out`", loneTarget));
return 1;
} else {
SourcePath output = Preconditions.checkNotNull(rule.getSourcePathToOutput(), "%s specified a build target that does not have an output file: %s", OUT_LONG_ARG, loneTarget);
ProjectFilesystem projectFilesystem = rule.getProjectFilesystem();
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(graphs.actionGraph.getResolver()));
projectFilesystem.copyFile(pathResolver.getAbsolutePath(output), outputPathForSingleBuildTarget);
}
}
return 0;
}
Aggregations