use of org.eclipse.vorto.codegen.api.Generated in project vorto by eclipse.
the class GeneratorMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Executing Generator of class '" + getGeneratorClass());
try {
final IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(loadInformationModels()))).read();
List<MappingModel> mappingModels = workspace.get().stream().filter(p -> p instanceof MappingModel).map(MappingModel.class::cast).collect(Collectors.toList());
for (Model model : workspace.get().stream().filter(p -> p instanceof InformationModel).collect(Collectors.toList())) {
InformationModel infomodel = (InformationModel) model;
IVortoCodeGenerator codeGenerator = (IVortoCodeGenerator) Class.forName(generatorClass).newInstance();
IGenerationResult result = codeGenerator.generate(infomodel, new InvocationContext(mappingModels, null, new HashMap<String, String>()), null);
if (result.getMediatype().equalsIgnoreCase("application/zip")) {
final ZipContentExtractCodeGeneratorTask task = new ZipContentExtractCodeGeneratorTask(result.getContent());
task.generate(null, InvocationContext.simpleInvocationContext(), new IGeneratedWriter() {
public void write(Generated generated) {
if (generated.getFileName() == null) {
File generatedDirectory = new File(outputPath, stripPath(generated.getFolderPath()));
generatedDirectory.mkdirs();
} else {
if (generated.getFileName().equals("pom.xml")) {
return;
}
File generatedDirectory = new File(outputPath, stripPath(generated.getFolderPath()));
File generatedFile = new File(generatedDirectory, generated.getFileName());
try {
FileUtils.writeByteArrayToFile(generatedFile, generated.getContent(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String stripPath(String folderPath) {
final String mavenSourcePath = "src/main/java/";
if (folderPath.indexOf(mavenSourcePath) > -1) {
return folderPath.substring(folderPath.indexOf(mavenSourcePath) + mavenSourcePath.length());
}
return folderPath;
}
});
} else {
File generatedFile = new File(outputPath, result.getFileName());
FileUtils.writeByteArrayToFile(generatedFile, result.getContent(), false);
}
}
} catch (InstantiationException e) {
throw new MojoExecutionException("Could not instantiate vorto code generator from given generatorClass", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Error during resolving code generator", e);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Could not instantiate vorto code generator from given generatorClass", e);
} catch (Exception e) {
throw new MojoExecutionException("Problem during code generator invocation", e);
}
}
use of org.eclipse.vorto.codegen.api.Generated in project vorto by eclipse.
the class CodeGenerationController method generate.
@RequestMapping(value = "/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> generate(@PathVariable String namespace, @PathVariable String name, @PathVariable String version, final HttpServletRequest request) {
byte[] modelResources = downloadModelWithReferences(namespace, name, version);
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelResources))).read();
Model model = workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get();
InformationModel infomodel = null;
if (model instanceof InformationModel) {
infomodel = (InformationModel) model;
} else if (model instanceof FunctionblockModel) {
infomodel = Utils.wrapFunctionBlock((FunctionblockModel) model);
}
IGenerationResult result = null;
try {
Map<String, String> requestParams = new HashMap<>();
request.getParameterMap().entrySet().stream().forEach(x -> requestParams.put(x.getKey(), x.getValue()[0]));
result = vortoGenerator.generate(infomodel, createInvocationContext(infomodel, vortoGenerator.getServiceKey(), requestParams), null);
} catch (Exception e) {
GenerationResultZip output = new GenerationResultZip(infomodel, vortoGenerator.getServiceKey());
Generated generated = new Generated("generation_error.log", "/generated", e.getMessage());
output.write(generated);
result = output;
}
return ResponseEntity.ok().contentLength(result.getContent().length).header("content-disposition", "attachment; filename = " + result.getFileName()).contentType(MediaType.parseMediaType(result.getMediatype())).body(new InputStreamResource(new ByteArrayInputStream(result.getContent())));
}
use of org.eclipse.vorto.codegen.api.Generated in project vorto by eclipse.
the class PluginBuildFileModule method generate.
public void generate(Context metaData, InvocationContext invocationContext, IGeneratedWriter outputter) {
Generated generatedBuildFile = new Generated(FILE_NAME, null, template.getContent(metaData, invocationContext));
outputter.write(generatedBuildFile);
}
use of org.eclipse.vorto.codegen.api.Generated in project vorto by eclipse.
the class PomFileModule method generate.
public void generate(Context model, InvocationContext invocationContext, IGeneratedWriter outputter) {
Generated generatedPom = new Generated(FILE_NAME, null, template.getContent(model, invocationContext));
outputter.write(generatedPom);
}
use of org.eclipse.vorto.codegen.api.Generated in project vorto by eclipse.
the class CopyResourceTask method generate.
public void generate(Context metaData, InvocationContext invocationContext, final IGeneratedWriter outputter) {
try {
Path start = Paths.get(basePath.toURI());
Files.walkFileTree(start, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
outputter.write(new Generated(file.getFileName().toFile().getName(), getOutputPath(file).isEmpty() ? null : getOutputPath(file), FileUtils.readFileToByteArray((file.toFile()))));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException ioEx) {
throw new RuntimeException(ioEx);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Aggregations