5
5
package net .minecraftforge .gradleutils .shared ;
6
6
7
7
import org .codehaus .groovy .runtime .InvokerHelper ;
8
- import org .gradle .StartParameter ;
9
8
import org .gradle .api .Plugin ;
10
9
import org .gradle .api .Project ;
11
10
import org .gradle .api .file .BuildLayout ;
12
11
import org .gradle .api .file .DirectoryProperty ;
12
+ import org .gradle .api .file .ProjectLayout ;
13
+ import org .gradle .api .initialization .Settings ;
13
14
import org .gradle .api .invocation .Gradle ;
14
15
import org .gradle .api .model .ObjectFactory ;
15
16
import org .gradle .api .provider .Provider ;
@@ -37,6 +38,13 @@ public abstract class EnhancedPlugin<T> implements Plugin<T>, EnhancedPluginAddi
37
38
/// Service Injection</a>
38
39
protected abstract @ Inject ObjectFactory getObjects ();
39
40
41
+ /// The project layout provided by Gradle services.
42
+ ///
43
+ /// @return The build layout
44
+ /// @see <a href="https://docs.gradle.org/current/userguide/service_injection.html#buildlayout">BuildLayout
45
+ /// Service Injection</a>
46
+ protected abstract @ Inject ProjectLayout getProjectLayout ();
47
+
40
48
/// The build layout provided by Gradle services.
41
49
///
42
50
/// @return The build layout
@@ -116,16 +124,16 @@ public final DirectoryProperty globalCaches() {
116
124
117
125
private DirectoryProperty makeGlobalCaches () {
118
126
try {
119
- StartParameter startParameter = ((Gradle ) InvokerHelper .getPropertySafe (this .target , "gradle" )). getStartParameter ( );
120
- DirectoryProperty gradleUserHomeDir = this .getObjects ().directoryProperty ().fileValue (startParameter .getGradleUserHomeDir ());
127
+ Gradle gradle = ((Gradle ) InvokerHelper .getProperty (this .target , "gradle" ));
128
+ DirectoryProperty gradleUserHomeDir = this .getObjects ().directoryProperty ().fileValue (gradle .getGradleUserHomeDir ());
121
129
122
130
return this .getObjects ().directoryProperty ().convention (
123
131
gradleUserHomeDir .dir ("caches/minecraftforge/" + this .name ).map (this .problemsInternal .ensureFileLocation ())
124
132
);
125
133
} catch (Exception e ) {
126
134
throw this .problemsInternal .illegalPluginTarget (
127
135
new IllegalArgumentException (String .format ("Failed to get %s global caches directory for target: %s" , this .displayName , this .target ), e ),
128
- "types with access to Gradle (#getGradle()Lorg/gradle/api/invocation/Gradle), such as projects or settings. "
136
+ "projects or settings"
129
137
);
130
138
}
131
139
}
@@ -141,11 +149,11 @@ private DirectoryProperty makeLocalCaches() {
141
149
try {
142
150
DirectoryProperty workingProjectBuildDir ;
143
151
if (this .target instanceof Project ) {
144
- workingProjectBuildDir = ((Project ) this .target ).getLayout ().getBuildDirectory ();
152
+ workingProjectBuildDir = this .getProjectLayout ().getBuildDirectory ();
153
+ } else if (this .target instanceof Settings ) {
154
+ workingProjectBuildDir = this .getObjects ().directoryProperty ().fileValue (new File (this .getBuildLayout ().getRootDirectory ().getAsFile (), "build" ));
145
155
} else {
146
- StartParameter startParameter = ((Gradle ) InvokerHelper .getPropertySafe (this .target , "gradle" )).getStartParameter ();
147
- File projectDir = startParameter .getProjectDir ();
148
- workingProjectBuildDir = this .getObjects ().directoryProperty ().fileValue (new File (projectDir != null ? projectDir : this .getBuildLayout ().getRootDirectory ().getAsFile (), "build" ));
156
+ throw new IllegalStateException ("Cannot make local caches with an unsupported type (must be project or settings)" );
149
157
}
150
158
151
159
return this .getObjects ().directoryProperty ().convention (
@@ -154,7 +162,32 @@ private DirectoryProperty makeLocalCaches() {
154
162
} catch (Exception e ) {
155
163
throw this .problemsInternal .illegalPluginTarget (
156
164
new IllegalArgumentException (String .format ("Failed to get %s local caches directory for target: %s" , this .displayName , this .getTarget ()), e ),
157
- "projects or types with access to Gradle (#getGradle()Lorg/gradle/api/invocation/Gradle), such as settings."
165
+ "projects or settings"
166
+ );
167
+ }
168
+ }
169
+
170
+ private final Lazy <DirectoryProperty > workingProjectDirectory = Lazy .simple (this ::makeWorkingProjectDirectory );
171
+
172
+ @ Override
173
+ public final DirectoryProperty workingProjectDirectory () {
174
+ return this .workingProjectDirectory .get ();
175
+ }
176
+
177
+ private DirectoryProperty makeWorkingProjectDirectory () {
178
+ try {
179
+ DirectoryProperty workingProjectDirectory = this .getObjects ().directoryProperty ();
180
+ if (this .target instanceof Project ) {
181
+ return workingProjectDirectory .value (this .getProjectLayout ().getProjectDirectory ());
182
+ } else if (this .target instanceof Settings ) {
183
+ return workingProjectDirectory .value (this .getBuildLayout ().getRootDirectory ());
184
+ } else {
185
+ throw new IllegalStateException ("Cannot get working project directory with an unsupported type (must be project or settings)" );
186
+ }
187
+ } catch (Exception e ) {
188
+ throw this .problemsInternal .illegalPluginTarget (
189
+ new IllegalArgumentException (String .format ("Failed to get %s working project directory for target: %s" , this .displayName , this .getTarget ()), e ),
190
+ "projects or settings"
158
191
);
159
192
}
160
193
}
0 commit comments