Sources and resource generator mojo which doesn't use value sets. (#852)

* Resource and sources generator mojo which doesn't use value sets.

* Avoid unecessary reference to javassist.
This commit is contained in:
Łukasz Dywicki 2018-12-13 11:28:16 +01:00 committed by James Agnew
parent 81e8131ffc
commit 1afe36e60a
5 changed files with 314 additions and 2 deletions

View File

@ -0,0 +1,50 @@
package ca.uhn.fhir.tinder;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.util.List;
/**
* Base class for mojo generatorss.
*/
public abstract class AbstractGeneratorMojo extends AbstractMojo {
protected final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(getClass());
@Parameter(required = true, defaultValue = "${project.build.directory}/..")
protected String baseDir;
@Parameter
protected String packageBase = "";
@Parameter
protected List<String> baseResourceNames;
@Parameter
protected List<String> excludeResourceNames;
@Parameter
protected String templateName;
@Parameter(required = true)
protected String version;
@Component
protected MavenProject myProject;
@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
doExecute(new Configuration(this.version, baseDir, getTargetDirectory(), this.packageBase, this.baseResourceNames, this.excludeResourceNames));
}
protected abstract void doExecute(Configuration mavenGeneratorConfiguration) throws MojoExecutionException, MojoFailureException;
protected abstract File getTargetDirectory();
}

View File

@ -0,0 +1,134 @@
package ca.uhn.fhir.tinder;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.tinder.parser.BaseStructureSpreadsheetParser;
import org.apache.commons.lang.WordUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;
public class Configuration {
private final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Configuration.class);
private String version;
private File targetDirectory;
private String packageSuffix;
private String packageBase;
private FhirContext fhirContext;
private File packageDirectoryBase;
private final List<String> resourceNames = new ArrayList<>();
private String baseDir;
public Configuration(String version, String baseDir, File targetDirectory, String packageBase, List<String> baseResourceNames, List<String> excludeResourceNames) {
this.targetDirectory = targetDirectory;
this.packageBase = packageBase;
this.packageDirectoryBase = new File(targetDirectory, packageBase.replace(".", File.separatorChar + ""));
switch (version) {
case "dstu2":
fhirContext = FhirContext.forDstu2();
break;
case "dstu3":
fhirContext = FhirContext.forDstu3();
packageSuffix = ".dstu3";
break;
case "r4":
fhirContext = FhirContext.forR4();
packageSuffix = ".r4";
break;
default:
throw new IllegalArgumentException("Unknown version configured: " + version);
}
this.version = version;
if (baseResourceNames == null || baseResourceNames.isEmpty()) {
ourLog.info("No resource names supplied, going to use all resources from version: {}", fhirContext.getVersion().getVersion());
Properties p = new Properties();
try {
p.load(fhirContext.getVersion().getFhirVersionPropertiesFile());
} catch (IOException e) {
throw new IllegalArgumentException("Failed to load version property file", e);
}
ourLog.debug("Property file contains: {}", p);
TreeSet<String> keys = new TreeSet<String>();
for (Object next : p.keySet()) {
keys.add((String) next);
}
for (String next : keys) {
if (next.startsWith("resource.")) {
resourceNames.add(next.substring("resource.".length()).toLowerCase());
}
}
if (fhirContext.getVersion().getVersion() == FhirVersionEnum.DSTU3) {
resourceNames.remove("conformance");
}
} else {
for (String resourceName : baseResourceNames) {
resourceNames.add(resourceName.toLowerCase());
}
}
if (excludeResourceNames != null) {
for (String resourceName : excludeResourceNames) {
resourceNames.remove(resourceName.toLowerCase());
}
}
ourLog.info("Including the following resources: {}", resourceNames);
}
public File getPackageDirectoryBase() {
return packageDirectoryBase;
}
public String getPackageSuffix() {
return packageSuffix;
}
public List<String> getResourceNames() {
return resourceNames;
}
public String getPackageBase() {
return packageBase;
}
public String getVersion() {
return version;
}
public String getResourcePackage() {
if (BaseStructureSpreadsheetParser.determineVersionEnum(version).isRi()) {
return "org.hl7.fhir." + version + ".model";
}
return "ca.uhn.fhir.model." + version + ".resource";
}
public String getVersionCapitalized() {
String capitalize = WordUtils.capitalize(version);
if ("Dstu".equals(capitalize)) {
return "Dstu1";
}
return capitalize;
}
public File getTargetDirectory() {
return targetDirectory;
}
public String getBaseDir() {
return baseDir;
}
}

View File

@ -0,0 +1,75 @@
package ca.uhn.fhir.tinder;
import ca.uhn.fhir.tinder.parser.ResourceGeneratorUsingModel;
import ca.uhn.fhir.tinder.parser.ResourceGeneratorUsingSpreadsheet;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.generic.EscapeTool;
import java.io.*;
@Mojo(name = "generate-resource", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
public class TinderResourceGeneratorMojo extends AbstractGeneratorMojo {
@Parameter(required = true, defaultValue = "${project.build.directory}/generated-resources/tinder")
protected File targetDirectory;
@Parameter(required = true)
protected String fileName = "";
@Override
protected void doExecute(Configuration configuration) throws MojoExecutionException, MojoFailureException {
File packageDirectoryBase = configuration.getPackageDirectoryBase();
packageDirectoryBase.mkdirs();
ResourceGeneratorUsingModel gen = new ResourceGeneratorUsingModel(configuration.getVersion(), configuration.getBaseDir());
gen.setBaseResourceNames(configuration.getResourceNames());
try {
gen.parse();
VelocityContext ctx = new VelocityContext();
ctx.put("resources", gen.getResources());
ctx.put("packageBase", configuration.getPackageBase());
ctx.put("version", configuration.getVersion());
ctx.put("package_suffix", configuration.getPackageSuffix());
ctx.put("esc", new EscapeTool());
ctx.put("resourcePackage", configuration.getResourcePackage());
ctx.put("versionCapitalized", configuration.getVersionCapitalized());
VelocityEngine v = new VelocityEngine();
v.setProperty("resource.loader", "cp");
v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
v.setProperty("runtime.references.strict", Boolean.TRUE);
InputStream templateIs = ResourceGeneratorUsingSpreadsheet.class.getResourceAsStream(templateName);
InputStreamReader templateReader = new InputStreamReader(templateIs);
File file = new File(packageDirectoryBase, fileName);
OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8");
v.evaluate(ctx, w, "", templateReader);
w.close();
Resource resource = new Resource();
resource.setDirectory(packageDirectoryBase.getAbsolutePath());
//resource.setDirectory(targetDirectory.getAbsolutePath());
//resource.addInclude(packageBase);
myProject.addResource(resource);
} catch (Exception e) {
throw new MojoFailureException("Failed to generate resources", e);
}
}
@Override
public File getTargetDirectory() {
return targetDirectory;
}
}

View File

@ -0,0 +1,53 @@
package ca.uhn.fhir.tinder;
import java.io.*;
import java.util.*;
import org.apache.maven.plugin.*;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.tinder.parser.*;
@Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class TinderSourcesGeneratorMojo extends AbstractGeneratorMojo {
@Parameter(required = true, defaultValue = "${project.build.directory}/generated-sources/tinder")
protected File targetDirectory;
@Parameter
private String filenameSuffix = "ResourceProvider";
@Parameter
private String filenamePrefix = "";
@Override
public void doExecute(Configuration configuration) throws MojoExecutionException, MojoFailureException {
File packageDirectoryBase = configuration.getPackageDirectoryBase();
packageDirectoryBase.mkdirs();
ResourceGeneratorUsingModel gen = new ResourceGeneratorUsingModel(configuration.getVersion(), configuration.getBaseDir());
gen.setBaseResourceNames(configuration.getResourceNames());
try {
gen.parse();
gen.setFilenameSuffix(filenameSuffix);
gen.setFilenamePrefix(filenamePrefix);
gen.setTemplate(templateName);
gen.writeAll(packageDirectoryBase, null, configuration.getPackageBase());
} catch (Exception e) {
throw new MojoFailureException("Failed to generate server", e);
}
myProject.addCompileSourceRoot(configuration.getTargetDirectory().getAbsolutePath());
}
@Override
protected File getTargetDirectory() {
return targetDirectory;
}
}

View File

@ -711,7 +711,7 @@ public abstract class BaseStructureParser {
return null;
}
public static FhirVersionEnum determineVersionEnum(String version) throws MojoFailureException {
public static FhirVersionEnum determineVersionEnum(String version) {
FhirVersionEnum versionEnum;
if ("dstu2".equals(version)) {
versionEnum = FhirVersionEnum.DSTU2;
@ -720,7 +720,7 @@ public abstract class BaseStructureParser {
} else if ("r4".equals(version)) {
versionEnum = FhirVersionEnum.R4;
} else {
throw new MojoFailureException("Unknown version: " + version);
throw new IllegalArgumentException("Unknown version: " + version);
}
return versionEnum;
}