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

Commit

Permalink
Add generation exclusion to RAML generator if file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Rich committed May 5, 2016
1 parent 0e671ed commit f4a6766
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.squareup.javapoet.TypeSpec.classBuilder;
import static javax.lang.model.element.Modifier.PUBLIC;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
import static uk.gov.justice.services.adapters.rest.generator.Names.RESOURCE_PACKAGE_NAME;
import static uk.gov.justice.services.adapters.rest.generator.Names.RESOURCE_PACKAGE_NAME_WITH_DOT;
import static uk.gov.justice.services.adapters.rest.generator.Names.applicationNameFrom;
import static uk.gov.justice.services.adapters.rest.generator.Names.baseUriPathWithoutContext;

Expand Down Expand Up @@ -119,6 +119,6 @@ private CodeBlock statementsToAddClassToSetForEach(final Collection<String> impl
* implementation class
*/
private ClassName classNameTypeOf(final String className) {
return ClassName.get(config.getBasePackageName() + RESOURCE_PACKAGE_NAME, className);
return ClassName.get(config.getBasePackageName() + RESOURCE_PACKAGE_NAME_WITH_DOT, className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static uk.gov.justice.services.adapters.rest.generator.Generators.byMimeTypeOrder;
import static uk.gov.justice.services.adapters.rest.generator.Names.DEFAULT_ANNOTATION_PARAMETER;
import static uk.gov.justice.services.adapters.rest.generator.Names.GENERIC_PAYLOAD_ARGUMENT_NAME;
import static uk.gov.justice.services.adapters.rest.generator.Names.RESOURCE_PACKAGE_NAME;
import static uk.gov.justice.services.adapters.rest.generator.Names.RESOURCE_PACKAGE_NAME_WITH_DOT;
import static uk.gov.justice.services.adapters.rest.generator.Names.buildResourceMethodName;
import static uk.gov.justice.services.adapters.rest.generator.Names.buildResourceMethodNameWithNoMimeType;
import static uk.gov.justice.services.adapters.rest.generator.Names.resourceInterfaceNameOf;
Expand Down Expand Up @@ -171,7 +171,7 @@ private List<MethodSpec> processOneOrMoreActionBodies(final Action action) {
* @return the {@link ClassName} of the interface
*/
private ClassName interfaceClassNameFor(final Resource resource) {
return ClassName.get(configuration.getBasePackageName() + RESOURCE_PACKAGE_NAME, resourceInterfaceNameOf(resource));
return ClassName.get(configuration.getBasePackageName() + RESOURCE_PACKAGE_NAME_WITH_DOT, resourceInterfaceNameOf(resource));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ final class Names {

static final String DEFAULT_ANNOTATION_PARAMETER = "value";
static final String GENERIC_PAYLOAD_ARGUMENT_NAME = "entity";
static final String RESOURCE_PACKAGE_NAME = ".resource";
static final String RESOURCE_PACKAGE_NAME = "resource";
static final String RESOURCE_PACKAGE_NAME_WITH_DOT = "." + RESOURCE_PACKAGE_NAME;
static final String JAVA_FILENAME_SUFFIX = ".java";

private static final Set<String> JAVA_KEYWORDS = Collections.unmodifiableSet(new HashSet<>(
Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static org.apache.commons.lang.Validate.notEmpty;
import static org.apache.commons.lang.Validate.notNull;
import static uk.gov.justice.services.adapters.rest.generator.Generators.componentFromBaseUriIn;
import static uk.gov.justice.services.adapters.rest.generator.Names.JAVA_FILENAME_SUFFIX;
import static uk.gov.justice.services.adapters.rest.generator.Names.RESOURCE_PACKAGE_NAME;
import static uk.gov.justice.services.adapters.rest.generator.Names.RESOURCE_PACKAGE_NAME_WITH_DOT;

import uk.gov.justice.raml.common.validator.CompositeRamlValidator;
import uk.gov.justice.raml.common.validator.ContainsActionsRamlValidator;
Expand All @@ -32,6 +34,8 @@

public class RestAdapterGenerator implements Generator {

private static final String JAVA_SRC_PATH = "\\main\\java\\";

private final RamlValidator validator = new CompositeRamlValidator(
new ContainsResourcesRamlValidator(),
new ContainsActionsRamlValidator(),
Expand Down Expand Up @@ -86,7 +90,7 @@ private void writeToBasePackage(final TypeSpec typeSpec, final GeneratorConfig c
* @return the list of class names written to file
*/
private List<String> writeToResourcePackage(final List<TypeSpec> typeSpecs, final GeneratorConfig configuration) {
return writeToBasePackage(typeSpecs, configuration, RESOURCE_PACKAGE_NAME);
return writeToBasePackage(typeSpecs, configuration, RESOURCE_PACKAGE_NAME_WITH_DOT);
}

/**
Expand All @@ -103,18 +107,33 @@ private List<String> writeToBasePackage(final List<TypeSpec> typeSpecs,

final List<String> implementationNames = new ArrayList<>();

typeSpecs.stream().forEach(typeSpec -> {
try {
JavaFile.builder(configuration.getBasePackageName() + packageName, typeSpec)
.build()
.writeTo(configuration.getOutputDirectory());
implementationNames.add(typeSpec.name);
} catch (IOException e) {
throw new IllegalStateException(e);
}
});
typeSpecs.stream()
.forEach(typeSpec -> {
try {
if (classDoesNotExist(configuration, typeSpec)) {
JavaFile.builder(configuration.getBasePackageName() + packageName, typeSpec)
.build()
.writeTo(configuration.getOutputDirectory());
}

implementationNames.add(typeSpec.name);
} catch (IOException e) {
throw new IllegalStateException(e);
}
});

return implementationNames;
}

private boolean classDoesNotExist(final GeneratorConfig configuration, final TypeSpec typeSpec) {
final String relativeJavaSourcePath = configuration.getSourceDirectory().getParent().toString() + JAVA_SRC_PATH;
final String basePackagePath = configuration.getBasePackageName().replaceAll("\\.", "\\\\");

final String pathname = relativeJavaSourcePath + basePackagePath + "\\"
+ RESOURCE_PACKAGE_NAME + "\\"
+ typeSpec.name + JAVA_FILENAME_SUFFIX;

return !new File(pathname).exists();
}

}

0 comments on commit f4a6766

Please sign in to comment.