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

Commit

Permalink
Merge pull request #3 from CJSCommonPlatform/CRC-1634
Browse files Browse the repository at this point in the history
Refactor mojo and change generator interface
  • Loading branch information
jaceko committed Mar 10, 2016
2 parents b1d35d8 + ffa0ac6 commit 5164ae5
Show file tree
Hide file tree
Showing 34 changed files with 826 additions and 400 deletions.
37 changes: 37 additions & 0 deletions io-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>raml-jaxrs</artifactId>
<groupId>uk.gov.justice</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>io-utils</artifactId>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.22</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
47 changes: 47 additions & 0 deletions io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.gov.justice.raml.io;

import org.codehaus.plexus.util.DirectoryScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

import static java.lang.String.format;

/**
* Utility class for searching directories.
*/
public class FileTreeScanner {

private static final Logger LOGGER = LoggerFactory.getLogger(FileTreeScanner.class);

/**
* Finding all files within a directory that fulfil a set of include and exclude patterns, using standard
* Ant patterns - {@see http://ant.apache.org/manual/dirtasks.html#patterns}.
*
* @param baseDir the path to search under
* @param includes the path patterns to include
* @param excludes the path patterns to exclude
* @return a list of paths to matching files under the specified directory
*/
public Collection<Path> find(final Path baseDir, final String[] includes, final String[] excludes) throws IOException {

LOGGER.debug(format("Finding files in: %s", baseDir.toString()));

DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(baseDir.toFile());
scanner.setIncludes(includes);
scanner.setExcludes(excludes);
scanner.scan();

return Arrays.asList(scanner.getIncludedFiles())
.stream()
.map(Paths::get)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.gov.justice.raml.io;

import org.junit.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;

/**
* Unit tests for the {@link FileTreeScanner} class.
*/
public class FileTreeScannerTest {

@Test
@SuppressWarnings("unchecked")
public void shouldFindOnlyMatchingFiles() throws Exception {

String[] includes = {"**/*.raml"};
String[] excludes = {"**/*ignore.raml"};
Path baseDir = Paths.get("src/test/resources/raml/");

FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);

assertThat(paths, hasSize(3));
assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("example-1.raml")),
equalTo(Paths.get("example-2.raml")),
equalTo(Paths.get("subfolder/example-3.raml"))));
}
}
15 changes: 15 additions & 0 deletions io-utils/src/test/resources/raml/example-1.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#%RAML 0.8
title: Example
baseUri: http://localhost:8080/
version: v0.1
mediaType: application/json
protocols: [ HTTP, HTTPS ]

/test:
description: |
Post to this entry point to create a test
post:
headers:
Content-Type:
type: string
example: application/json
15 changes: 15 additions & 0 deletions io-utils/src/test/resources/raml/example-2.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#%RAML 0.8
title: Example
baseUri: http://localhost:8080/
version: v0.1
mediaType: application/json
protocols: [ HTTP, HTTPS ]

/test:
description: |
Post to this entry point to create a test
post:
headers:
Content-Type:
type: string
example: application/json
15 changes: 15 additions & 0 deletions io-utils/src/test/resources/raml/ignore.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#%RAML 0.8
title: Example
baseUri: http://localhost:8080/
version: v0.1
mediaType: application/json
protocols: [ HTTP, HTTPS ]

/test:
description: |
Post to this entry point to create a test
post:
headers:
Content-Type:
type: string
example: application/json
1 change: 1 addition & 0 deletions io-utils/src/test/resources/raml/notRaml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A test file which is not raml.
15 changes: 15 additions & 0 deletions io-utils/src/test/resources/raml/subfolder/example-3.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#%RAML 0.8
title: Example
baseUri: http://localhost:8080/
version: v0.1
mediaType: application/json
protocols: [ HTTP, HTTPS ]

/test:
description: |
Post to this entry point to create a test
post:
headers:
Content-Type:
type: string
example: application/json
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<modules>
<module>raml-maven-plugin</module>
<module>raml-generator-core</module>
<module>io-utils</module>
<module>raml-parser</module>
</modules>

<dependencyManagement>
Expand All @@ -25,6 +27,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.raml</groupId>
<artifactId>raml-parser</artifactId>
<version>0.8.12</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
7 changes: 6 additions & 1 deletion raml-generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@

<artifactId>raml-generator-core</artifactId>


<dependencies>
<dependency>
<groupId>org.raml</groupId>
<artifactId>raml-parser</artifactId>
</dependency>
</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package uk.gov.justice.raml.core;

import org.raml.model.Raml;

import java.util.Set;

public interface Generator {

Set<String> run(String raml, Configuration configuration);
Set<String> run(Raml raml, GeneratorConfig generatorConfig);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package uk.gov.justice.raml.core;

import java.nio.file.Path;

public class GeneratorConfig {

private Path outputDirectory;
private String basePackageName;
private Path sourceDirectory;

public GeneratorConfig(final Path sourceDirectory, final Path outputDirectory, final String basePackageName) {
this.outputDirectory = outputDirectory;
this.basePackageName = basePackageName;
this.sourceDirectory = sourceDirectory;
}

public Path getOutputDirectory() {
return outputDirectory;
}

public String getBasePackageName() {
return basePackageName;
}

public Path getSourceDirectory() {
return sourceDirectory;
}
}
10 changes: 10 additions & 0 deletions raml-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
<artifactId>raml-generator-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice</groupId>
<artifactId>io-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice</groupId>
<artifactId>raml-parser</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.gov.justice.raml.jaxrs.maven;

import java.nio.file.Path;

/**
* POJO to hold config for the generate goal.
*/
public class GenerateGoalConfig {

private final String generatorName;

private final Path outputDirectory;

private final Path sourceDirectory;

private final String basePackageName;

public GenerateGoalConfig(final String generatorName, final Path sourceDirectory, final Path outputDirectory, final String basePackageName) {
this.generatorName = generatorName;
this.outputDirectory = outputDirectory;
this.sourceDirectory = sourceDirectory;
this.basePackageName = basePackageName;
}

public String getGeneratorName() {
return generatorName;
}

public Path getOutputDirectory() {
return outputDirectory;
}

public Path getSourceDirectory() {
return sourceDirectory;
}

public String getBasePackageName() {
return basePackageName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.justice.raml.jaxrs.maven;

import uk.gov.justice.raml.core.Generator;
import uk.gov.justice.raml.core.GeneratorConfig;
import uk.gov.justice.raml.io.FileTreeScanner;
import uk.gov.justice.raml.io.files.parser.RamlFileParser;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;

/**
* Service for calling a generator on some raml
*/
public class GenerateGoalProcessor {

private final GeneratorFactory generatorFactory;

public GenerateGoalProcessor(final GeneratorFactory generatorFactory) {
this.generatorFactory = generatorFactory;
}

public void generate(final GenerateGoalConfig config) throws IOException {

final String[] includes = {"**/*.raml"};
final String[] excludes = {};
final GeneratorConfig generatorConfig = new GeneratorConfig(config.getSourceDirectory(), config.getOutputDirectory(), config.getBasePackageName());

final Generator generator = generatorFactory.create(config.getGeneratorName());

Collection<Path> paths = new FileTreeScanner().find(config.getSourceDirectory(), includes, excludes);

new RamlFileParser()
.parse(config.getSourceDirectory(), paths)
.stream()
.forEach(raml -> generator.run(raml, generatorConfig));
}

}
Loading

0 comments on commit 5164ae5

Please sign in to comment.