Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra side artifacts support. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package apollo.deploy_options;

import apollo.maven.MavenDeployOption;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.apache.commons.io.FilenameUtils;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExtraSideArtifactsDeployOption implements MavenDeployOption {

private static class SideArtifact {
private final Path file;
private final String classifier;
private final String type;

private SideArtifact(Path file, String classifier, String type) {
this.file = file;
this.classifier = classifier;
this.type = type;
}
}

private final String filesOption;
private final String classifiersOption;
private final String typesOption;

@Inject
public ExtraSideArtifactsDeployOption(@Named("filesOption") String filesOption,
@Named("classifiersOption") String classifiersOption,
@Named("typesOption") String typesOption) {
this.filesOption = filesOption;
this.classifiersOption = classifiersOption;
this.typesOption = typesOption;
}

@Override
public Optional<String> getCommandOption(Path pathToPom) {
String pomFileName = FilenameUtils.getName(pathToPom.getFileName().toString());
if (!pomFileName.endsWith(".pom")) {
throw new IllegalArgumentException("POM file name must end with .pom");
}

try (Stream<Path> files = Files.walk(pathToPom.getParent(), 1)) {
String fileNamePrefix = pomFileName.substring(0, pomFileName.length() - ".pom".length()) + "-";
List<SideArtifact> artifacts = files.filter(Files::isRegularFile)
.filter((path) -> path.getFileName().toString().startsWith(fileNamePrefix))
.filter(ExtraSideArtifactsDeployOption::notHashFile)
.filter(ExtraSideArtifactsDeployOption::notSignatureFile)
.filter(ExtraSideArtifactsDeployOption::notLastUpdatedFile)
.filter(ExtraSideArtifactsDeployOption::notSourcesFile)
.filter(ExtraSideArtifactsDeployOption::notJavadocFile)
.map(toArtifact(fileNamePrefix))
.collect(Collectors.toList());

if (!artifacts.isEmpty()) {
return Optional.of(String.format("%s%s %s%s %s%s",
filesOption, artifacts.stream().map(artifact -> artifact.file.toString()).collect(Collectors.joining(",")),
classifiersOption, artifacts.stream().map(artifact -> artifact.classifier).collect(Collectors.joining(",")),
typesOption, artifacts.stream().map(artifact -> artifact.type).collect(Collectors.joining(","))));

} else {
return Optional.empty();
}

} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private Function<Path, SideArtifact> toArtifact(String fileNamePrefix) {
return path -> {
String rest = path.getFileName().toString().substring(fileNamePrefix.length());
int i = rest.lastIndexOf('.');
if (i == -1) {
throw new IllegalArgumentException("File name must end with -classifier.type");
}
String classifier = rest.substring(0, i);
String type = rest.substring(i + 1);
return new SideArtifact(path, classifier, type);
};
}

private static boolean notHashFile(Path path) {
String fileName = path.getFileName().toString();
return !fileName.endsWith(".sha1") && !fileName.endsWith(".md5") && !fileName.endsWith(".sha256") && !fileName.endsWith(".sha512");
}

private static boolean notSignatureFile(Path path) {
String fileName = path.getFileName().toString();
return !fileName.endsWith(".asc");
}

private static boolean notLastUpdatedFile(Path path) {
String fileName = path.getFileName().toString();
return !fileName.endsWith(".lastUpdated");
}

private static boolean notSourcesFile(Path path) {
String fileName = path.getFileName().toString();
return !fileName.endsWith("-sources.jar");
}

private static boolean notJavadocFile(Path path) {
String fileName = path.getFileName().toString();
return !fileName.endsWith("-javadoc.jar");
}

}
3 changes: 3 additions & 0 deletions src/main/java/apollo/modules/MavenCommandOptionsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ protected void configure() {
bind(String.class).annotatedWith(Names.named("packagingOption")).toInstance("-Dpackaging=pom");
bind(String.class).annotatedWith(Names.named("sourcesOption")).toInstance("-Dsources=");
bind(String.class).annotatedWith(Names.named("javadocOption")).toInstance("-Djavadoc=");
bind(String.class).annotatedWith(Names.named("filesOption")).toInstance("-Dfiles=");
bind(String.class).annotatedWith(Names.named("classifiersOption")).toInstance("-Dclassifiers=");
bind(String.class).annotatedWith(Names.named("typesOption")).toInstance("-Dtypes=");
}
}
6 changes: 4 additions & 2 deletions src/main/java/apollo/modules/MavenCommandsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ public class MavenCommandsProvider implements Provider<List<MavenDeployOption>>
private final PackagingOption packagingOption;
private final SourcesDeployOption sourcesDeployOption;
private final JavadocDeployOption javadocDeployOption;
private final ExtraSideArtifactsDeployOption extraSideArtifactsDeployOption;

@Inject
public MavenCommandsProvider(PomDeployOption pomDeployOption, FileDeployOption fileDeployOption, RepositoryIdOption repositoryIdOption, UrlDeployOption urlDeployOption, PackagingOption packagingOption, SourcesDeployOption sourcesDeployOption, JavadocDeployOption javadocDeployOption) {
public MavenCommandsProvider(PomDeployOption pomDeployOption, FileDeployOption fileDeployOption, RepositoryIdOption repositoryIdOption, UrlDeployOption urlDeployOption, PackagingOption packagingOption, SourcesDeployOption sourcesDeployOption, JavadocDeployOption javadocDeployOption, ExtraSideArtifactsDeployOption extraSideArtifactsDeployOption) {
this.pomDeployOption = pomDeployOption;
this.fileDeployOption = fileDeployOption;
this.repositoryIdOption = repositoryIdOption;
this.urlDeployOption = urlDeployOption;
this.packagingOption = packagingOption;
this.sourcesDeployOption = sourcesDeployOption;
this.javadocDeployOption = javadocDeployOption;
this.extraSideArtifactsDeployOption = extraSideArtifactsDeployOption;
}

@Override
public List<MavenDeployOption> get() {
return Arrays.asList(pomDeployOption, fileDeployOption, repositoryIdOption, urlDeployOption, packagingOption, sourcesDeployOption, javadocDeployOption);
return Arrays.asList(pomDeployOption, fileDeployOption, repositoryIdOption, urlDeployOption, packagingOption, sourcesDeployOption, javadocDeployOption, extraSideArtifactsDeployOption);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package delpoy_options_tests;

import apollo.deploy_options.ExtraSideArtifactsDeployOption;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.nio.file.Paths;
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class ExtraSideArtifactsDeployOptionTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private final String filesOption = "-Dfiles=";
private final String classifiersOption = "-Dclassifiers=";
private final String typesOption = "-Dtypes=";

private ExtraSideArtifactsDeployOption extraSideArtifactsDeployOption = new ExtraSideArtifactsDeployOption(filesOption, classifiersOption, typesOption);

@Test
public void pomWithExtraSideArtifacts() throws Exception {
File tmpFolder = temporaryFolder.newFolder("test");
File tmpPom = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile.pom").toString());
File tmpDebugJar = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar").toString());
File tmpSitePdf = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-site.pdf").toString());

Optional<String> command = extraSideArtifactsDeployOption.getCommandOption(tmpPom.toPath());

assertThat(command.get(), Matchers.is(filesOption + tmpDebugJar + "," + tmpSitePdf + " " + classifiersOption + "debug,site" + " " + typesOption + "jar,pdf"));
}

@Test
public void hashAndSignatureFilesShouldBeOmitted() throws Exception {
File tmpFolder = temporaryFolder.newFolder("test");
File tmpPom = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile.pom").toString());
File tmpDebugJar = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar").toString());
File tmpDebugJarAsc = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar.asc").toString());
File tmpDebugJarHash = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar.sha512").toString());
File tmpDebugJarAscHash = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar.asc.sha512").toString());
File tmpSitePdf = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-site.pdf").toString());

Optional<String> command = extraSideArtifactsDeployOption.getCommandOption(tmpPom.toPath());

assertThat(command.get(), Matchers.is(filesOption + tmpDebugJar + "," + tmpSitePdf + " " + classifiersOption + "debug,site" + " " + typesOption + "jar,pdf"));
}

@Test
public void lastUpdatedFilesShouldBeOmitted() throws Exception {
File tmpFolder = temporaryFolder.newFolder("test");
File tmpPom = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile.pom").toString());
File tmpDebugJarLastUpdated = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar.lastUpdated").toString());

Optional<String> command = extraSideArtifactsDeployOption.getCommandOption(tmpPom.toPath());

assertThat(command.isPresent(), is(false));
}

@Test
public void sourcesAndJavdocFilesShouldBeOmitted() throws Exception {
File tmpFolder = temporaryFolder.newFolder("test");
File tmpPom = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile.pom").toString());
File tmpDebugJar = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-debug.jar").toString());
File tmpSourcesJar = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-sources.jar").toString());
File tmpSourcesJarAsc = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-sources.jar.asc").toString());
File tmpSourcesJarHash = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-sources.jar.md5").toString());
File tmpSourcesJarAscHash = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-sources.jar.asc.md5").toString());
File tmpJavadocJar = temporaryFolder.newFile(Paths.get(tmpFolder.getName()).resolve("tmpFile-javadoc.jar").toString());

Optional<String> command = extraSideArtifactsDeployOption.getCommandOption(tmpPom.toPath());

assertThat(command.get(), Matchers.is(filesOption + tmpDebugJar + " " + classifiersOption + "debug" + " " + typesOption + "jar"));
}

}