Skip to content

Commit

Permalink
Introduce publication (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex authored Jul 11, 2022
1 parent 2549a40 commit bb59b67
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 3 deletions.
25 changes: 25 additions & 0 deletions interface-test/src/test/java/coursierapi/test/FetchTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coursierapi.test;

import coursierapi.Dependency;
import coursierapi.Publication;
import coursierapi.Fetch;
import coursierapi.FetchResult;
import coursierapi.error.CoursierError;
Expand Down Expand Up @@ -44,4 +45,28 @@ public void simple() {

}


@Test
public void withPublication() {
Dependency dep = Dependency.of("com.google.protobuf", "protoc", "3.18.2")
.withPublication(new Publication("protoc", "jar", "exe", "windows-x86_32"));

Fetch fetch = Fetch.create().addDependencies(dep);

FetchResult result;
try {
result = fetch.fetchResult();
} catch (CoursierError e) {
throw new RuntimeException(e);
}

Set<String> fileNames = new HashSet<>();
for (File f : result.getFiles()) {
fileNames.add(f.getName());
}

Set<String> expectedFileNames = new HashSet<>(Arrays.asList("protoc-3.18.2-windows-x86_32.exe"));
assertEquals(expectedFileNames, fileNames);
}

}
19 changes: 18 additions & 1 deletion interface/src/main/java/coursierapi/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class Dependency implements Serializable {
private String configuration;
private String type;
private String classifier;
private Publication publication;
private boolean transitive;


Expand All @@ -23,6 +24,7 @@ private Dependency(Module module, String version) {
this.configuration = "";
this.type = "";
this.classifier = "";
this.publication = null;
this.transitive = true;
}

Expand All @@ -38,6 +40,7 @@ public static Dependency of(Dependency dependency) {
.withConfiguration(dependency.getConfiguration())
.withType(dependency.getType())
.withClassifier(dependency.getClassifier())
.withPublication(dependency.getPublication())
.withTransitive(dependency.isTransitive());
}

Expand Down Expand Up @@ -82,6 +85,11 @@ public Dependency withClassifier(String classifier) {
return this;
}

public Dependency withPublication(Publication publication) {
this.publication = publication;
return this;
}

public Dependency withTransitive(boolean transitive) {
this.transitive = transitive;
return this;
Expand All @@ -99,14 +107,15 @@ public boolean equals(Object obj) {
this.configuration.equals(other.configuration) &&
this.type.equals(other.type) &&
this.classifier.equals(other.classifier) &&
Objects.equals(this.publication, other.publication) &&
this.transitive == other.transitive;
}
return false;
}

@Override
public int hashCode() {
return 37 * (37 * (37 * (37 * (37 * (37 * (17 + module.hashCode()) + version.hashCode()) + exclusions.hashCode()) + configuration.hashCode()) + type.hashCode()) + classifier.hashCode()) + Boolean.hashCode(transitive);
return 37 * (37 * (37 * (37 * (37 * (37 * (37 * (17 + module.hashCode()) + version.hashCode()) + exclusions.hashCode()) + configuration.hashCode()) + type.hashCode()) + classifier.hashCode()) + Objects.hashCode(publication)) + Boolean.hashCode(transitive);
}

@Override
Expand Down Expand Up @@ -135,6 +144,10 @@ public String toString() {
b.append(", classifier=");
b.append(classifier);
}
if (publication != null) {
b.append(", publication=");
b.append(publication);
}
if (!transitive) {
b.append(", intransitive");
}
Expand Down Expand Up @@ -167,6 +180,10 @@ public String getClassifier() {
return classifier;
}

public Publication getPublication() {
return publication;
}

public boolean isTransitive() {
return transitive;
}
Expand Down
86 changes: 86 additions & 0 deletions interface/src/main/java/coursierapi/Publication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package coursierapi;

import java.io.Serializable;

public final class Publication implements Serializable {

private final String name;
private final String type;
private final String extension;
private final String classifier;

public Publication(String name, String type, String extension, String classifier) {
this.name = name;
this.type = type;
this.extension = extension;
this.classifier = classifier;
}

public Publication(String name) {
this(name, "", "", "");
}

public Publication(String name, String type) {
this(name, type, "", "");
}

public Publication(String name, String type, String extension) {
this(name, type, extension, "");
}

public String getName() {
return name;
}

public String getType() {
return type;
}

public String getExtension() {
return extension;
}

public String getClassifier() {
return classifier;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof Publication) {
Publication other = (Publication) o;
return name.equals(other.name) &&
type.equals(other.type) &&
extension.equals(other.extension) &&
classifier.equals(other.classifier);
}
return false;
}

@Override
public int hashCode() {
return 37 * (37 * (37 * (37 * (17 + extension.hashCode()) + classifier.hashCode()) + type.hashCode()) + name.hashCode());
}

@Override
public String toString() {
StringBuilder b = new StringBuilder("Publication(");
b.append(name);
b.append(", ");
if (!type.isEmpty()) {
b.append(", type=");
b.append(type);
}
if (!classifier.isEmpty()) {
b.append(", classifier=");
b.append(classifier);
}
if (extension != null) {
b.append(", extension=");
b.append(extension);
}
b.append(")");
return b.toString();
}
}
11 changes: 9 additions & 2 deletions interface/src/main/scala/coursier/internal/api/ApiHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import coursier._
import coursierapi.{Credentials, Logger, SimpleLogger}
import coursier.cache.loggers.RefreshLogger
import coursier.cache.{ArchiveCache, CacheDefaults, CacheLogger, FileCache, UnArchiver}
import coursier.core.{Authentication, Configuration, Version}
import coursier.core.{Authentication, Configuration, Extension, Publication, Version}
import coursier.error.{CoursierError, FetchError, ResolutionError}
import coursier.ivy.IvyRepository
import coursier.jvm.{JavaHome, JvmCache}
Expand Down Expand Up @@ -117,11 +117,18 @@ object ApiHelper {
val tpe = Type(dep.getType)
val classifier = Classifier(dep.getClassifier)

Dependency(module0, dep.getVersion)
val dep0 = Dependency(module0, dep.getVersion)
.withExclusions(exclusions)
.withConfiguration(configuration)
.withAttributes(Attributes(tpe, classifier))
.withTransitive(dep.isTransitive)

Option(dep.getPublication)
.map { p =>
val p0 = Publication(p.getName, Type(p.getType), Extension(p.getExtension), Classifier(p.getClassifier))
dep0.withPublication(p0)
}
.getOrElse(dep0)
}

def dependency(dep: Dependency): coursierapi.Dependency =
Expand Down

0 comments on commit bb59b67

Please sign in to comment.