Skip to content

Commit

Permalink
Refactor AbstractGenerator
Browse files Browse the repository at this point in the history
Add methods `getDefaultPackageName()` and `getDefaultPackagePath()`

Closes gh-806
  • Loading branch information
rainboyan committed Dec 17, 2024
1 parent 6b9237b commit 07128e9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class AbstractGenerator implements Generator {
this.fileSystemInteraction
}

protected String getDefaultPackageName() {
loadApplicationConfig().getProperty('grails.codegen.defaultPackage')
}

protected String getDefaultPackagePath() {
getDefaultPackageName()?.replace('.', '/')
}

protected void createFile(String source, String destination, Map<String, Object> model, boolean overwrite) {
this.templateRenderer.render(this.templateRenderer.template(getTemplateRoot(), source),
fileSystemInteraction.file(destination), model, overwrite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import org.grails.config.CodeGenConfig

/**
* @author Michael Yan
Expand All @@ -35,21 +34,18 @@ class ControllerGenerator extends AbstractGenerator {
}

boolean overwrite = commandLine.hasOption('force') || commandLine.hasOption('f')
CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String propertyName = className.uncapitalize()
String[] actionNames = args.size() >= 3 ? args[2..-1] : ['index']
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

Map<String, Object> model = new HashMap<>()
model['packageName'] = defaultPackage
model['packageName'] = defaultPackageName
model['className'] = className
model['propertyName'] = propertyName
model['actions'] = actionNames

String controllerFile = 'app/controllers/' + packagePath + '/' + className + 'Controller.groovy'
String controllerSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'ControllerSpec.groovy'
String controllerFile = 'app/controllers/' + defaultPackagePath + '/' + className + 'Controller.groovy'
String controllerSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'ControllerSpec.groovy'
createFile('Controller.groovy.tpl', controllerFile, model, overwrite)
createFile('ControllerSpec.groovy.tpl', controllerSpecFile, model, overwrite)

Expand All @@ -70,14 +66,11 @@ class ControllerGenerator extends AbstractGenerator {
return
}

CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String propertyName = className.uncapitalize()
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

String controllerFile = 'app/controllers/' + packagePath + '/' + className + 'Controller.groovy'
String controllerSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'ControllerSpec.groovy'
String controllerFile = 'app/controllers/' + defaultPackagePath + '/' + className + 'Controller.groovy'
String controllerSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'ControllerSpec.groovy'
removeFile(controllerFile)
removeFile(controllerSpecFile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import org.grails.config.CodeGenConfig

/**
* @author Michael Yan
Expand Down Expand Up @@ -53,7 +52,7 @@ class DomainGenerator extends AbstractGenerator {
}

boolean overwrite = commandLine.hasOption('force') || commandLine.hasOption('f')
CodeGenConfig config = loadApplicationConfig()

String className = args[1].capitalize()
String propertyName = className.uncapitalize()
Map<String, String> classAttributes = new LinkedHashMap<>()
Expand All @@ -62,17 +61,15 @@ class DomainGenerator extends AbstractGenerator {
String[] attr = (item.contains(':') ? item.split(':') : [item, 'String']) as String[]
classAttributes[attr[0]] = TYPES[attr[1]] ?: attr[1]
}
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

Map<String, Object> model = new HashMap<>()
model['packageName'] = defaultPackage
model['packageName'] = defaultPackageName
model['className'] = className
model['propertyName'] = propertyName
model['attributes'] = classAttributes

String domainClassFile = 'app/domain/' + packagePath + '/' + className + '.groovy'
String domainClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'Spec.groovy'
String domainClassFile = 'app/domain/' + defaultPackagePath + '/' + className + '.groovy'
String domainClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'Spec.groovy'
createFile('DomainClass.groovy.tpl', domainClassFile, model, overwrite)
createFile('DomainClassSpec.groovy.tpl', domainClassSpecFile, model, overwrite)

Expand All @@ -86,13 +83,10 @@ class DomainGenerator extends AbstractGenerator {
return
}

CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

String domainClassFile = 'app/domain/' + packagePath + '/' + className + '.groovy'
String domainClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'Spec.groovy'
String domainClassFile = 'app/domain/' + defaultPackagePath + '/' + className + '.groovy'
String domainClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'Spec.groovy'
removeFile(domainClassFile)
removeFile(domainClassSpecFile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import org.grails.config.CodeGenConfig

/**
* @author Michael Yan
Expand All @@ -35,18 +34,15 @@ class InterceptorGenerator extends AbstractGenerator {
}

boolean overwrite = commandLine.hasOption('force') || commandLine.hasOption('f')
CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String propertyName = className.uncapitalize()
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

Map<String, Object> model = new HashMap<>()
model['packageName'] = defaultPackage
model['packageName'] = defaultPackageName
model['className'] = className
model['propertyName'] = propertyName
String interceptorClassFile = 'app/controllers/' + packagePath + '/' + className + 'Interceptor.groovy'
String interceptorClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'InterceptorSpec.groovy'
String interceptorClassFile = 'app/controllers/' + defaultPackagePath + '/' + className + 'Interceptor.groovy'
String interceptorClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'InterceptorSpec.groovy'

createFile('Interceptor.groovy.tpl', interceptorClassFile, model, overwrite)
createFile('InterceptorSpec.groovy.tpl', interceptorClassSpecFile, model, overwrite)
Expand All @@ -61,13 +57,10 @@ class InterceptorGenerator extends AbstractGenerator {
return
}

CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

String interceptorClassFile = 'app/controllers/' + packagePath + '/' + className + 'Interceptor.groovy'
String interceptorClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'InterceptorSpec.groovy'
String interceptorClassFile = 'app/controllers/' + defaultPackagePath + '/' + className + 'Interceptor.groovy'
String interceptorClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'InterceptorSpec.groovy'

removeFile(interceptorClassFile)
removeFile(interceptorClassSpecFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import org.grails.config.CodeGenConfig

/**
* @author Michael Yan
Expand All @@ -35,21 +34,18 @@ class ServiceGenerator extends AbstractGenerator {
}

boolean overwrite = commandLine.hasOption('force') || commandLine.hasOption('f')
CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String propertyName = className.uncapitalize()
String[] methodNames = (args.size() >= 3 ? args[2..-1] : ['serviceMethod']) as String[]
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

Map<String, Object> model = new HashMap<>()
model['packageName'] = defaultPackage
model['packageName'] = defaultPackageName
model['className'] = className
model['propertyName'] = propertyName
model['methods'] = methodNames

String serviceClassFile = 'app/services/' + packagePath + '/' + className + 'Service.groovy'
String serviceClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'ServiceSpec.groovy'
String serviceClassFile = 'app/services/' + defaultPackagePath + '/' + className + 'Service.groovy'
String serviceClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'ServiceSpec.groovy'
createFile('Service.groovy.tpl', serviceClassFile, model, overwrite)
createFile('ServiceSpec.groovy.tpl', serviceClassSpecFile, model, overwrite)

Expand All @@ -63,13 +59,10 @@ class ServiceGenerator extends AbstractGenerator {
return
}

CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

String serviceClassFile = 'app/services/' + packagePath + '/' + className + 'Service.groovy'
String serviceClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'ServiceSpec.groovy'
String serviceClassFile = 'app/services/' + defaultPackagePath + '/' + className + 'Service.groovy'
String serviceClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'ServiceSpec.groovy'
removeFile(serviceClassFile)
removeFile(serviceClassSpecFile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import org.grails.config.CodeGenConfig

/**
* @author Michael Yan
Expand All @@ -35,21 +34,18 @@ class TaglibGenerator extends AbstractGenerator {
}

boolean overwrite = commandLine.hasOption('force') || commandLine.hasOption('f')
CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String propertyName = className.uncapitalize()
String[] tagNames = (args.size() >= 3 ? args[2..-1] : ['tag']) as String[]
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

Map<String, Object> model = new HashMap<>()
model['packageName'] = defaultPackage
model['packageName'] = defaultPackageName
model['className'] = className
model['propertyName'] = propertyName
model['tags'] = tagNames

String taglibClassFile = 'app/taglib/' + packagePath + '/' + className + 'TagLib.groovy'
String taglibClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'TagLibSpec.groovy'
String taglibClassFile = 'app/taglib/' + defaultPackagePath + '/' + className + 'TagLib.groovy'
String taglibClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'TagLibSpec.groovy'
createFile('TagLib.groovy.tpl', taglibClassFile, model, overwrite)
createFile('TagLibSpec.groovy.tpl', taglibClassSpecFile, model, overwrite)
true
Expand All @@ -62,13 +58,10 @@ class TaglibGenerator extends AbstractGenerator {
return
}

CodeGenConfig config = loadApplicationConfig()
String className = args[1].capitalize()
String defaultPackage = config.getProperty('grails.codegen.defaultPackage')
String packagePath = defaultPackage.replace('.', '/')

String taglibClassFile = 'app/taglib/' + packagePath + '/' + className + 'TagLib.groovy'
String taglibClassSpecFile = 'src/test/groovy/' + packagePath + '/' + className + 'TagLibSpec.groovy'
String taglibClassFile = 'app/taglib/' + defaultPackagePath + '/' + className + 'TagLib.groovy'
String taglibClassSpecFile = 'src/test/groovy/' + defaultPackagePath + '/' + className + 'TagLibSpec.groovy'
removeFile(taglibClassFile)
removeFile(taglibClassSpecFile)

Expand Down

0 comments on commit 07128e9

Please sign in to comment.