66
77import net .minecraftforge .gradleutils .shared .Closures ;
88import org .gradle .api .Action ;
9- import org .gradle .api .InvalidUserDataException ;
10- import org .gradle .api .NamedDomainObjectProvider ;
119import org .gradle .api .Project ;
10+ import org .gradle .api .artifacts .Configuration ;
1211import org .gradle .api .artifacts .Dependency ;
13- import org .gradle .api .artifacts .DependencyScopeConfiguration ;
14- import org .gradle .api .artifacts .MinimalExternalModuleDependency ;
12+ import org .gradle .api .artifacts .DependencySubstitutions ;
1513import org .gradle .api .artifacts .ModuleVersionSelector ;
1614import org .gradle .api .artifacts .ProjectDependency ;
15+ import org .gradle .api .artifacts .component .ComponentSelector ;
1716import org .gradle .api .artifacts .dsl .DependencyHandler ;
1817import org .gradle .api .artifacts .type .ArtifactTypeDefinition ;
1918import org .gradle .api .attributes .Attribute ;
2019import org .gradle .api .attributes .AttributeContainer ;
21- import org .gradle .api .attributes .HasConfigurableAttributes ;
2220import org .gradle .api .file .ProjectLayout ;
2321import org .gradle .api .file .RegularFile ;
2422import org .gradle .api .file .RegularFileProperty ;
2523import org .gradle .api .model .ObjectFactory ;
2624import org .gradle .api .plugins .ExtensionAware ;
27- import org .gradle .api .plugins .JavaPluginExtension ;
2825import org .gradle .api .provider .ProviderFactory ;
2926import org .jetbrains .annotations .Nullable ;
3027
3330import java .io .IOException ;
3431import java .util .ArrayList ;
3532import java .util .List ;
33+ import java .util .function .Function ;
34+ import java .util .stream .Collectors ;
3635
3736abstract class AccessTransformersExtensionImpl implements AccessTransformersExtensionInternal {
3837 private final Project project ;
3938
4039 private final AccessTransformersPlugin plugin ;
4140 private final AccessTransformersProblems problems = this .getObjects ().newInstance (AccessTransformersProblems .class );
4241
43- private final NamedDomainObjectProvider <DependencyScopeConfiguration > constraintsConfiguration ;
44-
4542 private @ Nullable AccessTransformersContainer container ;
4643
4744 protected abstract @ Inject ObjectFactory getObjects ();
@@ -56,56 +53,83 @@ public AccessTransformersExtensionImpl(AccessTransformersPlugin plugin, Project
5653 this .plugin = plugin ;
5754
5855 var configurations = project .getConfigurations ();
59- {
60- NamedDomainObjectProvider <DependencyScopeConfiguration > c ;
61- try {
62- c = configurations .dependencyScope ("accessTransformersDependencyConstraints" , configuration -> {
63- configuration .setDescription ("Dependency constraints to enforce access transformer usage." );
64- });
65- } catch (InvalidUserDataException e ) {
66- c = configurations .named ("accessTransformerDependencyConstraints" , DependencyScopeConfiguration .class );
56+ configurations .configureEach (configuration -> configuration .withDependencies (dependencies -> {
57+ var constraints = configurations
58+ .stream ()
59+ .flatMap (c -> c .getDependencyConstraints ().matching (it ->
60+ ((ExtensionAware ) it ).getExtensions ().getExtraProperties ().has ("__accessTransformers_configs" )
61+ ).stream ())
62+ .collect (Collectors .toMap (ModuleVersionSelector ::getModule , Function .identity ()));
63+
64+ for (var c : configurations ) {
65+ for (var dependency : c .getDependencies ().matching (it ->
66+ ((ExtensionAware ) it ).getExtensions ().getExtraProperties ().has ("__accessTransformers_configs" )
67+ )) {
68+ var configs = (List <AccessTransformersConfigurationImpl >) ((ExtensionAware ) dependency ).getExtensions ().getExtraProperties ().get ("__accessTransformers_configs" );
69+ var attributes = new ArrayList <Attribute <Boolean >>(configs .size ());
70+ for (var config : configs ) {
71+ attributes .add (this .register (dependency , config ));
72+ }
73+
74+ if (dependency instanceof ModuleVersionSelector m ) {
75+ var constraint = constraints .remove (m .getModule ());
76+ if (constraint != null ) {
77+ var constraintConfigs = (List <AccessTransformersConfigurationImpl >) ((ExtensionAware ) constraint ).getExtensions ().getExtraProperties ().get ("__accessTransformers_configs" );
78+ for (var config : constraintConfigs ) {
79+ attributes .add (this .register (constraint , config ));
80+ }
81+ }
82+ }
83+
84+ Function <DependencySubstitutions , ComponentSelector > componentSelector ;
85+ if (dependency instanceof ProjectDependency p ) {
86+ var path = p .getPath ();
87+ componentSelector = s -> s .project (path );
88+ } else if (dependency instanceof ModuleVersionSelector m ) {
89+ var module = "%s%s" .formatted (m .getModule ().toString (), m .getVersion () != null ? ":" + m .getVersion () : "" );
90+ componentSelector = s -> s .module (module );
91+ } else {
92+ this .problems .reportIllegalTargetDependency (dependency );
93+ return ;
94+ }
95+
96+ this .apply (configuration , componentSelector , attributes );
97+ }
6798 }
68- this .constraintsConfiguration = c ;
69- }
7099
71- // FUCK
72- project .getGradle ().projectsEvaluated (gradle -> this .finish (project ));
73- }
74-
75- @ SuppressWarnings ({"unchecked" , "DataFlowIssue" })
76- private void finish (Project project ) {
77- project .getExtensions ().getByType (JavaPluginExtension .class ).getSourceSets ().forEach (sourceSet ->
78- Util .forEachClasspath (project .getConfigurations (), sourceSet , c -> c .extendsFrom (this .constraintsConfiguration .get ()))
79- );
80-
81- project .getConfigurations ().forEach (c -> c .getDependencies ().matching (it ->
82- ((ExtensionAware ) it ).getExtensions ().getExtraProperties ().has ("__accessTransformers_configs" )
83- ).forEach (dependency -> {
84- Object dependencyNotation = dependency ;
85- if (!(dependencyNotation instanceof MinimalExternalModuleDependency )
86- && !(dependencyNotation instanceof ProjectDependency )
87- && dependencyNotation instanceof ModuleVersionSelector module ) {
88- dependencyNotation = module .getModule ().toString ();
89- }
100+ for (var constraint : constraints .values ()) {
101+ var configs = (List <AccessTransformersConfigurationImpl >) ((ExtensionAware ) constraint ).getExtensions ().getExtraProperties ().get ("__accessTransformers_configs" );
102+ var attributes = new ArrayList <Attribute <Boolean >>(configs .size ());
103+ for (var config : configs ) {
104+ attributes .add (this .register (constraint , config ));
105+ }
90106
91- var configs = (List <AccessTransformersConfigurationImpl >) ((ExtensionAware ) dependency ).getExtensions ().getExtraProperties ().get ("__accessTransformers_configs" );
92- var attributes = new ArrayList <Attribute <Boolean >>(configs .size ());
93- for (var i = 0 ; i < configs .size (); i ++) {
94- var config = configs .get (i );
95- attributes .add (this .register (i , dependency , config ));
107+ var module = "%s%s" .formatted (constraint .getModule ().toString (), constraint .getVersion () != null ? ":" + constraint .getVersion () : "" );
108+ this .apply (configuration , s -> s .module (module ), attributes );
96109 }
97-
98- this .constraintsConfiguration .get ().getDependencyConstraints ().add (this .project .getDependencies ().getConstraints ().create (dependencyNotation , constraint ->
99- constraint .attributes (a -> {
100- for (var attribute : attributes ) a .attribute (attribute , true );
101- })
102- ));
103110 }));
104111 }
105112
106- private Attribute <Boolean > register (int index , Dependency dependency , AccessTransformersConfigurationImpl config ) {
113+ private void apply (Configuration configuration , Function <DependencySubstitutions , ComponentSelector > componentSelector , Iterable <Attribute <Boolean >> attributes ) {
114+ configuration .getResolutionStrategy ().dependencySubstitution (s -> {
115+ var component = componentSelector .apply (s );
116+ s .substitute (component ).using (s .variant (component , variant -> {
117+ variant .attributes (a -> {
118+ for (var attribute : attributes ) {
119+ a .attribute (attribute , true );
120+ }
121+ });
122+ }));
123+ });
124+ }
125+
126+ private Attribute <Boolean > register (Object dependency , AccessTransformersConfigurationImpl config ) {
107127 this .validateATFile (dependency , config .getConfig ());
108128
129+ var ext = this .project .getGradle ().getExtensions ().getExtraProperties ();
130+ int index = ext .has ("__accessTransformers_automatic_index" ) ? (int ) ext .get ("__accessTransformers_automatic_index" ) + 1 : 0 ;
131+ this .project .getGradle ().getExtensions ().getExtraProperties ().set ("__accessTransformers_automatic_index" , index );
132+
109133 var attribute = Attribute .of ("net.minecraftforge.accesstransformers.automatic." + index , Boolean .class );
110134 this .project .dependencies (Closures .<DependencyHandler >consumer (this , dependencies -> {
111135 dependencies .attributesSchema (attributesSchema -> {
@@ -145,39 +169,41 @@ private static void setAttributes(AttributeContainer attributes, Attribute<Boole
145169 .attribute (attribute , value );
146170 }
147171
148- private void validateATFile (Dependency dependency , RegularFileProperty atFileProperty ) {
172+ private void validateATFile (Object dependency , RegularFileProperty atFileProperty ) {
173+ var dependencyToString = dependency instanceof Dependency d ? Util .toString (d ) : dependency .toString ();
174+
149175 // check that consumer has defined the config
150176 RegularFile atFileSource ;
151177 try {
152178 atFileSource = atFileProperty .get ();
153179 } catch (IllegalStateException e ) {
154- throw this .problems .accessTransformerConfigNotDefined (new RuntimeException ("Failed to resolve config file property" , e ), dependency );
180+ throw this .problems .accessTransformerConfigNotDefined (new RuntimeException ("Failed to resolve config file property" , e ), dependencyToString );
155181 }
156182
157183 // check that the file exists
158184 var atFile = atFileSource .getAsFile ();
159185 var atFilePath = this .getLayout ().getProjectDirectory ().getAsFile ().toPath ().relativize (atFile .toPath ()).toString ();
160186 if (!atFile .exists ())
161- throw this .problems .accessTransformerConfigMissing (new RuntimeException (new FileNotFoundException ("Config file does not exist at " + atFilePath )), dependency , atFilePath );
187+ throw this .problems .accessTransformerConfigMissing (new RuntimeException (new FileNotFoundException ("Config file does not exist at " + atFilePath )), dependencyToString , atFilePath );
162188
163189 // check that the file can be read and isn't empty
164190 String atFileContents ;
165191 try {
166192 atFileContents = this .getProviders ().fileContents (atFileSource ).getAsText ().get ();
167193 } catch (Throwable e ) {
168- throw this .problems .accessTransformerConfigUnreadable (new RuntimeException (new IOException ("Failed to read config file at " + atFilePath , e )), dependency , atFilePath );
194+ throw this .problems .accessTransformerConfigUnreadable (new RuntimeException (new IOException ("Failed to read config file at " + atFilePath , e )), dependencyToString , atFilePath );
169195 }
170196 if (atFileContents .isBlank ())
171- throw this .problems .accessTransformerConfigEmpty (new IllegalStateException ("Config file must not be blank at " + atFilePath ), dependency , atFilePath );
197+ throw this .problems .accessTransformerConfigEmpty (new IllegalStateException ("Config file must not be blank at " + atFilePath ), dependencyToString , atFilePath );
172198 }
173199
174200 @ Override
175201 public AccessTransformersContainer register (Action <? super AccessTransformersContainer .Options > options ) {
176- return this .container = AccessTransformersContainer .register (this .project , options );
202+ return this .container = AccessTransformersContainerInternal .register (this .project , options );
177203 }
178204
179205 private AccessTransformersContainer getContainer () {
180- return this .container == null ? this .container = AccessTransformersContainer .register (this .project , it -> { }) : this .container ;
206+ return this .container == null ? this .container = AccessTransformersContainerInternal .register (this .project , it -> { }) : this .container ;
181207 }
182208
183209 @ Override
@@ -191,7 +217,7 @@ public void options(Action<? super Options> action) {
191217 }
192218
193219 @ Override
194- public void configure (Dependency dependency , Action <? super AccessTransformersConfiguration > action ) {
220+ public void configure (Object dependency , Action <? super AccessTransformersConfiguration > action ) {
195221 this .getContainer ().configure (dependency , action );
196222 }
197223}
0 commit comments