Skip to content

Commit

Permalink
fix: clean up Application.groovy imports (#501)
Browse files Browse the repository at this point in the history
- Replace star import for `PluginSource` with an explicit import.
- Restrict `PluginSource` import to plugin projects only.
- Always add `@CompileStatic`.
  • Loading branch information
matrei authored Jan 15, 2025
1 parent 8fc56f4 commit 493f64d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ package @project.getPackageName()

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
@if(applicationType.equals(ApplicationType.PLUGIN) || applicationType.equals(ApplicationType.WEB_PLUGIN)) {
import grails.plugins.metadata.PluginSource
}
import groovy.transform.CompileStatic
import grails.plugins.metadata.*

@if(applicationType.equals(ApplicationType.WEB) || applicationType.equals(ApplicationType.REST_API)) {
@@CompileStatic
}
@if(applicationType.equals(ApplicationType.PLUGIN) || applicationType.equals(ApplicationType.WEB_PLUGIN)) {
@@PluginSource
}
@@CompileStatic
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.grails.forge.create

import org.grails.forge.application.ApplicationType
import org.grails.forge.application.OperatingSystem
import org.grails.forge.utils.CommandSpec

Expand Down Expand Up @@ -38,6 +39,55 @@ class CreateAppSpec extends CommandSpec {
new File(dir, "grails-app/i18n").exists()
}

void "test create-app creates a correct Application.groovy"() {
given:
generateProject(OperatingSystem.MACOS_ARCH64, [], ApplicationType.DEFAULT_OPTION)
def applicationClassSourceFile = new File(dir, 'grails-app/init/example/grails/Application.groovy')

expect:
applicationClassSourceFile.exists()
applicationClassSourceFile.text == '''\
package example.grails
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import groovy.transform.CompileStatic
@CompileStatic
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
'''.stripIndent(8)
}

void "test create-app web-plugin creates a correct Application.groovy"() {
given:
generateProject(OperatingSystem.MACOS_ARCH64, [], ApplicationType.WEB_PLUGIN)

def applicationClassSourceFile = new File(dir, 'grails-app/init/example/grails/Application.groovy')

expect:
applicationClassSourceFile.exists()
applicationClassSourceFile.text == '''\
package example.grails
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import grails.plugins.metadata.PluginSource
import groovy.transform.CompileStatic
@PluginSource
@CompileStatic
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
'''.stripIndent(8)
}

@Override
String getTempDirectoryPrefix() {
return "test-app"
Expand Down

0 comments on commit 493f64d

Please sign in to comment.