use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.
the class JibServiceUtil method layers.
@Nonnull
public static List<FileEntriesLayer> layers(BuildDirs buildDirs, Map<Assembly, List<AssemblyFileEntry>> layers) {
final List<FileEntriesLayer> fileEntriesLayers = new ArrayList<>();
for (Map.Entry<Assembly, List<AssemblyFileEntry>> layer : layers.entrySet()) {
final FileEntriesLayer.Builder fel = FileEntriesLayer.builder();
final String layerId = layer.getKey().getId();
final Path outputPath;
if (StringUtils.isBlank(layerId)) {
outputPath = buildDirs.getOutputDirectory().toPath();
} else {
fel.setName(layerId);
outputPath = new File(buildDirs.getOutputDirectory(), layerId).toPath();
}
for (AssemblyFileEntry afe : layer.getValue()) {
final Path source = afe.getSource().toPath();
final AbsoluteUnixPath target = AbsoluteUnixPath.get(StringUtils.prependIfMissing(FilenameUtils.separatorsToUnix(outputPath.relativize(afe.getDest().toPath()).normalize().toString()), "/"));
final FilePermissions permissions = StringUtils.isNotBlank(afe.getFileMode()) ? FilePermissions.fromOctalString(StringUtils.right(afe.getFileMode(), 3)) : DEFAULT_FILE_PERMISSIONS_PROVIDER.get(source, target);
fel.addEntry(source, target, permissions);
}
fileEntriesLayers.add(fel.build());
}
return fileEntriesLayers;
}
use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.
the class JibServiceUtilTest method layers_withMultipleLayers_shouldReturnTransformedLayers.
@Test
public void layers_withMultipleLayers_shouldReturnTransformedLayers() throws IOException {
// Given
final BuildDirs buildDirs = new BuildDirs("layers-test", JKubeConfiguration.builder().outputDirectory("target/docker").project(JavaProject.builder().baseDirectory(temporaryFolder.getRoot()).build()).build());
final Map<Assembly, List<AssemblyFileEntry>> originalLayers = new LinkedHashMap<>();
originalLayers.put(Assembly.builder().id("layer-1").build(), Arrays.asList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("layer-1").resolve("l1.1.txt").toFile()).build(), AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("layer-1").resolve("l1.2.txt").toFile()).build()));
originalLayers.put(Assembly.builder().build(), Arrays.asList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(new File(buildDirs.getOutputDirectory(), "l2.1.txt")).build(), AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(new File(buildDirs.getOutputDirectory(), "l2.2.txt")).build()));
// Creates a denormalized path in JDK 8
originalLayers.put(Assembly.builder().id("jkube-generated-layer-final-artifact").build(), Collections.singletonList(AssemblyFileEntry.builder().source(temporaryFolder.newFile()).dest(buildDirs.getOutputDirectory().toPath().resolve("jkube-generated-layer-final-artifact").resolve("deployments").resolve(".").resolve("edge.case").toFile()).build()));
// When
final List<FileEntriesLayer> result = JibServiceUtil.layers(buildDirs, originalLayers);
// Then
assertThat(result).hasSize(3).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "layer-1").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/l1.1.txt", "/l1.2.txt")).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/l2.1.txt", "/l2.2.txt")).anySatisfy(fel -> assertThat(fel).hasFieldOrPropertyWithValue("name", "jkube-generated-layer-final-artifact").extracting(FileEntriesLayer::getEntries).asList().extracting("extractionPath.unixPath").containsExactly("/deployments/edge.case")).extracting(FileEntriesLayer::getName).containsExactly("layer-1", "", "jkube-generated-layer-final-artifact");
}
use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.
the class AssemblyFileSetUtils method processAssemblyFileSet.
/**
* Will copy files from the provided <code>baseDirectory</code> into
* <code>outputDirectory/assemblyConfiguration.targetDir[/layer.directory]</code>
* considering the inclusion and exclusion rules defined in the provided {@link AssemblyFileSet}.
*
* @param baseDirectory directory from where to resolve source files.
* @param outputDirectory directory where files should be output.
* @param assemblyFileSet fileSet to process.
* @param layer the layer to which fileSet belongs to.
* @param assemblyConfiguration configuration for assembly.
* @return List containing the copied {@link AssemblyFileEntry} for the processed {@link AssemblyFileSet}
* @throws IOException in case something goes wrong when performing File operations.
*/
@SuppressWarnings("squid:S3864")
@Nonnull
public static List<AssemblyFileEntry> processAssemblyFileSet(File baseDirectory, File outputDirectory, AssemblyFileSet assemblyFileSet, Assembly layer, AssemblyConfiguration assemblyConfiguration) throws IOException {
final File sourceDirectory = resolveSourceDirectory(baseDirectory, assemblyFileSet);
Objects.requireNonNull(assemblyConfiguration.getTargetDir(), "Assembly Configuration target dir is required");
if (!sourceDirectory.exists()) {
return Collections.emptyList();
}
final File targetDirectory;
if (StringUtils.isNotBlank(layer.getId())) {
targetDirectory = new File(new File(outputDirectory, layer.getId()), assemblyConfiguration.getTargetDir());
} else {
targetDirectory = new File(outputDirectory, assemblyConfiguration.getTargetDir());
}
final File destinationDirectory;
if (assemblyFileSet.getOutputDirectory() == null) {
destinationDirectory = new File(targetDirectory, sourceDirectory.getName());
} else if (assemblyFileSet.getOutputDirectory().isAbsolute()) {
destinationDirectory = assemblyFileSet.getOutputDirectory();
} else if (assemblyFileSet.getOutputDirectory().getPath().equals(PATH_TO_SELF)) {
destinationDirectory = targetDirectory;
} else {
destinationDirectory = targetDirectory.toPath().resolve(assemblyFileSet.getOutputDirectory().getPath()).toFile();
}
final List<String> includes = Optional.ofNullable(assemblyFileSet.getIncludes()).filter(i -> !i.isEmpty()).orElse(Collections.singletonList(PATH_TO_SELF));
final List<AssemblyFileEntry> allEntries = new ArrayList<>();
for (String include : includes) {
final String effectiveInclude = isSelfPath(include) ? "**" : include;
allEntries.addAll(processInclude(sourceDirectory.toPath(), effectiveInclude, destinationDirectory.toPath(), assemblyFileSet));
}
return allEntries;
}
use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.
the class JavaExecGeneratorTest method createAssemblyWithNoFatJarShouldAddDefaultFileSets.
@Test
public void createAssemblyWithNoFatJarShouldAddDefaultFileSets() {
// When
final AssemblyConfiguration result = new JavaExecGenerator(generatorContext).createAssembly();
// Then
assertThat(result).hasFieldOrPropertyWithValue("excludeFinalOutputArtifact", false).extracting(AssemblyConfiguration::getLayers).asList().hasSize(1).first().asInstanceOf(InstanceOfAssertFactories.type(Assembly.class)).extracting(Assembly::getFileSets).asList().hasSize(2);
}
use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.
the class JavaExecGeneratorTest method createAssemblyWithFatJarShouldAddDefaultFileSetsAndFatJar.
@Test
public void createAssemblyWithFatJarShouldAddDefaultFileSetsAndFatJar(@Mocked FatJarDetector fatJarDetector, @Mocked FatJarDetector.Result fjResult) {
// Given
// @formatter:off
new Expectations() {
{
generatorContext.getProject().getBuildPackageDirectory();
result = new File("");
generatorContext.getProject().getBaseDirectory();
result = new File("");
fjResult.getArchiveFile();
result = new File("fat.jar");
fatJarDetector.scan();
result = fjResult;
}
};
// @formatter:on
// When
final AssemblyConfiguration result = new JavaExecGenerator(generatorContext).createAssembly();
// Then
assertThat(result).hasFieldOrPropertyWithValue("excludeFinalOutputArtifact", true).extracting(AssemblyConfiguration::getLayers).asList().hasSize(1).first().asInstanceOf(InstanceOfAssertFactories.type(Assembly.class)).extracting(Assembly::getFileSets).asList().hasSize(3).extracting("directory", "includes").containsExactlyInAnyOrder(tuple(new File("src/main/jkube-includes"), Collections.emptyList()), tuple(new File("src/main/jkube-includes/bin"), Collections.emptyList()), tuple(new File(""), Collections.singletonList("fat.jar")));
}
Aggregations