Search in sources :

Example 1 with CobiGenCancellationException

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;
}
Also used : Path(java.nio.file.Path) TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) CobiGenCancellationException(com.devonfw.cobigen.api.exception.CobiGenCancellationException) IOException(java.io.IOException) Entry(java.util.Map.Entry) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) URLClassLoader(java.net.URLClassLoader) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Aggregations

CobiGenCancellationException (com.devonfw.cobigen.api.exception.CobiGenCancellationException)1 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)1 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)1 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)1 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)1 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1 Entry (java.util.Map.Entry)1