Skip to content

Commit

Permalink
Merge pull request #172 from grails/viewsImprov
Browse files Browse the repository at this point in the history
Improvements to ViewJson and ViewMarkup feature
  • Loading branch information
puneetbehl authored Jul 11, 2023
2 parents 626bd4e + f62e9db commit fe1bb6d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<String, Object> config = generatorContext.getConfiguration();
Expand Down Expand Up @@ -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<Feature> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<String, Object> config = generatorContext.getConfiguration();
Expand Down Expand Up @@ -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/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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\"")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Expand All @@ -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"() {
Expand All @@ -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]
}
}

0 comments on commit fe1bb6d

Please sign in to comment.