Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Merge bf9afda into 9c53223
Browse files Browse the repository at this point in the history
  • Loading branch information
BenNzewi committed Jul 11, 2018
2 parents 9c53223 + bf9afda commit 85a2de5
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## Unreleased

## [2.6.0] - 2018-07-11
- Handle source and classpath directory in generator-maven-plugin

## [2.5.1] - 2018-06-21

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ public class GenerateGoalConfig extends BasicGoalConfig {

private final List<Path> sourcePaths;

private final GenerateMojo.GenerationPath generationPath;

public GenerateGoalConfig(final String generatorName,
final Path sourceDirectory,
final Path outputDirectory,
final String basePackageName,
final List<String> includes,
final List<String> excludes,
final GeneratorProperties properties,
final List<Path> sourcePaths) {
final List<Path> sourcePaths,
final GenerateMojo.GenerationPath generationPath) {
super(sourceDirectory, includes, excludes);
this.generatorName = generatorName;
this.outputDirectory = outputDirectory;
this.basePackageName = basePackageName;
this.properties = properties;
this.sourcePaths = sourcePaths;
this.generationPath = generationPath;
}

public String getGeneratorName() {
Expand All @@ -56,4 +60,13 @@ public GeneratorProperties getProperties() {
public List<Path> getSourcePaths() {
return sourcePaths;
}

public GenerateMojo.GenerationPath getGenerationPath() { return generationPath; }

@Override
public String toString() {
return "GenerateGoalConfig{" +
"generationPath=" + generationPath +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package uk.gov.justice.maven.generator.io.files.parser.generator;

import static java.lang.String.format;
import static org.slf4j.LoggerFactory.getLogger;

import uk.gov.justice.maven.generator.io.files.parser.FileParser;
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScannerFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

/**
* Service for calling a generator on RAML files.
*/
public class GenerateGoalProcessor {
private static final Logger LOGGER = getLogger(GenerateGoalProcessor.class);
private static final String CLASSPATH = "CLASSPATH";

private final MojoGeneratorFactory mojoGeneratorFactory;
private final FileParser parser;
Expand All @@ -28,16 +39,59 @@ public GenerateGoalProcessor(final MojoGeneratorFactory mojoGeneratorFactory,

@SuppressWarnings("unchecked")
public void generate(final GenerateGoalConfig config) throws IOException {

LOGGER.info("Config: {}", config);
final String[] includes = config.getIncludes().toArray(new String[config.getIncludes().size()]);
final String[] excludes = config.getExcludes().toArray(new String[config.getExcludes().size()]);

final GeneratorConfig generatorConfig = new GeneratorConfig(config.getSourceDirectory(),
config.getOutputDirectory(), config.getBasePackageName(), config.getProperties(), config.getSourcePaths());

final Collection<Path> paths = scannerFactory.create().find(config.getSourceDirectory(), includes, excludes);
final Collection<Path> paths = getPaths(config.getSourceDirectory(), includes, excludes);
final Collection<Path> classPaths;

if (config.getGenerationPath()==GenerateMojo.GenerationPath.SOURCE_AND_CLASS_PATH) {
final Map<Path, Collection<Path>> combinedPaths = new HashMap<>();

classPaths = getPaths(Paths.get(CLASSPATH), includes, excludes);

combinedPaths.put(config.getSourceDirectory(), paths);
combinedPaths.put((Paths.get(format("%s/%s", config.getSourceDirectory().toString(), CLASSPATH))), classPaths);

combinedPaths.forEach((baseDir, combinedPath) ->
parseSourceDirectory(config, generatorConfig, combinedPath, isPathFromClasspath(baseDir, config.getGenerationPath())));
} else {
if ((config.getGenerationPath()==GenerateMojo.GenerationPath.CLASSPATH)) {
classPaths = getPaths(Paths.get(CLASSPATH), includes, excludes);

parseSourceDirectory(config, generatorConfig,
classPaths, isPathFromClasspath(config.getSourceDirectory(), config.getGenerationPath()));
} else {

if(StringUtils.isNoneBlank(config.getSourceDirectory().toString()) && !config.getSourceDirectory().toString().contains(CLASSPATH)){
parseSourceDirectory(config, generatorConfig, paths, isPathFromClasspath(config.getSourceDirectory(), config.getGenerationPath()));
}
else{
classPaths = getPaths(Paths.get(CLASSPATH), includes, excludes);

parseSourceDirectory(config, generatorConfig, classPaths, isPathFromClasspath(config.getSourceDirectory(), config.getGenerationPath()));
}
}
}
}

private Collection<Path> getPaths(final Path path, final String[] includes, final String[] excludes) throws IOException {
return scannerFactory.create().find(path, includes, excludes);
}

private void parseSourceDirectory(final GenerateGoalConfig config, final GeneratorConfig generatorConfig,
final Collection<Path> paths, boolean isPathFromClasspath) {
parser
.parse(config.getSourceDirectory(), paths)
.parse((isPathFromClasspath?Paths.get(format("%s/%s",config.getSourceDirectory().toString(),CLASSPATH)):config.getSourceDirectory()), paths)
.forEach(file -> mojoGeneratorFactory.instanceOf(config.getGeneratorName()).run(file, generatorConfig));
}

private boolean isPathFromClasspath(final Path baseDir, final GenerateMojo.GenerationPath generationPath){
return baseDir.toString().contains(CLASSPATH) || (generationPath != null && generationPath.name()==(CLASSPATH));
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.maven.generator.io.files.parser.generator;

import static java.lang.String.format;
import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE_PLUS_RUNTIME;

import uk.gov.justice.maven.generator.io.files.parser.common.BasicMojo;
Expand Down Expand Up @@ -52,6 +53,12 @@ public class GenerateMojo extends BasicMojo {
@Parameter(property = "skipGeneration", defaultValue = "false")
private boolean skip = false;

public enum GenerationPath {CLASSPATH, SOURCE_AND_CLASS_PATH}

@Parameter(property = "generationPath", required = false)
private GenerationPath generationPath;


@Override
public void execute() throws MojoExecutionException {
if (!skip) {
Expand All @@ -66,11 +73,14 @@ public void execute() throws MojoExecutionException {

try {
FileUtils.forceMkdir(outputDirectory);
new ProjectDependencyLoader(project).loadProjectDependencies();
final ProjectDependencyLoader projectDependencyLoader = new ProjectDependencyLoader(project);
projectDependencyLoader.loadProjectDependencies();
new GenerateGoalProcessor(

new MojoGeneratorFactory(),
new FileTreeScannerFactory(),
new FileParserInstanceFactory().newInstanceOf(parserName))
new FileParserInstanceFactory().newInstanceOf(parserName)
)
.generate(configuration(sourcePaths));
} catch (Exception e) {
throw new MojoExecutionException("Failed to apply generator to source file", e);
Expand All @@ -90,7 +100,7 @@ private GenerateGoalConfig configuration(final List<Path> sourcePaths) {
includes,
excludes,
generatorProperties,
sourcePaths);

sourcePaths,
generationPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package uk.gov.justice.maven.generator.io.files.parser.generator;


import static java.nio.file.Files.exists;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasProperty;
Expand All @@ -19,6 +22,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -44,7 +48,6 @@ public void testShouldGenerateFromValidRaml() throws Exception {
List<Pair<Raml, GeneratorConfig>> capturedGeneratorArgs = DummyGeneratorCaptor.getInstance().capturedArgs();
assertThat(capturedGeneratorArgs, hasSize(1));


Raml raml = capturedGeneratorArgs.get(0).getLeft();
assertThat(raml.getTitle(), equalTo("example.raml"));

Expand Down Expand Up @@ -123,6 +126,7 @@ public void testShouldGenerateFromOnlyIncludedRaml() throws Exception {
//WARNING: This test will fail in Intellij, as everything is on the one classpath in Intellij
@SuppressWarnings("unchecked")
public void testShouldIncludeRamlFilesFromTheClasspath() throws Exception {

File pom = getTestFile("src/test/resources/includes-excludes-external/pom.xml");

GenerateMojo mojo = (GenerateMojo) lookupConfiguredMojo(pom, "generate");
Expand All @@ -146,4 +150,53 @@ public void testShouldSkipExecution() throws Exception {
List<Pair<Raml, GeneratorConfig>> capturedGeneratorArgs = DummyGeneratorCaptor.getInstance().capturedArgs();
assertThat(capturedGeneratorArgs, hasSize(0));
}


public void testShouldIncludeRamlFilesWhenSourcePathOrClassPathSpecified() throws Exception {
final File pom = getTestFile("src/test/resources/generate-using-source-and-classpath/pom.xml");

final GenerateMojo mojo = (GenerateMojo) lookupConfiguredMojo(pom, "generate");

mojo.execute();
final List<Pair<Raml, GeneratorConfig>> capturedGeneratorArgs = DummyGeneratorCaptor.getInstance().capturedArgs();

assertThat(capturedGeneratorArgs.size(), greaterThan(5));

final Path expectedSourceDirectory = Paths.get(project.getBasedir().toString(), "src", "raml");
final Path expectedOutputDirectory = Paths.get(project.getBasedir().toString(), "target", "generated-sources");

final GeneratorConfig config = capturedGeneratorArgs.get(0).getRight();
assertThat(config.getSourceDirectory(), equalTo(expectedSourceDirectory));
assertThat(config.getOutputDirectory(), equalTo(expectedOutputDirectory));
assertThat(config.getBasePackageName(), equalTo("uk.gov.justice.api"));

final TestGeneratorProperties customGeneratorProperties = (TestGeneratorProperties) config.getGeneratorProperties();
assertThat(customGeneratorProperties.getProperty1(), equalTo("propertyValueABC"));
assertThat(customGeneratorProperties.getProperty2(), equalTo("propertyValueDDD"));

final List<Raml> capturedRamls = DummyGeneratorCaptor.getInstance().capturedRamls();
final List<String> capturedRamlTitles = capturedRamls.stream().map(capturedRamlTitle-> capturedRamlTitle.getTitle())
.collect(Collectors.toList());

assertThat(capturedRamlTitles, hasItem("external7.raml"));
assertThat(capturedRamlTitles, hasItem("external8.raml"));
}

public void testShouldIncludeRamlFilesWhenClassPathSpecifiedAndSubDirectorySetInPom() throws Exception {
final File pom = getTestFile("src/test/resources/generate-using-classpath/pom.xml");

final GenerateMojo mojo = (GenerateMojo) lookupConfiguredMojo(pom, "generate");

mojo.execute();
final List<Pair<Raml, GeneratorConfig>> capturedGeneratorArgs = DummyGeneratorCaptor.getInstance().capturedArgs();
assertThat(capturedGeneratorArgs.size(), greaterThan(5));

final List<Raml> capturedRamls = DummyGeneratorCaptor.getInstance().capturedRamls();
final List<String> capturedRamlTitles = capturedRamls.stream().map(capturedRamlTitle-> capturedRamlTitle.getTitle())
.collect(Collectors.toList());

assertThat(capturedRamlTitles, hasItem("example.raml"));
assertThat(capturedRamlTitles, hasItem("external7.raml"));
assertThat(capturedRamlTitles, hasItem("external8.raml"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId> uk.gov.justice.raml.maven.generator.test</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<cpp.service-component>UNKNOWN_SERVICE_COMPONENT</cpp.service-component>
<generationPath>CLASSPATH</generationPath>
</properties>

<build>
<plugins>
<plugin>
<artifactId>generator-plugin</artifactId>
<groupId>uk.gov.justice.maven.generator</groupId>
<configuration>
<generatorName>uk.gov.justice.maven.generator.io.files.parser.generator.DummyGenerator
</generatorName>
<parserName>uk.gov.justice.maven.generator.io.files.parser.RamlFileParser</parserName>
<basePackageName>uk.gov.justice.api</basePackageName>
<includes>
<include>**/ex*.raml</include>
</includes>
<excludes>
<exclude>invalid-raml/**/ex*.raml</exclude>
</excludes>
<sourceDirectory>${basedir}/src/raml</sourceDirectory>
<generatorProperties implementation="uk.gov.justice.maven.generator.io.files.parser.generator.property.TestGeneratorProperties">
<property1>propertyValueABC</property1>
<property2>propertyValueDDD</property2>

<nestedProperty>
<property>test1</property>
<property>test2</property>
<property>test3</property>
</nestedProperty>
</generatorProperties>

</configuration>

</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: example.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: example1.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external7.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external8.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId> uk.gov.justice.raml.maven.generator.test</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<cpp.service-component>UNKNOWN_SERVICE_COMPONENT</cpp.service-component>
<generationPath>SOURCE_AND_CLASS_PATH</generationPath>
</properties>

<build>

<plugins>
<plugin>
<artifactId>generator-plugin</artifactId>
<groupId>uk.gov.justice.maven.generator</groupId>
<configuration>
<generatorName>uk.gov.justice.maven.generator.io.files.parser.generator.DummyGenerator
</generatorName>
<parserName>uk.gov.justice.maven.generator.io.files.parser.RamlFileParser</parserName>
<basePackageName>uk.gov.justice.api</basePackageName>
<includes>
<include>**/ex*.raml</include>
</includes>
<excludes>
<exclude>invalid-raml/**/ex*.raml</exclude>
</excludes>
<sourceDirectory>${basedir}/src/raml</sourceDirectory>
<generatorProperties implementation="uk.gov.justice.maven.generator.io.files.parser.generator.property.TestGeneratorProperties">
<property1>propertyValueABC</property1>
<property2>propertyValueDDD</property2>

<nestedProperty>
<property>test1</property>
<property>test2</property>
<property>test3</property>
</nestedProperty>
</generatorProperties>

</configuration>

</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: example.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: example1.raml
Loading

0 comments on commit 85a2de5

Please sign in to comment.