Skip to content

Commit

Permalink
Code Refactoring
Browse files Browse the repository at this point in the history
Remove language and build-tool from method parameters and use defaults as there are no other option.
  • Loading branch information
puneetbehl committed Jul 18, 2023
1 parent ff24e33 commit c9434d1
Show file tree
Hide file tree
Showing 43 changed files with 117 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static CodeGenConfig load(BeanContext beanContext, File directory, Consol
.map(DefaultFeature.class::cast)
.filter(f -> f.shouldApply(
codeGenConfig.getApplicationType(),
new Options(codeGenConfig.getSourceLanguage(), codeGenConfig.getTestFramework(), codeGenConfig.getBuildTool(), VersionInfo.getJavaVersion()),
new Options(codeGenConfig.getTestFramework(), codeGenConfig.getBuildTool(), VersionInfo.getJavaVersion()),
new HashSet<>()))
.map(Feature::getName)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public abstract class CreateCommand extends BaseCommand implements Callable<Inte
@CommandLine.Parameters(arity = "0..1", paramLabel = "NAME", description = "The name of the application to create.")
String name;

@ReflectiveAccess
@CommandLine.Option(names = {"-l", "--lang"}, paramLabel = "LANG", description = "Which language to use. Possible values: ${COMPLETION-CANDIDATES}.", completionCandidates = LanguageCandidates.class, converter = LanguageConverter.class)
Language lang;

@ReflectiveAccess
@CommandLine.Option(names = {"-t", "--test"}, paramLabel = "TEST", description = "Which test framework to use. Possible values: ${COMPLETION-CANDIDATES}.", completionCandidates = TestFrameworkCandidates.class, converter = TestFrameworkConverter.class)
TestFramework test;
Expand Down Expand Up @@ -102,7 +98,7 @@ protected Map<String, Object> getAdditionalOptions() {
public Integer call() throws Exception {
if (listFeatures) {
new ListFeatures(availableFeatures,
new Options(lang, test, build, gormImpl, servletImpl, getJdkVersion(), getOperatingSystem()),
new Options(test, build, gormImpl, servletImpl, getJdkVersion(), getOperatingSystem()),
applicationType,
getOperatingSystem(),
contextFactory).output(this);
Expand All @@ -128,7 +124,7 @@ public void generate(OutputHandler outputHandler) throws Exception {
}

public void generate(Project project, OutputHandler outputHandler) throws Exception {
Options options = new Options(lang, test, build, gormImpl, servletImpl, getJdkVersion(), getOperatingSystem(), getAdditionalOptions());
Options options = new Options(test, build, gormImpl, servletImpl, getJdkVersion(), getOperatingSystem(), getAdditionalOptions());

projectGenerator.generate(applicationType, project, options, getOperatingSystem(), getSelectedFeatures(), outputHandler, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.grails.forge.io.OutputHandler
import org.grails.forge.options.BuildTool
import org.grails.forge.options.Language
import org.grails.forge.options.Options
import org.grails.forge.options.TestFramework
import org.grails.forge.util.NameUtils

trait CommandFixture {
Expand All @@ -25,7 +26,7 @@ trait CommandFixture {
ApplicationType applicationType = ApplicationType.DEFAULT_OPTION) {
beanContext.getBean(ProjectGenerator).generate(applicationType,
NameUtils.parse("example.grails.foo"),
new Options(language, null, buildTool),
new Options(TestFramework.DEFAULT_OPTION, buildTool),
OperatingSystem.MACOS_ARCH64,
features,
new FileSystemOutputHandler(dir, ConsoleOutput.NOOP),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ class CreateAppCommandSpec extends CommandSpec implements CommandFixture {
baos.toString().contains("Invalid GORM implementation selection: xyz")
}

void "test creating a project with an invalid language"() {
given:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
System.setErr(new PrintStream(baos))

when:
PicocliRunner.run(CreateAppCommand, ctx, "temp", "--lang", "xyz")

then:
noExceptionThrown()
baos.toString().contains("Invalid language selection: xyz")
}

void "community and preview features are labelled as such"() {
given:
ByteArrayOutputStream baos = new ByteArrayOutputStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ public FeatureList features(ApplicationType type,
List<FeatureDTO> featureDTOList = featureOperations
.getFeatures(requestInfo.getLocale(),
type,
new Options(Language.DEFAULT_OPTION,
test != null ? test.toTestFramework() : null,
new Options(test != null ? test.toTestFramework() : null,
build == null ? BuildTool.DEFAULT_OPTION : build,
gorm == null ? GormImpl.DEFAULT_OPTION : gorm,
servlet == null ? ServletImpl.DEFAULT_OPTION : servlet,
Expand All @@ -203,8 +202,7 @@ public FeatureList defaultFeatures(ApplicationType type,
List<FeatureDTO> featureDTOList = featureOperations
.getDefaultFeatures(requestInfo.getLocale(),
type,
new Options(Language.DEFAULT_OPTION,
test != null ? test.toTestFramework() : null,
new Options(test != null ? test.toTestFramework() : null,
build == null ? BuildTool.DEFAULT_OPTION : build,
gorm == null ? GormImpl.DEFAULT_OPTION : gorm,
servlet == null ? ServletImpl.DEFAULT_OPTION : servlet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ public GeneratorContext createProjectGeneratorContext(
try {
GormImpl gormImpl = gorm != null ? gorm : GormImpl.DEFAULT_OPTION;
ServletImpl servletImpl = servlet != null ? servlet : ServletImpl.DEFAULT_OPTION;
Language lang = Language.DEFAULT_OPTION;
generatorContext = projectGenerator.createGeneratorContext(
type,
project,
new Options(lang,
testFramework != null ? testFramework.toTestFramework() : lang.getDefaults().getTest(),
buildTool == null ? lang.getDefaults().getBuild() : buildTool,
new Options(testFramework != null ? testFramework.toTestFramework() : org.grails.forge.options.TestFramework.DEFAULT_OPTION,
buildTool == null ? BuildTool.DEFAULT_OPTION : buildTool,
gormImpl == null ? GormImpl.DEFAULT_OPTION : gormImpl,
servletImpl == null ? ServletImpl.DEFAULT_OPTION : servletImpl,
javaVersion == null ? JdkVersion.DEFAULT_OPTION : javaVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public PreviewDTO previewApp(
projectGenerator.generate(type,
project,
new Options(
Language.DEFAULT_OPTION,
test != null ? test.toTestFramework() : null,
build == null ? BuildTool.DEFAULT_OPTION : build,
gorm == null ? GormImpl.DEFAULT_OPTION : gorm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public FeatureContext createFeatureContext(AvailableFeatures availableFeatures,
}
}

Language language = determineLanguage(options.getLanguage(), features);
Options newOptions = options.withLanguage(language)
Options newOptions = options
.withTestFramework(determineTestFramework(options.getTestFramework()))
.withGormImpl(determineGormImpl(options.getGormImpl()))
.withServletImpl(determineServletImpl(options.getServletImpl()))
.withBuildTool(determineBuildTool(language, options.getBuildTool()));
.withBuildTool(determineBuildTool(options.getBuildTool()));

availableFeatures.getAllFeatures()
.filter(f -> f instanceof DefaultFeature)
Expand All @@ -87,6 +87,13 @@ public GeneratorContext createGeneratorContext(Project project,
return new GeneratorContext(project, featureContext.getApplicationType(), featureContext.getOptions(), featureContext.getOperatingSystem(), featureList, coordinateResolver);
}

TestFramework determineTestFramework(TestFramework testFramework) {
if (testFramework == null) {
testFramework = TestFramework.DEFAULT_OPTION;
}
return testFramework;
}

Language determineLanguage(Language language, Set<Feature> features) {
if (language == null) {
language = Language.infer(features);
Expand All @@ -97,9 +104,9 @@ Language determineLanguage(Language language, Set<Feature> features) {
return language;
}

BuildTool determineBuildTool(Language language, BuildTool buildTool) {
BuildTool determineBuildTool(BuildTool buildTool) {
if (buildTool == null) {
buildTool = language.getDefaults().getBuild();
buildTool = BuildTool.GRADLE;
}
return buildTool;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum OperatingSystem {
SOLARIS;

public static final OperatingSystem DEFAULT = OperatingSystem.LINUX;
public static final OperatingSystem DEFAULT_OPTION = OperatingSystem.LINUX;
private final String architecture;

OperatingSystem(String architecture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.grails.forge.feature.cli;
import org.grails.forge.io.ConsoleOutput;
import org.grails.forge.io.OutputHandler;
import org.grails.forge.options.Language;
import org.grails.forge.options.Options;
import org.grails.forge.template.RenderResult;
import org.grails.forge.template.RockerTemplate;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void generate(

generatorContext.addTemplate("grailsCli",
new RockerTemplate("grails-cli.yml",
cli.template(generatorContext.getLanguage(),
cli.template(Language.DEFAULT_OPTION,
generatorContext.getTestFramework(),
generatorContext.getBuildTool(),
generatorContext.getGorm(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,6 @@ public void addConfiguration(@NonNull Configuration configuration) {
return Collections.unmodifiableList(helpTemplates);
}

/**
* @return The language
*/
@NonNull public Language getLanguage() {
return options.getLanguage();
}

/**
* @return The test framework
*/
Expand Down Expand Up @@ -299,28 +292,24 @@ public <T extends Feature> T getRequiredFeature(Class<T> feature) {
}

public String getSourcePath(String path) {
return getLanguage().getSourcePath(path);
return Language.DEFAULT_OPTION.getSourcePath(path);
}

public String getTestSourcePath(String path) {
return getTestFramework().getSourcePath(path, getLanguage());
return getTestFramework().getSourcePath(path, Language.DEFAULT_OPTION);
}

public String getIntegrationTestSourcePath(String path) {
return getTestFramework().getIntegrationSourcePath(path, getLanguage());
return getTestFramework().getIntegrationSourcePath(path, Language.DEFAULT_OPTION);
}

RockerModel parseModel(RockerModel javaTemplate,
RockerModel groovyTemplate) {
switch (getLanguage()) {
case GROOVY:
default:
return groovyTemplate;
}
return groovyTemplate;
}

public void addTemplate(String name, String path, TestRockerModelProvider testRockerModelProvider) {
RockerModel rockerModel = testRockerModelProvider.findModel(getLanguage(), getTestFramework());
RockerModel rockerModel = testRockerModelProvider.findModel(Language.DEFAULT_OPTION, getTestFramework());
if (rockerModel != null) {
addTemplate(name, new RockerTemplate(path, rockerModel));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.grails.forge.build.dependencies.Coordinate;
import org.grails.forge.build.dependencies.Dependency;
import org.grails.forge.build.dependencies.DependencyCoordinate;
import org.grails.forge.options.Language;
import org.grails.forge.template.Writable;

import java.util.Comparator;
Expand Down Expand Up @@ -57,7 +58,7 @@ public GradleDependency(@NonNull Dependency dependency,
super(dependency);
gradleConfiguration = GradleConfiguration.of(
dependency.getScope(),
generatorContext.getLanguage(),
Language.DEFAULT_OPTION,
generatorContext.getTestFramework()
).orElseThrow(() ->
new IllegalArgumentException(String.format("Cannot map the dependency scope: [%s] to a Gradle specific scope", dependency.getScope())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void produceDiff(
projectGenerator.generate(
applicationType,
project,
new Options(generatorContext.getLanguage(), generatorContext.getTestFramework(), generatorContext.getBuildTool(), generatorContext.getJdkVersion()),
new Options(generatorContext.getTestFramework(), generatorContext.getBuildTool(), generatorContext.getJdkVersion()),
generatorContext.getOperatingSystem(),
Collections.emptyList(),
outputHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ public Set<Feature> getFinalFeatures(ConsoleOutput consoleOutput) {
}).collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
}

public Language getLanguage() {
return options.getLanguage();
}

public TestFramework getTestFramework() {
return options.getTestFramework();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Geb(Spock spock) {

@Override
public boolean shouldApply(ApplicationType applicationType, Options options, Set<Feature> selectedFeatures) {
return applicationType == ApplicationType.WEB;
return applicationType == ApplicationType.WEB && options.getTestFramework() != TestFramework.JUNIT;
}

@NonNull
Expand Down Expand Up @@ -132,7 +132,7 @@ public void apply(GeneratorContext generatorContext) {
TestRockerModelProvider provider = new DefaultTestRockerModelProvider(org.grails.forge.feature.test.template.spock.template(project),
groovyJunit.template(project));
generatorContext.addTemplate("applicationTest",
new RockerTemplate(integrationTestSourcePath, provider.findModel(generatorContext.getLanguage(), testFramework))
new RockerTemplate(integrationTestSourcePath, provider.findModel(Language.DEFAULT_OPTION, testFramework))
);
generatorContext.addTemplate("gebConfig",
new RockerTemplate("src/integration-test/resources/GebConfig.groovy", gebConfig.template(project)));
Expand Down
Loading

0 comments on commit c9434d1

Please sign in to comment.