Skip to content

Commit

Permalink
Generator: generate operation can be revoked
Browse files Browse the repository at this point in the history
Closes gh-802
  • Loading branch information
rainboyan committed Dec 16, 2024
1 parent 324ffb3 commit 28dc4d4
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class AbstractGenerator implements Generator {
SpringIOUtils.copy(templateRenderer.template(getTemplateRoot(), source), file(destination))
}

void removeFile(String destination) {
this.console.addStatus('remove '.padLeft(13), destination, 'YELLOW')
File file = new File(getBaseDir(), destination)
if (file.exists()) {
file.delete()
}
}

File file(Object path) {
if (path instanceof File) {
return (File) path
Expand Down
4 changes: 4 additions & 0 deletions grace-cli/src/main/groovy/grails/cli/generator/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ default boolean help(GenerationContext generationContext) {
return true;
}

default boolean revoke(GenerationContext generationContext) {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,31 @@ class ControllerGenerator extends AbstractGenerator {
true
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
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'
removeFile(controllerFile)
removeFile(controllerSpecFile)

String gspViewDir = 'app/views/' + propertyName
new File(gspViewDir).list().each {
removeFile(gspViewDir + '/' + it)
}

true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,25 @@ class DomainGenerator extends AbstractGenerator {
true
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
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'
removeFile(domainClassFile)
removeFile(domainClassSpecFile)

true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,26 @@ class InterceptorGenerator extends AbstractGenerator {
true
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
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'

removeFile(interceptorClassFile)
removeFile(interceptorClassSpecFile)

true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,25 @@ class ServiceGenerator extends AbstractGenerator {
true
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
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'
removeFile(serviceClassFile)
removeFile(serviceClassSpecFile)

true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ class TaglibGenerator extends AbstractGenerator {
true
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
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'
removeFile(taglibClassFile)
removeFile(taglibClassSpecFile)

true
}

}

0 comments on commit 28dc4d4

Please sign in to comment.