use of com.devonfw.cobigen.api.exception.CobiGenCancellationException in project cobigen by devonfw.
the class GenerationProcessorImpl method generate.
@Override
public GenerationReportTo generate(Object input, List<? extends GenerableArtifact> generableArtifacts, Path targetRootPath, boolean forceOverride, Map<String, Object> rawModel, BiConsumer<String, Integer> progressCallback) {
InputValidator.validateInputsUnequalNull(input, generableArtifacts);
List<Class<?>> logicClasses = null;
// only implicit dependency to javaplugin to lower classloader complexity
ClassLoader inputProjectClassLoader = null;
if (input instanceof Class) {
inputProjectClassLoader = ((Class<?>) input).getClassLoader();
} else if (input instanceof Object[]) {
for (Object obj : (Object[]) input) {
if (obj instanceof Class) {
inputProjectClassLoader = ((Class<?>) obj).getClassLoader();
}
}
}
Path templateConfigPath = Paths.get(this.configurationHolder.getConfigurationLocation());
progressCallback.accept("Prepend Templates Classloader", 10);
inputProjectClassLoader = prependTemplatesClassloader(templateConfigPath, inputProjectClassLoader);
if (inputProjectClassLoader != null) {
try {
logicClasses = ConfigurationClassLoaderUtil.resolveUtilClasses(this.configurationHolder, inputProjectClassLoader);
} catch (IOException e) {
LOG.error("An IOException occured while resolving utility classes!", e);
}
}
// initialize
this.forceOverride = forceOverride;
this.input = input;
if (logicClasses != null) {
progressCallback.accept("Load Template logic classes", 20);
loadLogicClasses(progressCallback, logicClasses);
}
progressCallback.accept("Create Temporary Target Directory", 40);
this.rawModel = rawModel;
try {
this.tmpTargetRootPath = Files.createTempDirectory("cobigen-");
LOG.info("Temporary working directory: {}", this.tmpTargetRootPath);
} catch (IOException e) {
throw new CobiGenRuntimeException("Could not create temporary folder.", e);
}
this.targetRootPath = targetRootPath;
this.generationReport = new GenerationReportTo();
progressCallback.accept("Load templates", 50);
LOG.debug("Collecting templates");
Collection<TemplateTo> templatesToBeGenerated = flatten(generableArtifacts);
// generate
Map<File, File> origToTmpFileTrace = Maps.newHashMap();
try {
LOG.debug("Generating {} templates", templatesToBeGenerated.size());
for (TemplateTo template : templatesToBeGenerated) {
try {
Trigger trigger = this.configurationHolder.readContextConfiguration().getTrigger(template.getTriggerId());
TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
InputValidator.validateTriggerInterpreter(triggerInterpreter, trigger);
progressCallback.accept("Generating " + template.getId(), Math.round(1 / (float) templatesToBeGenerated.size() * 800));
generate(template, triggerInterpreter, origToTmpFileTrace, progressCallback);
} catch (CobiGenCancellationException e) {
throw (e);
} catch (CobiGenRuntimeException e) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
this.generationReport.addError(e);
} catch (Throwable e) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
this.generationReport.addError(new CobiGenRuntimeException("Something unexpected happened" + ((e.getMessage() != null) ? ": " + e.getMessage() : "!"), e));
}
}
} catch (CobiGenCancellationException e) {
LOG.error("the Generation has been Canceled.", e);
this.generationReport.setCancelled(true);
}
if (this.generationReport.isCancelled()) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
this.tmpTargetRootPath.toFile().deleteOnExit();
// do nothing if cancelled
} else if (this.generationReport.isSuccessful()) {
try {
for (Entry<File, File> origToTmpFile : origToTmpFileTrace.entrySet()) {
Files.createDirectories(origToTmpFile.getKey().toPath().getParent());
Files.copy(origToTmpFile.getValue().toPath(), origToTmpFile.getKey().toPath(), StandardCopyOption.REPLACE_EXISTING);
this.generationReport.addGeneratedFile(origToTmpFile.getKey().toPath());
}
this.tmpTargetRootPath.toFile().deleteOnExit();
} catch (IOException e) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
throw new CobiGenRuntimeException("Could not copy generated files to target location!", e);
}
} else {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
LOG.warn("Generation finished non-successful. Generated contents can be reviewed in " + this.tmpTargetRootPath.toUri());
}
return this.generationReport;
}
Aggregations