From 41ad87bae4e5f09ac6f9c053e9a1c37e9ffef6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 4 Aug 2023 03:51:25 +0100 Subject: [PATCH] Change all Project reference in extensions to FileOperations --- .../appyaml/AppEngineAppYamlExtension.java | 4 +- .../appyaml/AppEngineAppYamlPlugin.java | 9 +++- .../appyaml/StageAppYamlExtension.java | 11 +++-- .../appengine/core/DeployExtension.java | 6 +-- .../gradle/appengine/core/ToolsExtension.java | 6 +-- .../standard/AppEngineStandardExtension.java | 4 +- .../standard/AppEngineStandardPlugin.java | 9 +++- .../appengine/standard/RunExtension.java | 46 ++++++++++--------- .../standard/StageStandardExtension.java | 6 +-- .../appyaml/StageAppYamlExtensionTest.java | 14 ++++-- .../appengine/core/DeployAllTaskTest.java | 6 ++- .../appengine/core/DeployExtensionTest.java | 10 +++- 12 files changed, 81 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlExtension.java index 905961a9..059aecae 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlExtension.java @@ -22,7 +22,7 @@ import com.google.cloud.tools.gradle.appengine.core.InternalProperty; import com.google.cloud.tools.gradle.appengine.core.ToolsExtension; import org.gradle.api.Action; -import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; import org.gradle.api.plugins.ExtensionAware; public class AppEngineAppYamlExtension implements AppEngineCoreExtensionProperties { @@ -35,7 +35,7 @@ public class AppEngineAppYamlExtension implements AppEngineCoreExtensionProperti @InternalProperty private StageAppYamlExtension stage; /** Create nested configuration blocks as Extensions. */ - public void createSubExtensions(Project project) { + public void createSubExtensions(FileOperations project) { tools = ((ExtensionAware) this).getExtensions().create(TOOLS_EXT, ToolsExtension.class, project); deploy = diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlPlugin.java b/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlPlugin.java index d2a8475a..afbf997a 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlPlugin.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/AppEngineAppYamlPlugin.java @@ -26,9 +26,11 @@ import com.google.cloud.tools.gradle.appengine.core.ToolsExtension; import com.google.cloud.tools.gradle.appengine.util.GradleCompatibility; import java.io.File; +import javax.inject.Inject; import org.gradle.api.GradleException; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.WarPlugin; @@ -36,7 +38,7 @@ import org.gradle.api.tasks.bundling.War; /** Plugin definition for App Engine app.yaml based projects. */ -public class AppEngineAppYamlPlugin implements Plugin { +public abstract class AppEngineAppYamlPlugin implements Plugin { public static final String APP_ENGINE_APP_YAML_TASK_GROUP = "App Engine app.yaml based projects"; private static final String STAGE_TASK_NAME = "appengineStage"; @@ -47,12 +49,15 @@ public class AppEngineAppYamlPlugin implements Plugin { private AppEngineAppYamlExtension appengineExtension; private StageAppYamlExtension stageExtension; + @Inject + protected abstract FileOperations getFileOperations(); + @Override public void apply(Project project) { this.project = project; appengineExtension = project.getExtensions().create("appengine", AppEngineAppYamlExtension.class); - appengineExtension.createSubExtensions(project); + appengineExtension.createSubExtensions(getFileOperations()); new AppEngineCorePluginConfiguration() .configureCoreProperties( diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtension.java index bd880d6f..91b95f85 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtension.java @@ -22,8 +22,8 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import org.gradle.api.Project; import org.gradle.api.file.FileCollection; +import org.gradle.api.internal.file.FileOperations; import org.gradle.api.tasks.InputDirectory; import org.gradle.api.tasks.InputFile; import org.gradle.api.tasks.InputFiles; @@ -34,7 +34,7 @@ /** Extension element to define Stage configurations for app.yaml base projects. */ public class StageAppYamlExtension { - private final Project project; + private final FileOperations project; private File appEngineDirectory; private File dockerDirectory; @@ -42,7 +42,7 @@ public class StageAppYamlExtension { private File stagingDirectory; private List extraFilesDirectories; - public StageAppYamlExtension(Project project) { + public StageAppYamlExtension(FileOperations project) { this.project = project; } @@ -90,7 +90,7 @@ public FileCollection getExtraFilesDirectoriesAsInputFiles() { if (extraFilesDirectories == null) { return null; } - FileCollection files = project.files(); + FileCollection files = project.immutableFiles(); for (File directory : extraFilesDirectories) { files = files.plus(project.fileTree(directory)); } @@ -103,7 +103,8 @@ public List getExtraFilesDirectories() { } public void setExtraFilesDirectories(Object extraFilesDirectories) { - this.extraFilesDirectories = new ArrayList<>(project.files(extraFilesDirectories).getFiles()); + this.extraFilesDirectories = + new ArrayList<>(project.immutableFiles(extraFilesDirectories).getFiles()); } AppYamlProjectStageConfiguration toAppYamlProjectStageConfiguration() { diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/core/DeployExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/core/DeployExtension.java index b8f10459..4a55c2d1 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/core/DeployExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/core/DeployExtension.java @@ -23,7 +23,7 @@ import java.nio.file.Path; import java.util.List; import org.gradle.api.GradleException; -import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; /** Extension element to define Deployable configurations for App Engine. */ public class DeployExtension { @@ -31,7 +31,7 @@ public class DeployExtension { @InternalProperty private DeployTargetResolver deployTargetResolver; // named gradleProject to disambiguate with deploy parameter "project" - private final Project gradleProject; + private final FileOperations gradleProject; private String bucket; private String gcloudMode; @@ -44,7 +44,7 @@ public class DeployExtension { private String version; private File appEngineDirectory; - public DeployExtension(Project gradleProject) { + public DeployExtension(FileOperations gradleProject) { this.gradleProject = gradleProject; } diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/core/ToolsExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/core/ToolsExtension.java index 8060eadb..424156cf 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/core/ToolsExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/core/ToolsExtension.java @@ -18,19 +18,19 @@ package com.google.cloud.tools.gradle.appengine.core; import java.io.File; -import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; /** Extension element to define the location of cloud sdk tooling. */ public class ToolsExtension { - private final Project project; + private final FileOperations project; private File cloudSdkHome; private String cloudSdkVersion; private File cloudSdkServiceAccountFile; private String verbosity; - public ToolsExtension(Project project) { + public ToolsExtension(FileOperations project) { this.project = project; } diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardExtension.java index 661951df..24d45233 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardExtension.java @@ -22,7 +22,7 @@ import com.google.cloud.tools.gradle.appengine.core.InternalProperty; import com.google.cloud.tools.gradle.appengine.core.ToolsExtension; import org.gradle.api.Action; -import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; import org.gradle.api.plugins.ExtensionAware; public class AppEngineStandardExtension implements AppEngineCoreExtensionProperties { @@ -37,7 +37,7 @@ public class AppEngineStandardExtension implements AppEngineCoreExtensionPropert @InternalProperty private RunExtension run; /** Create nested configuration blocks as Extensions. */ - public void createSubExtensions(Project project) { + public void createSubExtensions(FileOperations project) { tools = ((ExtensionAware) this).getExtensions().create(TOOLS_EXT, ToolsExtension.class, project); deploy = diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardPlugin.java b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardPlugin.java index 2fdc629b..ec0956bf 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardPlugin.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/AppEngineStandardPlugin.java @@ -28,15 +28,17 @@ import com.google.cloud.tools.gradle.appengine.util.GradleCompatibility; import com.google.common.base.Strings; import java.io.File; +import javax.inject.Inject; import org.gradle.api.GradleException; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; import org.gradle.api.plugins.BasePlugin; import org.gradle.api.plugins.WarPlugin; import org.gradle.api.tasks.bundling.War; /** Plugin definition for App Engine standard environments. */ -public class AppEngineStandardPlugin implements Plugin { +public abstract class AppEngineStandardPlugin implements Plugin { public static final String APP_ENGINE_STANDARD_TASK_GROUP = "App Engine Standard environment"; public static final String EXPLODE_WAR_TASK_NAME = "explodeWar"; @@ -59,13 +61,16 @@ public class AppEngineStandardPlugin implements Plugin { private StageStandardExtension stageExtension; private File explodedWarDir; + @Inject + protected abstract FileOperations getFileOperations(); + @Override public void apply(Project project) { this.project = project; project.getPluginManager().apply(WarPlugin.class); appengineExtension = project.getExtensions().create("appengine", AppEngineStandardExtension.class); - appengineExtension.createSubExtensions(project); + appengineExtension.createSubExtensions(getFileOperations()); appEngineCorePluginConfiguration = new AppEngineCorePluginConfiguration(); appEngineCorePluginConfiguration.configureCoreProperties( diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/RunExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/RunExtension.java index 9926354f..03d2654b 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/RunExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/RunExtension.java @@ -29,14 +29,14 @@ import java.util.stream.Collectors; import org.gradle.api.Project; import org.gradle.api.ProjectConfigurationException; -import org.gradle.api.plugins.BasePlugin; +import org.gradle.api.internal.file.FileOperations; /** Extension element to define Run configurations for App Engine Standard Environments. */ public class RunExtension { @InternalProperty private DeployTargetResolver deployTargetResolver; - private final Project project; + private final FileOperations project; private int startSuccessTimeout; private String serverVersion; @@ -55,7 +55,7 @@ public class RunExtension { * * @param project The gradle project. */ - public RunExtension(Project project) { + public RunExtension(FileOperations project) { this.project = project; } @@ -124,7 +124,7 @@ public List getServices() { } public void setServices(Object services) { - this.services = new ArrayList<>(project.files(services).getFiles()); + this.services = new ArrayList<>(project.immutableFiles(services).getFiles()); } /** @@ -132,7 +132,8 @@ public void setServices(Object services) { * run/start to ensure {@code serviceProject} is built first. */ public File projectAsService(String serviceProject) { - return projectAsService(project.getRootProject().project(serviceProject)); + throw new UnsupportedOperationException("Has no access to current project"); + // return projectAsService(project.getRootProject().project(serviceProject)); } /** @@ -140,23 +141,24 @@ public File projectAsService(String serviceProject) { * run/start to ensure {@code serviceProject} is built first. */ public File projectAsService(Project serviceProject) { - if (!serviceProject.equals(project)) { - project.evaluationDependsOn(serviceProject.getPath()); - } - project - .getTasks() - .findByName(AppEngineStandardPlugin.RUN_TASK_NAME) - .dependsOn(serviceProject.getTasks().findByPath(BasePlugin.ASSEMBLE_TASK_NAME)); - project - .getTasks() - .findByName(AppEngineStandardPlugin.START_TASK_NAME) - .dependsOn(serviceProject.getTasks().findByPath(BasePlugin.ASSEMBLE_TASK_NAME)); - return serviceProject - .getTasks() - .findByName(AppEngineStandardPlugin.EXPLODE_WAR_TASK_NAME) - .getOutputs() - .getFiles() - .getSingleFile(); + throw new UnsupportedOperationException("Has no access to current project"); + // if (!serviceProject.equals(project)) { + // project.evaluationDependsOn(serviceProject.getPath()); + // } + // project + // .getTasks() + // .findByName(AppEngineStandardPlugin.RUN_TASK_NAME) + // .dependsOn(serviceProject.getTasks().findByPath(BasePlugin.ASSEMBLE_TASK_NAME)); + // project + // .getTasks() + // .findByName(AppEngineStandardPlugin.START_TASK_NAME) + // .dependsOn(serviceProject.getTasks().findByPath(BasePlugin.ASSEMBLE_TASK_NAME)); + // return serviceProject + // .getTasks() + // .findByName(AppEngineStandardPlugin.EXPLODE_WAR_TASK_NAME) + // .getOutputs() + // .getFiles() + // .getSingleFile(); } public Map getEnvironment() { diff --git a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/StageStandardExtension.java b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/StageStandardExtension.java index 4285c20a..e1789c5c 100644 --- a/src/main/java/com/google/cloud/tools/gradle/appengine/standard/StageStandardExtension.java +++ b/src/main/java/com/google/cloud/tools/gradle/appengine/standard/StageStandardExtension.java @@ -20,7 +20,7 @@ import com.google.cloud.tools.appengine.configuration.AppEngineWebXmlProjectStageConfiguration; import com.google.cloud.tools.gradle.appengine.util.NullSafe; import java.io.File; -import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputDirectory; import org.gradle.api.tasks.InputFile; @@ -30,7 +30,7 @@ /** Extension element to define Stage configurations for App Engine Standard Environments. */ public class StageStandardExtension { - private final Project project; + private final FileOperations project; private File sourceDirectory; private File stagingDirectory; @@ -46,7 +46,7 @@ public class StageStandardExtension { private String runtime; /** Constuctor. */ - public StageStandardExtension(Project project) { + public StageStandardExtension(FileOperations project) { this.project = project; } diff --git a/src/test/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtensionTest.java b/src/test/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtensionTest.java index 24170959..8d466a83 100644 --- a/src/test/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtensionTest.java +++ b/src/test/java/com/google/cloud/tools/gradle/appengine/appyaml/StageAppYamlExtensionTest.java @@ -27,6 +27,8 @@ import java.util.List; import java.util.stream.Collectors; import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; +import org.gradle.api.internal.project.ProjectInternal; import org.gradle.testfixtures.ProjectBuilder; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; @@ -60,7 +62,9 @@ public void setUpFiles() throws IOException { @Test public void testToAppYamlProjectStageConfiguration_allValuesSet() { - StageAppYamlExtension extension = new StageAppYamlExtension(testContextProject); + FileOperations fileOperations = + ((ProjectInternal) testContextProject).getServices().get(FileOperations.class); + StageAppYamlExtension extension = new StageAppYamlExtension(fileOperations); extension.setStagingDirectory(stagingDirectory); extension.setAppEngineDirectory(appEngineDirectory); @@ -81,7 +85,9 @@ public void testToAppYamlProjectStageConfiguration_allValuesSet() { @Test public void testToAppYamlProjectStageConfiguration_nullExtraFiles() { - StageAppYamlExtension extension = new StageAppYamlExtension(testContextProject); + FileOperations fileOperations = + ((ProjectInternal) testContextProject).getServices().get(FileOperations.class); + StageAppYamlExtension extension = new StageAppYamlExtension(fileOperations); extension.setStagingDirectory(stagingDirectory); extension.setAppEngineDirectory(appEngineDirectory); @@ -100,7 +106,9 @@ public void testToAppYamlProjectStageConfiguration_nullExtraFiles() { @Test public void testToAppYamlProjectStageConfiguration_emptyExtraFiles() { - StageAppYamlExtension extension = new StageAppYamlExtension(testContextProject); + FileOperations fileOperations = + ((ProjectInternal) testContextProject).getServices().get(FileOperations.class); + StageAppYamlExtension extension = new StageAppYamlExtension(fileOperations); extension.setStagingDirectory(stagingDirectory); extension.setAppEngineDirectory(appEngineDirectory); diff --git a/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployAllTaskTest.java b/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployAllTaskTest.java index 844724d3..f540aa84 100644 --- a/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployAllTaskTest.java +++ b/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployAllTaskTest.java @@ -30,6 +30,8 @@ import java.io.IOException; import java.nio.file.Path; import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; +import org.gradle.api.internal.project.ProjectInternal; import org.gradle.testfixtures.ProjectBuilder; import org.junit.Before; import org.junit.Rule; @@ -61,7 +63,9 @@ public class DeployAllTaskTest { @Before public void setup() throws IOException { Project tempProject = ProjectBuilder.builder().build(); - deployExtension = new DeployExtension(tempProject); + FileOperations fileOperations = + ((ProjectInternal) tempProject).getServices().get(FileOperations.class); + deployExtension = new DeployExtension(fileOperations); deployExtension.setDeployTargetResolver(deployTargetResolver); deployCapture = ArgumentCaptor.forClass(DeployConfiguration.class); stageDir = tempFolder.newFolder("staging"); diff --git a/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployExtensionTest.java b/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployExtensionTest.java index 5084dcac..45d1a2a8 100644 --- a/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployExtensionTest.java +++ b/src/test/java/com/google/cloud/tools/gradle/appengine/core/DeployExtensionTest.java @@ -22,6 +22,8 @@ import java.nio.file.Paths; import java.util.List; import org.gradle.api.Project; +import org.gradle.api.internal.file.FileOperations; +import org.gradle.api.internal.project.ProjectInternal; import org.gradle.testfixtures.ProjectBuilder; import org.junit.Assert; import org.junit.Before; @@ -51,7 +53,9 @@ public void setUp() { @Test public void testToDeployConfiguration_allValuesSet() { - DeployExtension testExtension = new DeployExtension(testProject); + FileOperations fileOperations = + ((ProjectInternal) testProject).getServices().get(FileOperations.class); + DeployExtension testExtension = new DeployExtension(fileOperations); testExtension.setDeployTargetResolver(deployTargetResolver); testExtension.setBucket("test-bucket"); @@ -83,7 +87,9 @@ public void testToDeployConfiguration_allValuesSet() { @Test public void testToDeployConfiguration_onlyRequiredValuesSet() { - DeployExtension testExtension = new DeployExtension(testProject); + FileOperations fileOperations = + ((ProjectInternal) testProject).getServices().get(FileOperations.class); + DeployExtension testExtension = new DeployExtension(fileOperations); testExtension.setDeployTargetResolver(deployTargetResolver); testExtension.setProjectId("test-project-id");