Search in sources :

Example 1 with ITransformGoal

use of org.metaborg.core.action.ITransformGoal in project spoofax by metaborg.

the class Builder method transform.

private Collection<T> transform(BuildInput input, ILanguageImpl langImpl, FileObject location, Multimap<IContext, A> allAnalysisUnits, Set<FileName> includeFiles, boolean pardoned, Set<FileName> removedResources, Collection<IMessage> extraMessages, RefBool success, IProgress progress, ICancel cancel) throws InterruptedException {
    final int size = allAnalysisUnits.size();
    progress.setWorkRemaining(size);
    final Collection<T> allTransformUnits = Lists.newArrayListWithCapacity(size);
    if (size == 0) {
        return allTransformUnits;
    }
    progress.setDescription("Compiling " + size + " file(s) of " + langImpl.belongsTo().name());
    logger.debug("Compiling {} analysis results", size);
    for (Entry<IContext, Collection<A>> entry : allAnalysisUnits.asMap().entrySet()) {
        cancel.throwIfCancelled();
        final IContext context = entry.getKey();
        final Iterable<A> analysisResults = entry.getValue();
        try (IClosableLock lock = context.read()) {
            for (A analysisResult : analysisResults) {
                cancel.throwIfCancelled();
                final FileObject source = analysisResult.source();
                final FileName name = source.getName();
                if (removedResources.contains(name) || includeFiles.contains(name)) {
                    // Don't compile removed resources, which the analysis results contain for legacy reasons.
                    // Don't transform included resources, they should just be parsed and analyzed.
                    progress.work(1);
                    continue;
                }
                if (!analysisResult.valid()) {
                    logger.warn("Input result for {} is invalid, cannot transform it", source != null ? source.getName().getPath() : "detached source");
                    progress.work(1);
                    continue;
                }
                for (ITransformGoal goal : input.transformGoals) {
                    cancel.throwIfCancelled();
                    if (!transformService.available(context, goal)) {
                        logger.trace("No {} transformation required for {}", goal, context.language());
                        progress.work(1);
                        continue;
                    }
                    try {
                        final Collection<TA> results = transformService.transform(analysisResult, context, goal);
                        for (TA result : results) {
                            final boolean noErrors = printMessages(result.messages(), goal + " transformation", input, pardoned);
                            success.and(noErrors);
                            @SuppressWarnings("unchecked") final T genericResult = (T) result;
                            allTransformUnits.add(genericResult);
                        }
                        progress.work(1);
                    } catch (TransformException e) {
                        final String message = String.format("Transformation failed unexpectedly for %s", name);
                        logger.error(message, e);
                        final boolean noErrors = printMessage(source, message, e, input, pardoned);
                        success.and(noErrors);
                        extraMessages.add(MessageFactory.newBuilderErrorAtTop(location, "Transformation failed unexpectedly", e));
                    }
                }
            }
        // GTODO: also compile any affected sources
        }
    }
    return allTransformUnits;
}
Also used : IContext(org.metaborg.core.context.IContext) FileName(org.apache.commons.vfs2.FileName) TransformException(org.metaborg.core.transform.TransformException) ITransformGoal(org.metaborg.core.action.ITransformGoal) Collection(java.util.Collection) IClosableLock(org.metaborg.util.concurrent.IClosableLock) FileObject(org.apache.commons.vfs2.FileObject)

Example 2 with ITransformGoal

use of org.metaborg.core.action.ITransformGoal in project spoofax by metaborg.

the class ActionFacetFromESV method create.

@Nullable
public static ActionFacet create(IStrategoAppl esv) {
    final Iterable<IStrategoAppl> menuTerms = ESVReader.collectTerms(esv, "ToolbarMenu");
    final Collection<IMenu> menus = Lists.newLinkedList();
    final Multimap<ITransformGoal, ITransformAction> actions = HashMultimap.create();
    final ImmutableList<String> nesting = ImmutableList.of();
    for (IStrategoAppl menuTerm : menuTerms) {
        final IMenu submenu = menu(menuTerm, new TransformActionFlags(), nesting, actions);
        menus.add(submenu);
    }
    addCompileGoal(esv, actions);
    if (menus.isEmpty() && actions.isEmpty()) {
        return null;
    }
    return new ActionFacet(actions, menus);
}
Also used : IMenu(org.metaborg.core.menu.IMenu) ITransformGoal(org.metaborg.core.action.ITransformGoal) TransformActionFlags(org.metaborg.core.action.TransformActionFlags) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITransformAction(org.metaborg.core.action.ITransformAction) Nullable(javax.annotation.Nullable)

Example 3 with ITransformGoal

use of org.metaborg.core.action.ITransformGoal in project spoofax by metaborg.

the class ActionFacetFromESV method addCompileGoal.

private static void addCompileGoal(IStrategoAppl esv, Multimap<ITransformGoal, ITransformAction> actions) {
    final List<IStrategoAppl> onSaveHandlers = ESVReader.collectTerms(esv, "OnSave");
    if (onSaveHandlers.isEmpty()) {
        return;
    }
    for (IStrategoAppl onSaveHandler : onSaveHandlers) {
        final String strategyName = Tools.asJavaString(onSaveHandler.getSubterm(0).getSubterm(0));
        final ITransformGoal goal = new CompileGoal();
        final ITransformAction action = new TransformAction("Compile", goal, new TransformActionFlags(), strategyName);
        actions.put(goal, action);
    }
}
Also used : CompileGoal(org.metaborg.core.action.CompileGoal) ITransformGoal(org.metaborg.core.action.ITransformGoal) TransformActionFlags(org.metaborg.core.action.TransformActionFlags) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITransformAction(org.metaborg.core.action.ITransformAction) ITransformAction(org.metaborg.core.action.ITransformAction)

Aggregations

ITransformGoal (org.metaborg.core.action.ITransformGoal)3 ITransformAction (org.metaborg.core.action.ITransformAction)2 TransformActionFlags (org.metaborg.core.action.TransformActionFlags)2 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 Collection (java.util.Collection)1 Nullable (javax.annotation.Nullable)1 FileName (org.apache.commons.vfs2.FileName)1 FileObject (org.apache.commons.vfs2.FileObject)1 CompileGoal (org.metaborg.core.action.CompileGoal)1 IContext (org.metaborg.core.context.IContext)1 IMenu (org.metaborg.core.menu.IMenu)1 TransformException (org.metaborg.core.transform.TransformException)1 IClosableLock (org.metaborg.util.concurrent.IClosableLock)1