use of org.eclipse.vorto.codegen.api.ZipContentExtractCodeGeneratorTask 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.ZipContentExtractCodeGeneratorTask in project vorto by eclipse.
the class CodeGenerationHelper method createEclipseProject.
public static void createEclipseProject(ModelId modelId, String serviceKey, IGenerationResult generatedResult) {
byte[] generated = generatedResult.getContent();
ICodeGeneratorTask<ModelId> task = null;
String name = modelId.getName() + "_" + serviceKey + "_generated";
boolean containsEclipseProject = false;
boolean generateMaven = false;
if (generatedResult.getFileName().endsWith(".zip")) {
ZipContentExtractCodeGeneratorTask tmp = new ZipContentExtractCodeGeneratorTask(generated);
tmp.preprocess();
if (tmp.getEclipseProjectName() != null) {
name = tmp.getEclipseProjectName();
containsEclipseProject = true;
} else {
generateMaven = tmp.isMavenContent();
}
task = tmp;
} else {
task = new SingleFileContentCodeGeneratorTask(generatedResult.getFileName(), generated);
}
EclipseProjectGenerator<ModelId> generator = new EclipseProjectGenerator<>(name);
generator.addTask(task);
generator.generate(modelId, containsEclipseProject ? InvocationContext.simpleInvocationContext(Collections.singletonMap(EclipseProjectGenerator.SKIP_PROJECT_CONFIGURATION, Boolean.TRUE.toString())) : InvocationContext.simpleInvocationContext(), new NullProgressMonitor());
if (generateMaven) {
createMavenProjectFromGeneratedCode(generator);
}
}
use of org.eclipse.vorto.codegen.api.ZipContentExtractCodeGeneratorTask in project vorto by eclipse.
the class GenerationResultBuilder method append.
public GenerationResultBuilder append(IGenerationResult result) {
if (result == null) {
return this;
}
ZipContentExtractCodeGeneratorTask task = new ZipContentExtractCodeGeneratorTask(result.getContent());
task.generate(null, InvocationContext.simpleInvocationContext(), (IGeneratedWriter) this.result);
return this;
}
Aggregations