55package net .minecraftforge .gradleutils .shared ;
66
77import org .codehaus .groovy .runtime .InvokerHelper ;
8- import org .gradle .StartParameter ;
98import org .gradle .api .Plugin ;
109import org .gradle .api .Project ;
1110import org .gradle .api .file .BuildLayout ;
1211import org .gradle .api .file .DirectoryProperty ;
12+ import org .gradle .api .file .ProjectLayout ;
13+ import org .gradle .api .initialization .Settings ;
1314import org .gradle .api .invocation .Gradle ;
1415import org .gradle .api .model .ObjectFactory ;
1516import org .gradle .api .provider .Provider ;
@@ -37,6 +38,13 @@ public abstract class EnhancedPlugin<T> implements Plugin<T>, EnhancedPluginAddi
3738 /// Service Injection</a>
3839 protected abstract @ Inject ObjectFactory getObjects ();
3940
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+
4048 /// The build layout provided by Gradle services.
4149 ///
4250 /// @return The build layout
@@ -116,16 +124,16 @@ public final DirectoryProperty globalCaches() {
116124
117125 private DirectoryProperty makeGlobalCaches () {
118126 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 ());
121129
122130 return this .getObjects ().directoryProperty ().convention (
123131 gradleUserHomeDir .dir ("caches/minecraftforge/" + this .name ).map (this .problemsInternal .ensureFileLocation ())
124132 );
125133 } catch (Exception e ) {
126134 throw this .problemsInternal .illegalPluginTarget (
127135 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"
129137 );
130138 }
131139 }
@@ -141,11 +149,11 @@ private DirectoryProperty makeLocalCaches() {
141149 try {
142150 DirectoryProperty workingProjectBuildDir ;
143151 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" ));
145155 } 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)" );
149157 }
150158
151159 return this .getObjects ().directoryProperty ().convention (
@@ -154,7 +162,32 @@ private DirectoryProperty makeLocalCaches() {
154162 } catch (Exception e ) {
155163 throw this .problemsInternal .illegalPluginTarget (
156164 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"
158191 );
159192 }
160193 }
0 commit comments