From 7369e8740e0560a626cdb41eeb5929f3a43dfad5 Mon Sep 17 00:00:00 2001 From: Puneet Behl Date: Tue, 11 Jul 2023 07:02:18 +0530 Subject: [PATCH 1/2] Improvements to ViewJson and ViewMarkup feature --- .../forge/feature/view/GrailsViews.java | 62 +++++++++++++++++++ .../forge/feature/view/ViewFeature.java | 38 ------------ .../forge/feature/view/json/ViewJson.java | 37 ++--------- .../forge/feature/view/markup/ViewMarkup.java | 35 +---------- .../feature/view/json/ViewMarkupSpec.groovy | 20 ++---- 5 files changed, 74 insertions(+), 118 deletions(-) create mode 100644 grails-forge-core/src/main/java/org/grails/forge/feature/view/GrailsViews.java delete mode 100644 grails-forge-core/src/main/java/org/grails/forge/feature/view/ViewFeature.java diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/view/GrailsViews.java b/grails-forge-core/src/main/java/org/grails/forge/feature/view/GrailsViews.java new file mode 100644 index 00000000..ad6d1e86 --- /dev/null +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/view/GrailsViews.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017-2020 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.grails.forge.feature.view; + +import org.grails.forge.application.ApplicationType; +import org.grails.forge.feature.Category; +import org.grails.forge.feature.FeatureContext; +import org.grails.forge.feature.OneOfFeature; +import org.grails.forge.feature.web.GrailsWeb; + +public abstract class GrailsViews implements OneOfFeature { + + public GrailsWeb grailsWeb; + + public GrailsViews(GrailsWeb grailsWeb) { + this.grailsWeb = grailsWeb; + } + + @Override + public Class getFeatureClass() { + return GrailsViews.class; + } + + @Override + public boolean supports(ApplicationType applicationType) { + return true; + } + + @Override + public String getCategory() { + return Category.VIEW; + } + + @Override + public String getDocumentation() { + return "https://views.grails.org/"; + } + + public String getViewFolderPath() { + return "grails-app/views/"; + } + + @Override + public void processSelectedFeatures(FeatureContext featureContext) { + if (!featureContext.isPresent(GrailsWeb.class) && grailsWeb != null) { + featureContext.addFeature(grailsWeb); + } + } +} diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/view/ViewFeature.java b/grails-forge-core/src/main/java/org/grails/forge/feature/view/ViewFeature.java deleted file mode 100644 index 0be57a94..00000000 --- a/grails-forge-core/src/main/java/org/grails/forge/feature/view/ViewFeature.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2017-2020 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.grails.forge.feature.view; - -import org.grails.forge.application.ApplicationType; -import org.grails.forge.feature.Category; -import org.grails.forge.feature.OneOfFeature; - -public interface ViewFeature extends OneOfFeature { - - @Override - default Class getFeatureClass() { - return ViewFeature.class; - } - - @Override - default boolean supports(ApplicationType applicationType) { - return applicationType == ApplicationType.WEB; - } - - @Override - default String getCategory() { - return Category.VIEW; - } -} diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/view/json/ViewJson.java b/grails-forge-core/src/main/java/org/grails/forge/feature/view/json/ViewJson.java index 4dbc2122..1c625a0d 100644 --- a/grails-forge-core/src/main/java/org/grails/forge/feature/view/json/ViewJson.java +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/view/json/ViewJson.java @@ -21,10 +21,9 @@ import org.grails.forge.application.generator.GeneratorContext; import org.grails.forge.build.dependencies.Dependency; import org.grails.forge.build.gradle.GradlePlugin; -import org.grails.forge.feature.Category; import org.grails.forge.feature.DefaultFeature; import org.grails.forge.feature.Feature; -import org.grails.forge.feature.FeatureContext; +import org.grails.forge.feature.view.GrailsViews; import org.grails.forge.feature.view.json.templates.*; import org.grails.forge.feature.web.GrailsWeb; import org.grails.forge.options.Options; @@ -35,12 +34,10 @@ import java.util.Set; @Singleton -public class ViewJson implements DefaultFeature { - - private final GrailsWeb grailsWeb; +public class ViewJson extends GrailsViews implements DefaultFeature { public ViewJson(GrailsWeb grailsWeb) { - this.grailsWeb = grailsWeb; + super(grailsWeb); } @Override @@ -60,13 +57,6 @@ public String getDescription() { return "JSON views are written in Groovy, end with the file extension gson and reside in the grails-app/views directory. They provide a DSL for producing output in the JSON format."; } - @Override - public void processSelectedFeatures(FeatureContext featureContext) { - if (!featureContext.isPresent(GrailsWeb.class) && grailsWeb != null) { - featureContext.addFeature(grailsWeb); - } - } - @Override public void apply(GeneratorContext generatorContext) { final Map config = generatorContext.getConfiguration(); @@ -110,27 +100,8 @@ public void apply(GeneratorContext generatorContext) { generatorContext.addTemplate("notFound_gson", new RockerTemplate(getViewFolderPath() + "notFound.gson", notFound.template())); } - @Override - public boolean supports(ApplicationType applicationType) { - return true; - } - - @Override - public String getCategory() { - return Category.VIEW; - } - - @Override - public String getDocumentation() { - return "https://views.grails.org/"; - } - @Override public boolean shouldApply(ApplicationType applicationType, Options options, Set selectedFeatures) { - return applicationType == ApplicationType.REST_API; - } - - protected String getViewFolderPath() { - return "grails-app/views/"; + return applicationType == ApplicationType.REST_API && selectedFeatures.stream().noneMatch(f -> f instanceof GrailsViews); } } diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/view/markup/ViewMarkup.java b/grails-forge-core/src/main/java/org/grails/forge/feature/view/markup/ViewMarkup.java index ac8de31e..0b35810f 100644 --- a/grails-forge-core/src/main/java/org/grails/forge/feature/view/markup/ViewMarkup.java +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/view/markup/ViewMarkup.java @@ -17,13 +17,11 @@ import io.micronaut.core.annotation.NonNull; import jakarta.inject.Singleton; -import org.grails.forge.application.ApplicationType; import org.grails.forge.application.generator.GeneratorContext; import org.grails.forge.build.dependencies.Dependency; import org.grails.forge.build.gradle.GradlePlugin; -import org.grails.forge.feature.Category; import org.grails.forge.feature.Feature; -import org.grails.forge.feature.FeatureContext; +import org.grails.forge.feature.view.GrailsViews; import org.grails.forge.feature.view.markup.templates.*; import org.grails.forge.feature.web.GrailsWeb; import org.grails.forge.template.RockerTemplate; @@ -32,12 +30,10 @@ import java.util.Map; @Singleton -public class ViewMarkup implements Feature { - - private final GrailsWeb grailsWeb; +public class ViewMarkup extends GrailsViews implements Feature { public ViewMarkup(GrailsWeb grailsWeb) { - this.grailsWeb = grailsWeb; + super(grailsWeb); } @Override @@ -57,13 +53,6 @@ public String getDescription() { return "Markup views are written in Groovy, end with the file extension gml and reside in the grails-app/views directory. They provide a DSL for producing output in the XML."; } - @Override - public void processSelectedFeatures(FeatureContext featureContext) { - if (!featureContext.isPresent(GrailsWeb.class) && grailsWeb != null) { - featureContext.addFeature(grailsWeb); - } - } - @Override public void apply(GeneratorContext generatorContext) { final Map config = generatorContext.getConfiguration(); @@ -98,22 +87,4 @@ public void apply(GeneratorContext generatorContext) { generatorContext.addTemplate("notFound_gml", new RockerTemplate(getViewFolderPath() + "notFound.gml", notFound.template())); } - @Override - public boolean supports(ApplicationType applicationType) { - return true; - } - - @Override - public String getCategory() { - return Category.VIEW; - } - - @Override - public String getDocumentation() { - return "https://views.grails.org/"; - } - - protected String getViewFolderPath() { - return "grails-app/views/"; - } } diff --git a/grails-forge-core/src/test/groovy/org/grails/forge/feature/view/json/ViewMarkupSpec.groovy b/grails-forge-core/src/test/groovy/org/grails/forge/feature/view/json/ViewMarkupSpec.groovy index 2ffa7e9b..2f1aa328 100644 --- a/grails-forge-core/src/test/groovy/org/grails/forge/feature/view/json/ViewMarkupSpec.groovy +++ b/grails-forge-core/src/test/groovy/org/grails/forge/feature/view/json/ViewMarkupSpec.groovy @@ -17,6 +17,7 @@ class ViewMarkupSpec extends ApplicationContextSpec implements CommandOutputFixt then: features.contains("grails-web") features.contains("views-markup") + !features.contains("views-json") } void "test dependencies are present for Gradle"() { @@ -29,6 +30,8 @@ class ViewMarkupSpec extends ApplicationContextSpec implements CommandOutputFixt template.contains("id \"org.grails.grails-web\"") template.contains("id \"org.grails.plugins.views-markup\"") template.contains("implementation(\"org.grails.plugins:views-markup\"") + !template.contains("id \"org.grails.plugins.views-json\"") + !template.contains("id \"org.grails.plugins.views-json-testing-support\"") } void "test default gml views are present"() { @@ -53,24 +56,11 @@ class ViewMarkupSpec extends ApplicationContextSpec implements CommandOutputFixt build.contains("id \"org.grails.grails-web\"") build.contains("id \"org.grails.plugins.views-markup\"") build.contains("implementation(\"org.grails.plugins:views-markup\"") - - where: - applicationType << [ApplicationType.REST_API] - } - - @Unroll - void "test views-markup gradle plugins and dependencies are NOT present for #applicationType application"() { - when: - final def output = generate(applicationType, new Options(Language.GROOVY, TestFramework.SPOCK, BuildTool.GRADLE, JdkVersion.JDK_11), ["views-markup"]) - final String build = output['build.gradle'] - - then: !build.contains("id \"org.grails.plugins.views-json\"") !build.contains("implementation(\"org.grails.plugins:views-json\"") - !build.contains("implementation(\"org.grails.plugins:views-json-templates\"") - !build.contains("testImplementation(\"org.grails.plugins:views-json-testing-support\"") + !build.contains("implementation(\"org.grails.plugins:views-json-testing-support\"") where: - applicationType << [ApplicationType.WEB, ApplicationType.WEB_PLUGIN, ApplicationType.PLUGIN] + applicationType << [ApplicationType.REST_API] } } From f62e9dbfe0a9213fa674660472575a2cc1f7d737 Mon Sep 17 00:00:00 2001 From: Puneet Behl Date: Tue, 11 Jul 2023 15:42:58 +0530 Subject: [PATCH 2/2] Update GradleSpec.groovy --- .../test/groovy/org/grails/forge/build/gradle/GradleSpec.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-forge-core/src/test/groovy/org/grails/forge/build/gradle/GradleSpec.groovy b/grails-forge-core/src/test/groovy/org/grails/forge/build/gradle/GradleSpec.groovy index f6619c2a..fb95ad6a 100644 --- a/grails-forge-core/src/test/groovy/org/grails/forge/build/gradle/GradleSpec.groovy +++ b/grails-forge-core/src/test/groovy/org/grails/forge/build/gradle/GradleSpec.groovy @@ -79,8 +79,8 @@ class GradleSpec extends ApplicationContextSpec implements CommandOutputFixture settingsGradle.contains("maven { url \"https://repo.grails.org/grails/core/\" }") settingsGradle.contains("gradlePluginPortal()") settingsGradle.contains("id \"org.grails.grails-web\" version \"6.0.0-RC1\"") - settingsGradle.contains("id \"org.grails.plugins.views-json\" version \"3.0.0-RC1\"") settingsGradle.contains("id \"org.grails.plugins.views-markup\" version \"3.0.0-RC1\"") + !settingsGradle.contains("id \"org.grails.plugins.views-json\" version \"3.0.0-RC1\"") !settingsGradle.contains("id \"org.grails.grails-gsp\" version \"6.0.0-RC1\"") !settingsGradle.contains("id \"com.bertramlabs.asset-pipeline\" version \"3.4.7\"") }