Skip to content

Commit

Permalink
Refactor Generator API
Browse files Browse the repository at this point in the history
* Add `void init(GenerationContext contenxt)`
* Enhance `GenerationContext`

See gh-801

Closes gh-805
  • Loading branch information
rainboyan committed Dec 17, 2024
1 parent 392d2d8 commit 6b9237b
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import grails.dev.commands.io.FileSystemInteraction
import grails.dev.commands.io.FileSystemInteractionImpl
import grails.dev.commands.template.TemplateRenderer
import grails.dev.commands.template.TemplateRendererImpl
import grails.util.BuildSettings
import org.grails.build.parsing.CommandLine
import org.grails.config.CodeGenConfig
import org.grails.io.support.DefaultResourceLoader
import org.grails.io.support.Resource
Expand All @@ -35,10 +35,25 @@ import org.grails.io.support.SpringIOUtils
*/
class AbstractGenerator implements Generator {

protected GrailsConsole console = GrailsConsole.getInstance()
protected GenerationContext generationContext
protected GrailsConsole console
protected CommandLine commandLine

protected File baseDir

protected TemplateRenderer templateRenderer
protected FileSystemInteraction fileSystemInteraction

@Override
void init(GenerationContext generationContext) {
this.generationContext = generationContext
this.console = generationContext.console
this.commandLine = generationContext.commandLine
this.baseDir = generationContext.baseDir
this.templateRenderer = new TemplateRendererImpl(this.baseDir)
this.fileSystemInteraction = new FileSystemInteractionImpl(this.baseDir, new DefaultResourceLoader())
}

protected CodeGenConfig loadApplicationConfig() {
CodeGenConfig config = new CodeGenConfig()
File applicationYml = new File(getBaseDir(), "app/conf/application.yml")
Expand All @@ -53,29 +68,23 @@ class AbstractGenerator implements Generator {
}

protected File getBaseDir() {
this.baseDir ?: BuildSettings.BASE_DIR
}

protected void setBaseDir(File baseDir) {
this.baseDir = baseDir
this.baseDir
}

protected String getTemplateRoot() {
"templates/generators/$name"
}

protected TemplateRenderer getTemplateRenderer() {
TemplateRenderer templateRenderer = new TemplateRendererImpl(getBaseDir())
templateRenderer
this.templateRenderer
}

protected FileSystemInteraction getFileSystemInteraction() {
FileSystemInteraction fileSystemInteraction = new FileSystemInteractionImpl(getBaseDir(), new DefaultResourceLoader())
fileSystemInteraction
this.fileSystemInteraction
}

protected void createFile(String source, String destination, Map<String, Object> model, boolean overwrite) {
templateRenderer.render(templateRenderer.template(getTemplateRoot(), source),
this.templateRenderer.render(this.templateRenderer.template(getTemplateRoot(), source),
fileSystemInteraction.file(destination), model, overwrite)
}

Expand All @@ -84,16 +93,16 @@ class AbstractGenerator implements Generator {
}

void template(String source, String destination, Map<String, Object> model) {
templateRenderer.render(templateRenderer.template(getTemplateRoot(), source), file(destination), model, true)
this.templateRenderer.render(this.templateRenderer.template(getTemplateRoot(), source), file(destination), model, true)
}

void template(String source, String destination, Map<String, Object> model, boolean overwrite) {
templateRenderer.render(templateRenderer.template(getTemplateRoot(), source), file(destination), model, overwrite)
this.templateRenderer.render(this.templateRenderer.template(getTemplateRoot(), source), file(destination), model, overwrite)
}

void copyFile(String source, String destination) {
this.console.addStatus('create '.padLeft(13), destination, 'GREEN')
SpringIOUtils.copy(templateRenderer.template(getTemplateRoot(), source), file(destination))
SpringIOUtils.copy(this.templateRenderer.template(getTemplateRoot(), source), file(destination))
}

void removeFile(String destination) {
Expand Down Expand Up @@ -162,7 +171,7 @@ class AbstractGenerator implements Generator {
* @return the created directory
*/
File dir(String name) {
console.addStatus('create '.padLeft(13), name, 'GREEN')
this.console.addStatus('create '.padLeft(13), name, 'GREEN')
def file = new File(getBaseDir(), name)
file.mkdirs()
file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
package grails.cli.generator;

import java.io.File;

import grails.build.logging.GrailsConsole;
import grails.util.BuildSettings;
import org.grails.build.parsing.CommandLine;

public class GenerationContext {
private CommandLine commandLine;

protected File baseDir = BuildSettings.BASE_DIR;
protected GrailsConsole console;
protected CommandLine commandLine;

public File getBaseDir() {
return this.baseDir;
}

public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
}

public GrailsConsole getConsole() {
return this.console;
}

public void setConsole(GrailsConsole console) {
this.console = console;
}

public CommandLine getCommandLine() {
return this.commandLine;
Expand Down
9 changes: 6 additions & 3 deletions grace-cli/src/main/groovy/grails/cli/generator/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ default String getDescription() {
return getName();
}

default boolean generate(GenerationContext generationContext) {
default void init(GenerationContext generationContext) {
}

default boolean generate() {
return true;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ class DestroyCommand implements ProjectCommand {
boolean handle(ExecutionContext executionContext) {
CommandLine commandLine = executionContext.commandLine
GenerationContext generationContext = new GenerationContext()
generationContext.baseDir = executionContext.baseDir
generationContext.console = executionContext.console
generationContext.commandLine = commandLine

if (commandLine.hasOption(CommandLine.HELP_ARGUMENT) || commandLine.hasOption('h')) {
if (commandLine.remainingArgs) {
String generatorName = commandLine.remainingArgs[0]
Generator generator = loadedGenerators().find { it.name == generatorName }
if (generator) {
return generator.help(generationContext)
generator.init(generationContext)
return generator.help()
}
else {
noGenerator(generatorName, executionContext.console)
Expand All @@ -81,7 +84,8 @@ class DestroyCommand implements ProjectCommand {
String generatorName = commandLine.remainingArgs[0]
Generator generator = loadedGenerators().find { it.name == generatorName }
if (generator) {
return generator.revoke(generationContext)
generator.init(generationContext)
return generator.revoke()
}
else {
noGenerator(generatorName, executionContext.console)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ class GenerateCommand implements ProjectCommand {
boolean handle(ExecutionContext executionContext) {
CommandLine commandLine = executionContext.commandLine
GenerationContext generationContext = new GenerationContext()
generationContext.baseDir = executionContext.baseDir
generationContext.console = executionContext.console
generationContext.commandLine = commandLine

if (commandLine.hasOption(CommandLine.HELP_ARGUMENT) || commandLine.hasOption('h')) {
if (commandLine.remainingArgs) {
String generatorName = commandLine.remainingArgs[0]
Generator generator = loadedGenerators().find { it.name == generatorName }
if (generator) {
return generator.help(generationContext)
generator.init(generationContext)
return generator.help()
}
else {
noGenerator(generatorName, executionContext.console)
Expand All @@ -83,7 +86,8 @@ class GenerateCommand implements ProjectCommand {
String generatorName = commandLine.remainingArgs[0]
Generator generator = loadedGenerators().find { it.name == generatorName }
if (generator) {
return generator.generate(generationContext)
generator.init(generationContext)
return generator.generate()
}
else {
noGenerator(generatorName, executionContext.console)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import grails.cli.generator.GenerationContext
import org.grails.build.parsing.CommandLine
import org.grails.config.CodeGenConfig

/**
Expand All @@ -30,8 +28,7 @@ import org.grails.config.CodeGenConfig
class ControllerGenerator extends AbstractGenerator {

@Override
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean generate() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down Expand Up @@ -67,8 +64,7 @@ class ControllerGenerator extends AbstractGenerator {
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean revoke() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import grails.cli.generator.GenerationContext
import org.grails.build.parsing.CommandLine
import org.grails.config.CodeGenConfig

/**
Expand Down Expand Up @@ -48,8 +46,7 @@ class DomainGenerator extends AbstractGenerator {
]

@Override
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean generate() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down Expand Up @@ -83,8 +80,7 @@ class DomainGenerator extends AbstractGenerator {
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean revoke() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import grails.cli.generator.GenerationContext
import org.grails.build.parsing.CommandLine
import org.grails.config.CodeGenConfig

/**
Expand All @@ -30,8 +28,7 @@ import org.grails.config.CodeGenConfig
class InterceptorGenerator extends AbstractGenerator {

@Override
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean generate() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand All @@ -58,8 +55,7 @@ class InterceptorGenerator extends AbstractGenerator {
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean revoke() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import grails.cli.generator.GenerationContext
import org.grails.build.parsing.CommandLine
import org.grails.config.CodeGenConfig

/**
Expand All @@ -30,8 +28,7 @@ import org.grails.config.CodeGenConfig
class ServiceGenerator extends AbstractGenerator {

@Override
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean generate() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down Expand Up @@ -60,8 +57,7 @@ class ServiceGenerator extends AbstractGenerator {
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean revoke() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.grails.cli.generator
import groovy.transform.CompileStatic

import grails.cli.generator.AbstractGenerator
import grails.cli.generator.GenerationContext
import org.grails.build.parsing.CommandLine
import org.grails.config.CodeGenConfig

/**
Expand All @@ -30,8 +28,7 @@ import org.grails.config.CodeGenConfig
class TaglibGenerator extends AbstractGenerator {

@Override
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean generate() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down Expand Up @@ -59,8 +56,7 @@ class TaglibGenerator extends AbstractGenerator {
}

@Override
boolean revoke(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
boolean revoke() {
String[] args = commandLine.remainingArgs.toArray(new String[0])
if (args.size() < 2) {
return
Expand Down

0 comments on commit 6b9237b

Please sign in to comment.