Skip to content

Commit 9036390

Browse files
Add Dependency#overrides stuff
1 parent 12b3175 commit 9036390

File tree

4 files changed

+261
-4
lines changed

4 files changed

+261
-4
lines changed

interface/src/main/java/coursierapi/Dependency.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public final class Dependency implements Serializable {
1313
private String configuration;
1414
private Publication publication;
1515
private boolean transitive;
16+
private final HashMap<DependencyManagement.Key, DependencyManagement.Values> overrides;
1617

1718

1819
private Dependency(Module module, String version) {
@@ -22,6 +23,7 @@ private Dependency(Module module, String version) {
2223
this.configuration = "";
2324
this.publication = null;
2425
this.transitive = true;
26+
this.overrides = new HashMap<>();
2527
}
2628

2729
public static Dependency of(Module module, String version) {
@@ -37,7 +39,8 @@ public static Dependency of(Dependency dependency) {
3739
.withType(dependency.getType())
3840
.withClassifier(dependency.getClassifier())
3941
.withPublication(dependency.getPublication())
40-
.withTransitive(dependency.isTransitive());
42+
.withTransitive(dependency.isTransitive())
43+
.withOverrides(dependency.getOverrides());
4144
}
4245

4346
public static Dependency parse(String dep, ScalaVersion scalaVersion) {
@@ -101,6 +104,23 @@ public Dependency withTransitive(boolean transitive) {
101104
return this;
102105
}
103106

107+
public Dependency addOverride(DependencyManagement.Key key, DependencyManagement.Values values) {
108+
this.overrides.put(key, values);
109+
return this;
110+
}
111+
public Dependency addOverride(String organization, String name, String version) {
112+
this.overrides.put(
113+
new DependencyManagement.Key(organization, name, "", ""),
114+
new DependencyManagement.Values("", version, false));
115+
return this;
116+
}
117+
118+
public Dependency withOverrides(Map<DependencyManagement.Key, DependencyManagement.Values> overrides) {
119+
this.overrides.clear();
120+
this.overrides.putAll(overrides);
121+
return this;
122+
}
123+
104124
@Override
105125
public boolean equals(Object o) {
106126
if (this == o) return true;
@@ -111,7 +131,8 @@ public boolean equals(Object o) {
111131
Objects.equals(version, that.version) &&
112132
Objects.equals(exclusions, that.exclusions) &&
113133
Objects.equals(configuration, that.configuration) &&
114-
Objects.equals(publication, that.publication);
134+
Objects.equals(publication, that.publication) &&
135+
Objects.equals(overrides, that.overrides);
115136
}
116137

117138
@Override
@@ -122,7 +143,8 @@ public int hashCode() {
122143
exclusions,
123144
configuration,
124145
publication,
125-
transitive);
146+
transitive,
147+
overrides);
126148
}
127149

128150
@Override
@@ -134,6 +156,7 @@ public String toString() {
134156
", configuration='" + configuration + '\'' +
135157
", publication=" + publication +
136158
", transitive=" + transitive +
159+
", overrides=" + overrides +
137160
'}';
138161
}
139162

@@ -171,4 +194,8 @@ public Publication getPublication() {
171194
public boolean isTransitive() {
172195
return transitive;
173196
}
197+
198+
public Map<DependencyManagement.Key, DependencyManagement.Values> getOverrides() {
199+
return Collections.unmodifiableMap(overrides);
200+
}
174201
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package coursierapi;
2+
3+
import java.util.*;
4+
5+
public final class DependencyManagement {
6+
7+
private DependencyManagement() {}
8+
9+
public static final class Key {
10+
private final String organization;
11+
private final String name;
12+
private final String type;
13+
private final String classifier;
14+
15+
public Key(String organization, String name, String type, String classifier) {
16+
this.organization = organization;
17+
this.name = name;
18+
this.type = type;
19+
this.classifier = classifier;
20+
}
21+
22+
public String getOrganization() {
23+
return organization;
24+
}
25+
public String getName() {
26+
return name;
27+
}
28+
public String getType() {
29+
return type;
30+
}
31+
public String getClassifier() {
32+
return classifier;
33+
}
34+
35+
public Key withOrganization(String newOrganization) {
36+
return new Key(newOrganization, name, type, classifier);
37+
}
38+
public Key withName(String newName) {
39+
return new Key(organization, newName, type, classifier);
40+
}
41+
public Key withType(String newType) {
42+
return new Key(organization, name, newType, classifier);
43+
}
44+
public Key withClassifier(String newClassifier) {
45+
return new Key(organization, name, type, newClassifier);
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (this == o) return true;
51+
if (!(o instanceof Key)) return false;
52+
Key key = (Key) o;
53+
return Objects.equals(organization, key.organization) && Objects.equals(name, key.name) && Objects.equals(type, key.type) && Objects.equals(classifier, key.classifier);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash(organization, name, type, classifier);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "Key{" +
64+
"organization='" + organization + '\'' +
65+
", name='" + name + '\'' +
66+
", type='" + type + '\'' +
67+
", classifier='" + classifier + '\'' +
68+
'}';
69+
}
70+
}
71+
72+
public static final class Values {
73+
private final String configuration;
74+
private final String version;
75+
private final Set<Map.Entry<String, String>> exclusions = new HashSet<>();
76+
private final boolean optional;
77+
78+
public Values(String configuration, String version, boolean optional) {
79+
this.configuration = configuration;
80+
this.version = version;
81+
this.optional = optional;
82+
}
83+
84+
public Values addExclusion(String organization, String name) {
85+
this.exclusions.add(new AbstractMap.SimpleImmutableEntry<>(organization, name));
86+
return this;
87+
}
88+
public Values addExclusions(Set<Map.Entry<String, String>> exclusions) {
89+
this.exclusions.addAll(exclusions);
90+
return this;
91+
}
92+
93+
public String getConfiguration() {
94+
return configuration;
95+
}
96+
public String getVersion() {
97+
return version;
98+
}
99+
public Set<Map.Entry<String, String>> getExclusions() {
100+
return Collections.unmodifiableSet(exclusions);
101+
}
102+
public boolean isOptional() {
103+
return optional;
104+
}
105+
106+
public Values withConfiguration(String newConfiguration) {
107+
Values newValues = new Values(newConfiguration, version, optional);
108+
newValues.addExclusions(this.exclusions);
109+
return newValues;
110+
}
111+
public Values withVersion(String newVersion) {
112+
Values newValues = new Values(configuration, newVersion, optional);
113+
newValues.addExclusions(this.exclusions);
114+
return newValues;
115+
}
116+
public Values withExclusions(Set<Map.Entry<String, String>> exclusions) {
117+
Values newValues = new Values(configuration, version, optional);
118+
newValues.addExclusions(exclusions);
119+
return newValues;
120+
}
121+
public Values withOptional(boolean newOptional) {
122+
Values newValues = new Values(configuration, version, newOptional);
123+
newValues.addExclusions(this.exclusions);
124+
return newValues;
125+
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) return true;
130+
if (!(o instanceof Values)) return false;
131+
Values values = (Values) o;
132+
return optional == values.optional && Objects.equals(configuration, values.configuration) && Objects.equals(version, values.version) && Objects.equals(exclusions, values.exclusions);
133+
}
134+
135+
@Override
136+
public int hashCode() {
137+
return Objects.hash(configuration, version, exclusions, optional);
138+
}
139+
140+
@Override
141+
public String toString() {
142+
return "Values{" +
143+
"configuration='" + configuration + '\'' +
144+
", version='" + version + '\'' +
145+
", exclusions=" + exclusions +
146+
", optional=" + optional +
147+
'}';
148+
}
149+
}
150+
151+
}

interface/src/main/scala/coursier/internal/api/ApiHelper.scala

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import coursier._
99
import coursierapi.{Credentials, Logger, SimpleLogger}
1010
import coursier.cache.loggers.RefreshLogger
1111
import coursier.cache.{ArchiveCache, CacheDefaults, CacheLogger, FileCache, UnArchiver}
12-
import coursier.core.{Authentication, Configuration, Extension, Publication, Version}
12+
import coursier.core.{Authentication, Configuration, DependencyManagement, Extension, MinimizedExclusions, Publication, Version}
1313
import coursier.error.{CoursierError, FetchError, ResolutionError}
1414
import coursier.ivy.IvyRepository
1515
import coursier.jvm.{JavaHome, JvmCache}
@@ -103,6 +103,52 @@ object ApiHelper {
103103
mod.attributes.asJava
104104
)
105105

106+
def depMgmtKey(key: coursierapi.DependencyManagement.Key): DependencyManagement.Key =
107+
DependencyManagement.Key(
108+
Organization(key.getOrganization),
109+
ModuleName(key.getName),
110+
Type(key.getType),
111+
Classifier(key.getClassifier)
112+
)
113+
def depMgmtKey(key: DependencyManagement.Key): coursierapi.DependencyManagement.Key =
114+
new coursierapi.DependencyManagement.Key(
115+
key.organization.value,
116+
key.name.value,
117+
key.`type`.value,
118+
key.classifier.value
119+
)
120+
121+
def depMgmtValues(values: coursierapi.DependencyManagement.Values): DependencyManagement.Values =
122+
DependencyManagement.Values(
123+
Configuration(values.getConfiguration),
124+
values.getVersion,
125+
MinimizedExclusions(
126+
values.getExclusions.asScala
127+
.map { e =>
128+
(Organization(e.getKey), ModuleName(e.getValue))
129+
}
130+
.toSet
131+
),
132+
values.isOptional
133+
)
134+
def depMgmtValues(values: DependencyManagement.Values): coursierapi.DependencyManagement.Values = {
135+
val apiValues = new coursierapi.DependencyManagement.Values(
136+
values.config.value,
137+
values.version,
138+
values.optional
139+
)
140+
apiValues.withExclusions(
141+
values.minimizedExclusions.toSeq()
142+
.map {
143+
case (org, name) =>
144+
new ju.AbstractMap.SimpleImmutableEntry(org.value, name.value): ju.Map.Entry[String, String]
145+
}
146+
.toSet
147+
.asJava
148+
)
149+
apiValues
150+
}
151+
106152
def dependency(dep: coursierapi.Dependency): Dependency = {
107153

108154
val module0 = module(dep.getModule)
@@ -118,6 +164,14 @@ object ApiHelper {
118164
.withExclusions(exclusions)
119165
.withConfiguration(configuration)
120166
.withTransitive(dep.isTransitive)
167+
.withOverrides(
168+
dep.getOverrides.asScala
169+
.map {
170+
case (key, values) =>
171+
(depMgmtKey(key), depMgmtValues(values))
172+
}
173+
.toMap
174+
)
121175

122176
Option(dep.getPublication)
123177
.map { p =>
@@ -146,6 +200,14 @@ object ApiHelper {
146200
.asJava
147201
)
148202
.withTransitive(dep.transitive)
203+
.withOverrides(
204+
dep.overrides
205+
.map {
206+
case (key, values) =>
207+
(depMgmtKey(key), depMgmtValues(values))
208+
}
209+
.asJava
210+
)
149211

150212
def repository(repo: coursierapi.Repository): Repository =
151213
repo match {

interface/src/test/scala/coursierapi/DependencyTests.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ object DependencyTests extends TestSuite {
5959
assert(dep == dep0)
6060
}
6161

62+
test("overrides") {
63+
val dep = Dependency.of(initialDep)
64+
.withTransitive(false)
65+
.addOverride("some-org", "some-name", "1.2")
66+
.addOverride(
67+
new DependencyManagement.Key("other-org", "other-name", "a-type", "a-classifier"),
68+
new DependencyManagement.Values("a-config", "1.4", false)
69+
.addExclusion("nope-org", "nope-name")
70+
.addExclusion("nope-org2", "nope-name2")
71+
)
72+
73+
val dep0 = ApiHelper.dependency(ApiHelper.dependency(dep))
74+
75+
assert(dep != initialDep)
76+
assert(dep == dep0)
77+
}
78+
6279
}
6380

6481
}

0 commit comments

Comments
 (0)