Skip to content

Commit

Permalink
Refactor Generator API
Browse files Browse the repository at this point in the history
Introduce `GenerationContext` to `grails.cli.generator.Generator`

Closes gh-801
  • Loading branch information
rainboyan committed Dec 15, 2024
1 parent 68402f9 commit 324ffb3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package grails.cli.generator;

import org.grails.build.parsing.CommandLine;

public class GenerationContext {
private CommandLine commandLine;

public CommandLine getCommandLine() {
return this.commandLine;
}

public void setCommandLine(CommandLine commandLine) {
this.commandLine = commandLine;
}

}
5 changes: 2 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 @@ -16,7 +16,6 @@
package grails.cli.generator;

import grails.util.GrailsNameUtils;
import org.grails.build.parsing.CommandLine;

/**
* @author Michael Yan
Expand All @@ -32,11 +31,11 @@ default String getDescription() {
return getName();
}

default boolean generate(CommandLine commandLine) {
default boolean generate(GenerationContext generationContext) {
return true;
}

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

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

import grails.build.logging.GrailsConsole
import grails.cli.generator.GenerationContext
import grails.cli.generator.Generator
import org.grails.build.parsing.CommandLine
import org.grails.cli.profile.CommandDescription
Expand Down Expand Up @@ -53,13 +54,15 @@ class GenerateCommand implements ProjectCommand {
@Override
boolean handle(ExecutionContext executionContext) {
CommandLine commandLine = executionContext.commandLine
GenerationContext generationContext = new GenerationContext()
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(commandLine)
return generator.help(generationContext)
}
else {
noGenerator(generatorName, executionContext.console)
Expand All @@ -80,7 +83,7 @@ class GenerateCommand implements ProjectCommand {
String generatorName = commandLine.remainingArgs[0]
Generator generator = loadedGenerators().find { it.name == generatorName }
if (generator) {
return generator.generate(commandLine)
return generator.generate(generationContext)
}
else {
noGenerator(generatorName, executionContext.console)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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 @@ -29,7 +30,8 @@ import org.grails.config.CodeGenConfig
class ControllerGenerator extends AbstractGenerator {

@Override
boolean generate(CommandLine commandLine) {
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
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,6 +18,7 @@ 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 @@ -47,7 +48,8 @@ class DomainGenerator extends AbstractGenerator {
]

@Override
boolean generate(CommandLine commandLine) {
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
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,6 +18,7 @@ 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 @@ -29,7 +30,8 @@ import org.grails.config.CodeGenConfig
class InterceptorGenerator extends AbstractGenerator {

@Override
boolean generate(CommandLine commandLine) {
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
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,6 +18,7 @@ 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 @@ -29,7 +30,8 @@ import org.grails.config.CodeGenConfig
class ServiceGenerator extends AbstractGenerator {

@Override
boolean generate(CommandLine commandLine) {
boolean generate(GenerationContext generationContext) {
CommandLine commandLine = generationContext.commandLine
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,6 +18,7 @@ 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 @@ -29,7 +30,8 @@ import org.grails.config.CodeGenConfig
class TaglibGenerator extends AbstractGenerator {

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

0 comments on commit 324ffb3

Please sign in to comment.