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

jarek/7209: add classifier to %classpath mvn add #7238

Merged
merged 2 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
#7209: add classifier
  • Loading branch information
jaroslawmalekcodete committed Apr 25, 2018
commit 7079317b8ce5156f8129c7d09d6fa8547a4c6d36
20 changes: 19 additions & 1 deletion doc/groovy/ClasspathMagicCommands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,30 @@
"model = org.apache.jena.rdf.model.ModelFactory.createDefaultModel()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## POM-Classifier Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%classpath config resolver repository.sonatype.platanios https://oss.sonatype.org/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"%classpath add mvn org.platanios tensorflow 0.1.0-SNAPSHOT jar linux-cpu-x86_64"
]
},
{
"cell_type": "markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public class MavenJarResolver {
Expand Down Expand Up @@ -160,20 +161,27 @@ public static class Dependency {

static final String DEFAULT_TYPE = "jar";

String groupId;
String artifactId;
String version;
String type = DEFAULT_TYPE;
private String groupId;
private String artifactId;
private String version;
private String type = DEFAULT_TYPE;
private Optional<String> classifier = Optional.empty();

public Dependency(String groupId, String artifactId, String version) {
this(groupId, artifactId, version, DEFAULT_TYPE);
}

public Dependency(String groupId, String artifactId, String version, String type) {
private Dependency(String groupId, String artifactId, String version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.type = type;
}

public static Dependency create(List<String> args) {
Dependency dependency = new Dependency(args.get(0), args.get(1), args.get(2));
if (args.size() > 3) {
dependency.type = args.get(3);
}
if (args.size() > 4) {
dependency.classifier = Optional.of(args.get(4));
}
return dependency;
}

public String getGroupId() {
Expand All @@ -187,6 +195,14 @@ public String getArtifactId() {
public String getVersion() {
return version;
}

public String getType() {
return type;
}

public Optional<String> getClassifier() {
return classifier;
}
}

public static class AddMvnCommandResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

public class PomFactory {

Expand All @@ -48,14 +49,19 @@ private String configureDependency(Dependency dependency, String pomAsString) {
return pomAsString.replace(
"</dependencies>",
" <dependency>\n" +
" <groupId>" + dependency.groupId + "</groupId>\n" +
" <artifactId>" + dependency.artifactId + "</artifactId>\n" +
" <version>" + dependency.version + "</version>\n" +
" <type>" + dependency.type + "</type>\n" +
" <groupId>" + dependency.getGroupId() + "</groupId>\n" +
" <artifactId>" + dependency.getArtifactId() + "</artifactId>\n" +
" <version>" + dependency.getVersion() + "</version>\n" +
" <type>" + dependency.getType() + "</type>\n" +
classifier(dependency.getClassifier()) +
" </dependency>\n" +
"</dependencies>");
}

private String classifier(Optional<String> classifier) {
return classifier.map(s -> "<classifier>" + s + "</classifier>\n").orElse("");
}

private String configureOutputDir(String pathToMavenRepo, String pomAsString) {
String absolutePath = new File(pathToMavenRepo).getAbsolutePath();
return pomAsString.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class ClassPathAddMvnCellMagicCommand extends ClasspathMagicCommand {
public static final String CLASSPATH_ADD_MVN_CELL = "%" + CLASSPATH + " " + ADD + " " + MVN;
public static final String MVN_CELL_FORMAT_ERROR_MESSAGE =
"Wrong command format, should be " + CLASSPATH_ADD_MVN_CELL + "\n"
+ " group name version or group:name:version" + "\n"
+ " group name version or group:name:version";
+ " group name version [type classifier] or group:name:version[:type:classifier]" + "\n"
+ " group name version [type classifier] or group:name:version[:type:classifier]";
private static final String SPLIT_LINE_REGEX = "\\r?\\n";

private MavenJarResolver.ResolverParams commandParams;
Expand Down Expand Up @@ -93,24 +93,16 @@ private List<MavenJarResolver.Dependency> getDepsFromCommand(String[] lines) {
List<MavenJarResolver.Dependency> dependencies = new ArrayList<>();
for (String line : lines) {
String[] dependencyData = MagicCommandUtils.splitPath(line);
dependencies.add(getDep(dependencyData));
dependencies.add(MavenJarResolver.Dependency.create(Arrays.asList(dependencyData)));
}
return dependencies;
}

private MavenJarResolver.Dependency getDep(String[] split) {
if (split.length == 4) {
return new MavenJarResolver.Dependency(split[0], split[1], split[2], split[3]);
} else {
return new MavenJarResolver.Dependency(split[0], split[1], split[2]);
}
}

private boolean validateCommandLines(String[] commandLines) {
boolean isValid = false;
for (int i = 1; i < commandLines.length; i++) {
String[] strings = MagicCommandUtils.splitPath(commandLines[i]);
if (strings.length == 3 || strings.length == 4) {
if (strings.length > 2) {
isValid = true;
} else {
isValid = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
import com.twosigma.beakerx.kernel.magic.command.MagicCommandExecutionParam;
import com.twosigma.beakerx.kernel.magic.command.MavenJarResolver;
import com.twosigma.beakerx.kernel.magic.command.MavenJarResolver.AddMvnCommandResult;
import com.twosigma.beakerx.kernel.magic.command.MavenJarResolver.Dependency;
import com.twosigma.beakerx.kernel.magic.command.PomFactory;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput;

import static java.util.Arrays.asList;

public class ClasspathAddMvnMagicCommand extends ClasspathMagicCommand {

public static final String ADD = "add";
public static final String MVN = "mvn";
public static final String CLASSPATH_ADD_MVN = CLASSPATH + " " + ADD + " " + MVN;
public static final String ADD_MVN_FORMAT_ERROR_MESSAGE = "Wrong command format, should be" + CLASSPATH_ADD_MVN + " group name version [type] or " + CLASSPATH_ADD_MVN + " group:name:version:[type]";
public static final String ADD_MVN_FORMAT_ERROR_MESSAGE = "Wrong command format, should be" + CLASSPATH_ADD_MVN + " group name version [type classifier] or " + CLASSPATH_ADD_MVN + " group:name:version[:type:classifier]";

private MavenJarResolver.ResolverParams commandParams;
private PomFactory pomFactory;
Expand Down Expand Up @@ -70,44 +73,28 @@ public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, result.getErrorMessage());
}

private MavenJarResolver.Dependency getDependency(String[] split) {
private Dependency getDependency(String[] split) {
if (isGradleFormat(split)) {
String[] valuesFromGradlePattern = split[3].split(":");
return getDepForGradle(valuesFromGradlePattern);
return Dependency.create((asList(valuesFromGradlePattern)));
} else {
return getDepForMaven(split);
return Dependency.create(asList(split).subList(3, split.length));
}
}

private boolean isMavenFormat(String[] split) {
return split.length == 6 || split.length == 7;
return split.length > 5;
}

private boolean isGradleFormat(String[] split) {
if (split.length == 4) {
String[] valuesFromGradlePattern = split[3].split(":");
return valuesFromGradlePattern.length == 3 || valuesFromGradlePattern.length == 4;
return valuesFromGradlePattern.length > 2;
}
return false;
}

private MavenJarResolver.Dependency getDepForMaven(String[] split) {
if (split.length == 7) {
return new MavenJarResolver.Dependency(split[3], split[4], split[5], split[6]);
} else {
return new MavenJarResolver.Dependency(split[3], split[4], split[5]);
}
}

private MavenJarResolver.Dependency getDepForGradle(String[] valuesFromGradlePattern) {
if (valuesFromGradlePattern.length == 4) {
return new MavenJarResolver.Dependency(valuesFromGradlePattern[0], valuesFromGradlePattern[1], valuesFromGradlePattern[2], valuesFromGradlePattern[3]);
} else {
return new MavenJarResolver.Dependency(valuesFromGradlePattern[0], valuesFromGradlePattern[1], valuesFromGradlePattern[2]);
}
}

private AddMvnCommandResult retrieve(MavenJarResolver.Dependency dependency, MavenJarResolver classpathAddMvnCommand, MvnLoggerWidget progress) {
private AddMvnCommandResult retrieve(Dependency dependency, MavenJarResolver classpathAddMvnCommand, MvnLoggerWidget progress) {
return classpathAddMvnCommand.retrieve(dependency, progress);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Map;

import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.deleteWhitespace;
import static org.apache.commons.lang3.StringUtils.normalizeSpace;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -72,22 +72,43 @@ public void createPomWithRepos() throws Exception {
Map<String, String> repos = Maps.newHashMap();
repos.put("repo2", "urlToRepo2");
repos.put("repository.spring.snapshot", "http://repo.spring.io/snapshot");
Dependency dependency = new Dependency("", "", "");
Dependency dependency = Dependency.create(asList("", "", ""));
//when
String pomAsString = pomFactory.createPom("/", Arrays.asList(dependency), repos);
String pomAsString = pomFactory.createPom("/", asList(dependency), repos);
//then
assertThat(removeWhitespaces(pomAsString)).contains(removeWhitespaces(EXPECTED_RESULT_BLOCK));
}

@Test
public void createPomWithMultipleDependencies() throws Exception{
public void createPomWithMultipleDependencies() throws Exception {
Map<String, String> repos = Maps.newHashMap();
Dependency dependency1 = new Dependency("group", "artifact", "1.1.1");
Dependency dependency2 = new Dependency("other-group", "other-artifact", "1.1.1");
String pomAsString = pomFactory.createPom("/", Arrays.asList(dependency1, dependency2), repos);
Dependency dependency1 = Dependency.create(asList("group", "artifact", "1.1.1"));
Dependency dependency2 = Dependency.create(asList("other-group", "other-artifact", "1.1.1"));
String pomAsString = pomFactory.createPom("/", asList(dependency1, dependency2), repos);
assertThat(removeWhitespaces(pomAsString).contains(removeWhitespaces(EXPECTED_MULTIPLE_DEP_POM)));
}

@Test
public void createPomWithType() throws Exception {
//given
Dependency dependency = Dependency.create(asList("group", "art", "ver", "pom"));
//when
String pomAsString = pomFactory.createPom("/", asList(dependency), Maps.newHashMap());
//then
assertThat(removeWhitespaces(pomAsString)).contains("<type>pom</type>");
assertThat(removeWhitespaces(pomAsString)).doesNotContain("<classifier");
}

@Test
public void createPomWithClassifier() throws Exception {
//given
Dependency dependency = Dependency.create(asList("group", "art", "ver", "pom", "Classifier"));
//when
String pomAsString = pomFactory.createPom("/", asList(dependency), Maps.newHashMap());
//then
assertThat(removeWhitespaces(pomAsString)).contains("<classifier>Classifier</classifier>");
}

private String removeWhitespaces(String pomAsString) {
return normalizeSpace(deleteWhitespace(pomAsString));
}
Expand Down