use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class JavaContainerBuilder method addDirectoryContentsToLayer.
private void addDirectoryContentsToLayer(Map<LayerType, FileEntriesLayer.Builder> layerBuilders, LayerType layerType, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
if (!layerBuilders.containsKey(layerType)) {
layerBuilders.put(layerType, FileEntriesLayer.builder());
}
FileEntriesLayer.Builder builder = layerBuilders.get(layerType);
new DirectoryWalker(sourceRoot).filterRoot().filter(path -> Files.isDirectory(path) || pathFilter.test(path)).walk(path -> {
AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
builder.addEntry(path, pathOnContainer, modificationTime);
});
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class ArtifactLayers method getDirectoryContentsAsLayer.
/**
* Creates a layer containing contents of a directory. Only paths that match the given predicate
* will be added.
*
* @param layerName name of the layer
* @param sourceRoot path to source directory
* @param pathFilter predicate to determine whether to add the path or not
* @param basePathInContainer path to destination on container
* @return {@link FileEntriesLayer} representing the layer
* @throws IOException if io exception occurs when reading from the source directory
*/
public static FileEntriesLayer getDirectoryContentsAsLayer(String layerName, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(layerName);
new DirectoryWalker(sourceRoot).filterRoot().filter(path -> pathFilter.test(path)).walk(path -> {
AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
builder.addEntry(path, pathOnContainer);
});
return builder.build();
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class MavenProjectPropertiesTest method zipUpDirectory.
private static Path zipUpDirectory(Path sourceRoot, Path targetZip) throws IOException {
try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(targetZip))) {
for (Path source : new DirectoryWalker(sourceRoot).filterRoot().walk()) {
StringJoiner pathJoiner = new StringJoiner("/", "", "");
sourceRoot.relativize(source).forEach(element -> pathJoiner.add(element.toString()));
String zipEntryPath = Files.isDirectory(source) ? pathJoiner.toString() + '/' : pathJoiner.toString();
ZipEntry entry = new ZipEntry(zipEntryPath);
zipOut.putNextEntry(entry);
if (!Files.isDirectory(source)) {
try (InputStream in = Files.newInputStream(source)) {
ByteStreams.copy(in, zipOut);
}
}
zipOut.closeEntry();
}
}
return targetZip;
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class TestProject method copyProject.
/**
* Copies test project {@code projectName} to {@code destination} folder.
*/
private static void copyProject(String projectName, Path destination) throws IOException, URISyntaxException {
Path projectPathInResources = Paths.get(Resources.getResource(PROJECTS_PATH_IN_RESOURCES + projectName).toURI());
new DirectoryWalker(projectPathInResources).filterRoot().walk(path -> {
// Creates the same path in the destDir.
Path destPath = destination.resolve(projectPathInResources.relativize(path));
if (Files.isDirectory(path)) {
Files.createDirectory(destPath);
} else {
Files.copy(path, destPath);
}
});
}
use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.
the class TestProject method copyProject.
private void copyProject() throws IOException {
projectRoot = ResourceExtractor.extractResourcePath(TestProject.class, PROJECTS_PATH_IN_RESOURCES + projectDir, newFolder(), true).toPath();
// Puts the correct plugin version into the test project pom.xml.
Path gradleProperties = Paths.get("gradle.properties");
Properties properties = new Properties();
properties.load(Files.newInputStream(gradleProperties));
String pluginVersion = properties.getProperty("version");
new DirectoryWalker(projectRoot).filter(TestProject::isPomXml).walk(pomXml -> Files.write(pomXml, new String(Files.readAllBytes(pomXml), StandardCharsets.UTF_8).replace("@@PluginVersion@@", pluginVersion).getBytes(StandardCharsets.UTF_8)));
}
Aggregations