use of java.nio.file.FileVisitResult in project buck by facebook.
the class DirectoryCleaner method computeDirSizeBytesRecursively.
private static long computeDirSizeBytesRecursively(Path directoryPath) throws IOException {
final AtomicLong totalSizeBytes = new AtomicLong(0);
Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
totalSizeBytes.addAndGet(attrs.size());
return FileVisitResult.CONTINUE;
}
});
return totalSizeBytes.get();
}
use of java.nio.file.FileVisitResult in project buck by facebook.
the class ProjectGenerator method addCoreDataModelBuildPhase.
private void addCoreDataModelBuildPhase(PBXGroup targetGroup, Iterable<AppleWrapperResourceArg> dataModels) throws IOException {
for (final AppleWrapperResourceArg dataModel : dataModels) {
// Core data models go in the resources group also.
PBXGroup resourcesGroup = targetGroup.getOrCreateChildGroupByName("Resources");
if (CoreDataModelDescription.isVersionedDataModel(dataModel)) {
// It's safe to do I/O here to figure out the current version because we're returning all
// the versions and the file pointing to the current version from
// getInputsToCompareToOutput(), so the rule will be correctly detected as stale if any of
// them change.
final String currentVersionFileName = ".xccurrentversion";
final String currentVersionKey = "_XCCurrentVersionName";
final XCVersionGroup versionGroup = resourcesGroup.getOrCreateChildVersionGroupsBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
projectFilesystem.walkRelativeFileTree(dataModel.path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if (dir.equals(dataModel.path)) {
return FileVisitResult.CONTINUE;
}
versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dir), Optional.empty()));
return FileVisitResult.SKIP_SUBTREE;
}
});
Path currentVersionPath = dataModel.path.resolve(currentVersionFileName);
try (InputStream in = projectFilesystem.newFileInputStream(currentVersionPath)) {
NSObject rootObject;
try {
rootObject = PropertyListParser.parse(in);
} catch (IOException e) {
throw e;
} catch (Exception e) {
rootObject = null;
}
if (!(rootObject instanceof NSDictionary)) {
throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
}
NSDictionary rootDictionary = (NSDictionary) rootObject;
NSObject currentVersionName = rootDictionary.objectForKey(currentVersionKey);
if (!(currentVersionName instanceof NSString)) {
throw new HumanReadableException("Malformed %s file.", currentVersionFileName);
}
PBXFileReference ref = versionGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path.resolve(currentVersionName.toString())), Optional.empty()));
versionGroup.setCurrentVersion(Optional.of(ref));
} catch (NoSuchFileException e) {
if (versionGroup.getChildren().size() == 1) {
versionGroup.setCurrentVersion(Optional.of(Iterables.get(versionGroup.getChildren(), 0)));
}
}
} else {
resourcesGroup.getOrCreateFileReferenceBySourceTreePath(new SourceTreePath(PBXReference.SourceTree.SOURCE_ROOT, pathRelativizer.outputDirToRootRelative(dataModel.path), Optional.empty()));
}
}
}
use of java.nio.file.FileVisitResult in project buck by facebook.
the class ArchiveStep method getAllInputs.
private ImmutableList<String> getAllInputs() throws IOException {
ImmutableList.Builder<String> allInputs = ImmutableList.builder();
// found from a recursive search.
for (Path input : inputs) {
if (filesystem.isDirectory(input)) {
// We make sure to sort the files we find under the directories so that we get
// deterministic output.
final Set<String> dirFiles = new TreeSet<>();
filesystem.walkFileTree(filesystem.resolve(input), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
dirFiles.add(file.toString());
return FileVisitResult.CONTINUE;
}
});
allInputs.addAll(dirFiles);
} else {
allInputs.add(input.toString());
}
}
return allInputs.build();
}
use of java.nio.file.FileVisitResult in project buck by facebook.
the class JarDirectoryStepHelper method addFilesInDirectoryToJar.
/**
* @param directory that must not contain symlinks with loops.
* @param jar is the file being written.
*/
private static void addFilesInDirectoryToJar(final ProjectFilesystem filesystem, final Path directory, CustomZipOutputStream jar, final Set<String> alreadyAddedEntries, final Iterable<Pattern> blacklist, final JavacEventSink eventSink) throws IOException {
// Since filesystem traversals can be non-deterministic, sort the entries we find into
// a tree map before writing them out.
final Map<String, Pair<JarEntry, Optional<Path>>> entries = Maps.newTreeMap();
filesystem.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, file));
// Skip re-reading the manifest
if (JarFile.MANIFEST_NAME.equals(relativePath)) {
return FileVisitResult.CONTINUE;
}
// Check if the entry belongs to the blacklist and it should be excluded from the Jar.
if (shouldEntryBeRemovedFromJar(eventSink, relativePath, blacklist)) {
return FileVisitResult.CONTINUE;
}
JarEntry entry = new JarEntry(relativePath);
String entryName = entry.getName();
// We want deterministic JARs, so avoid mtimes.
entry.setTime(ZipConstants.getFakeTime());
// those repeatedly would be lame, so don't do that.
if (!isDuplicateAllowed(entryName) && !alreadyAddedEntries.add(entryName)) {
if (!entryName.endsWith("/")) {
eventSink.reportEvent(determineSeverity(entry), "Duplicate found when adding directory to jar: %s", relativePath);
}
return FileVisitResult.CONTINUE;
}
entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
String relativePath = MorePaths.pathWithUnixSeparators(MorePaths.relativize(directory, dir));
if (relativePath.isEmpty()) {
// root of the tree. Skip.
return FileVisitResult.CONTINUE;
}
String entryName = relativePath.replace('\\', '/') + "/";
if (alreadyAddedEntries.contains(entryName)) {
return FileVisitResult.CONTINUE;
}
JarEntry entry = new JarEntry(entryName);
// We want deterministic JARs, so avoid mtimes.
entry.setTime(ZipConstants.getFakeTime());
entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
return FileVisitResult.CONTINUE;
}
});
// Write the entries out using the iteration order of the tree map above.
for (Pair<JarEntry, Optional<Path>> entry : entries.values()) {
jar.putNextEntry(entry.getFirst());
if (entry.getSecond().isPresent()) {
Files.copy(entry.getSecond().get(), jar);
}
jar.closeEntry();
}
}
use of java.nio.file.FileVisitResult in project buck by facebook.
the class ProvisioningProfileCopyStepTest method setUp.
@Before
public void setUp() throws IOException {
testdataDir = TestDataHelper.getTestDataDirectory(this).resolve("provisioning_profiles");
projectFilesystem = new FakeProjectFilesystem(testdataDir);
Files.walkFileTree(testdataDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
projectFilesystem.writeBytesToPath(Files.readAllBytes(file), projectFilesystem.resolve(file));
return FileVisitResult.CONTINUE;
}
});
tempOutputDir = tmp.getRoot();
outputFile = tempOutputDir.resolve("embedded.mobileprovision");
xcentFile = Paths.get("test.xcent");
dryRunResultFile = Paths.get("test_dry_run_results.plist");
executionContext = TestExecutionContext.newInstance();
codeSignIdentityStore = CodeSignIdentityStore.fromIdentities(ImmutableList.of());
entitlementsFile = testdataDir.resolve("Entitlements.plist");
}
Aggregations