1919
2020package net .flintmc .gradle .maven ;
2121
22- import com .google .common .base .Objects ;
23- import java .util .ArrayList ;
24- import java .util .List ;
22+ import net .flintmc .gradle .extension .FlintGradleExtension ;
2523import org .gradle .api .Project ;
2624import org .gradle .api .artifacts .ModuleVersionSelector ;
2725
26+ import java .util .ArrayList ;
27+ import java .util .List ;
28+ import java .util .Objects ;
29+
2830public class FlintResolutionStrategy {
2931
3032 private static final FlintResolutionStrategy instance = new FlintResolutionStrategy ();
@@ -54,8 +56,8 @@ public static FlintResolutionStrategy getInstance() {
5456 }
5557
5658 /**
57- * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new
58- * forced modules to be considered when resolving dependencies. It accepts following notations:
59+ * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to
60+ * be considered when resolving dependencies. It accepts following notations:
5961 *
6062 * <ul>
6163 * <li>String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
@@ -67,22 +69,22 @@ public static FlintResolutionStrategy getInstance() {
6769 public void forceDependency (Object moduleVersionSelectorNotation ) {
6870 ForcedDependency forcedDependency = this .createForcedDependency (moduleVersionSelectorNotation );
6971
70- if (forcedDependency == null ) {
72+ if (forcedDependency == null ) {
7173 return ;
7274 }
7375
74- for (int index = 0 ; index < this .forcedDependencies .size (); index ++) {
76+ for (int index = 0 ; index < this .forcedDependencies .size (); index ++) {
7577 ForcedDependency dependency = this .createForcedDependency (this .forcedDependencies .get (index ));
7678
77- if (dependency == null ) {
79+ if (dependency == null ) {
7880 return ;
7981 }
8082
81- if (dependency .equals (forcedDependency )) {
83+ if (dependency .equals (forcedDependency )) {
8284 return ;
8385 }
8486
85- if (dependency .nonVersionEquals (forcedDependency )) {
87+ if (dependency .nonVersionEquals (forcedDependency )) {
8688 this .forcedDependencies .set (index , moduleVersionSelectorNotation );
8789 return ;
8890 }
@@ -92,8 +94,8 @@ public void forceDependency(Object moduleVersionSelectorNotation) {
9294 }
9395
9496 /**
95- * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new
96- * forced modules to be considered when resolving dependencies. It accepts following notations:
97+ * Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to
98+ * be considered when resolving dependencies. It accepts following notations:
9799 *
98100 * <ul>
99101 * <li>String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
@@ -104,47 +106,63 @@ public void forceDependency(Object moduleVersionSelectorNotation) {
104106 * @param moduleVersionSelectorNotations Typically group:name:version notations to append.
105107 */
106108 public void forceDependencies (Object ... moduleVersionSelectorNotations ) {
107- for (Object moduleVersionSelectorNotation : moduleVersionSelectorNotations ) {
109+ for (Object moduleVersionSelectorNotation : moduleVersionSelectorNotations ) {
108110 this .forceDependency (moduleVersionSelectorNotation );
109111 }
110112 }
111113
112114 /**
113115 * Forces a collection of dependencies on all configurations of the given {@code project}.
114116 *
115- * @param project The project for which the dependencies are to be enforced.
117+ * @param project The project for which the dependencies are to be enforced.
118+ * @param extension The flint extension for the project
116119 */
117- public void forceResolutionStrategy (Project project ) {
120+ public void forceResolutionStrategy (Project project , FlintGradleExtension extension ) {
121+ String flintVersion = extension .getFlintVersion ();
122+
118123 project
119124 .getConfigurations ()
120125 .forEach (
121126 configuration ->
122127 configuration .resolutionStrategy (
123128 resolutionStrategy -> {
124129 forcedDependencies .forEach (resolutionStrategy ::force );
130+ resolutionStrategy .eachDependency ((details ) -> {
131+ ModuleVersionSelector selector = details .getRequested ();
132+
133+ if (selector .getGroup ().equals ("net.flintmc" ) && !Objects .equals (selector .getVersion (), flintVersion )) {
134+
135+ // Force the flint version to what is specified in the project
136+ details .useVersion (flintVersion );
137+ details .because (
138+ "Forcing net.flintmc dependency from " + selector .getVersion () + " to " +
139+ flintVersion + " to ensure a consistent framework version " +
140+ "[Automatically done by flint-gradle]" );
141+ }
142+ });
125143 }));
126144 }
127145
128146 private ForcedDependency createForcedDependency (Object moduleVersionSelectorNotation ) {
129147
130- if (moduleVersionSelectorNotation instanceof String ) {
148+ if (moduleVersionSelectorNotation instanceof String ) {
131149 String notation = (String ) moduleVersionSelectorNotation ;
132150
133- if (!notation .contains (":" )) {
151+ if (!notation .contains (":" )) {
134152 return null ;
135153 }
136154
137155 String [] split = notation .split (":" );
138156
139- if (split .length == 3 ) {
157+ if (split .length == 3 ) {
140158
141159 String group = split [0 ];
142160 String name = split [1 ];
143161 String version = split [2 ];
144162
145163 return new ForcedDependency (group , name , version );
146164 }
147- } else if (moduleVersionSelectorNotation instanceof ModuleVersionSelector ) {
165+ } else if (moduleVersionSelectorNotation instanceof ModuleVersionSelector ) {
148166 ModuleVersionSelector moduleVersionSelector =
149167 (ModuleVersionSelector ) moduleVersionSelectorNotation ;
150168 return new ForcedDependency (
@@ -180,25 +198,33 @@ public String getVersion() {
180198 }
181199
182200 public boolean nonVersionEquals (Object o ) {
183- if (this == o ) return true ;
184- if (o == null || getClass () != o .getClass ()) return false ;
201+ if (this == o ) {
202+ return true ;
203+ }
204+ if (o == null || getClass () != o .getClass ()) {
205+ return false ;
206+ }
185207 ForcedDependency that = (ForcedDependency ) o ;
186- return Objects .equal (group , that .group ) && Objects .equal (name , that .name );
208+ return Objects .equals (group , that .group ) && Objects .equals (name , that .name );
187209 }
188210
189211 @ Override
190212 public boolean equals (Object o ) {
191- if (this == o ) return true ;
192- if (o == null || getClass () != o .getClass ()) return false ;
213+ if (this == o ) {
214+ return true ;
215+ }
216+ if (o == null || getClass () != o .getClass ()) {
217+ return false ;
218+ }
193219 ForcedDependency that = (ForcedDependency ) o ;
194- return Objects .equal (group , that .group )
195- && Objects .equal (name , that .name )
196- && Objects .equal (version , that .version );
220+ return Objects .equals (group , that .group )
221+ && Objects .equals (name , that .name )
222+ && Objects .equals (version , that .version );
197223 }
198224
199225 @ Override
200226 public int hashCode () {
201- return Objects .hashCode (group , name , version );
227+ return Objects .hash (group , name , version );
202228 }
203229 }
204230}
0 commit comments