Skip to content

Commit

Permalink
Workaround for switch issue (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCockx authored Jan 8, 2025
1 parent 1428d73 commit 0dd97e3
Showing 1 changed file with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,47 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.inject.Inject;
import javax.inject.Provider;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.builder.standalone.LanguageAccess;
import org.eclipse.xtext.builder.standalone.StandaloneBuilder;
import org.eclipse.xtext.common.types.access.impl.IndexedJvmTypeAccess;
import org.eclipse.xtext.generator.GeneratorContext;
import org.eclipse.xtext.generator.GeneratorDelegate;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.resource.clustering.DisabledClusteringPolicy;
import org.eclipse.xtext.resource.clustering.DynamicResourceClusteringPolicy;
import org.eclipse.xtext.resource.clustering.IResourceClusteringPolicy;
import org.eclipse.xtext.resource.impl.ResourceDescriptionsData;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.regnosys.rosetta.generator.RosettaGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RosettaStandaloneBuilder extends StandaloneBuilder {
private static final Logger LOG = LoggerFactory.getLogger(RosettaStandaloneBuilder.class);

@Inject
private Provider<XtextResourceSet> resourceSetProvider;
@Inject
private IndexedJvmTypeAccess jvmTypeAccess;

private LanguageAccess rosettaLanguageAccess = null;
// TODO: patch Xtext to make `languages` available
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -78,7 +100,111 @@ private RosettaGenerator getRosettaGenerator() {
@Override
public boolean launch() {
needsBeforeAllCall = true;
boolean success = super.launch();

// START COPY PASTE OF ORIGINAL IMPLEMENTATION
boolean needsJava = IterableExtensions.exists(getLanguages().values(), l -> l.isLinksAgainstJava());
if (getBaseDir() == null) {
setBaseDir(System.getProperty("user.dir"));
LOG.warn("Property baseDir not set. Using '" + getBaseDir() + "'");
}
if (needsJava) {
LOG.info("Using common types.");
}
XtextResourceSet resourceSet = resourceSetProvider.get();
if (getEncoding() != null) {
forceDebugLog("Setting encoding.");
fileEncodingSetup(getLanguages().values(), getEncoding());
}
LOG.info("Collecting source models.");
long startedAt = System.currentTimeMillis();
Iterable<String> rootsToTravers = getClassPathEntries();
if (getClassPathLookUpFilter() != null) {
LOG.info("Class path look up filter is active.");
Pattern cpLookUpFilter = Pattern.compile(getClassPathLookUpFilter());
rootsToTravers = Iterables.filter(getClassPathEntries(), root -> cpLookUpFilter.matcher(root).matches());
LOG.info("Investigating " + Iterables.size(rootsToTravers) + " of " + Iterables.size(getClassPathEntries())
+ " class path entries.");
}
List<URI> sourceResourceURIs = collectResources(getSourceDirs(), resourceSet);
// PATCHED THE FOLLOWING LINE TO WORKAROUND ISSUE: https://github.com/finos/rune-dsl/issues/878
Iterable<URI> allResourcesURIs = Iterables.concat(collectResources(rootsToTravers, resourceSet), sourceResourceURIs);
forceDebugLog("Finished collecting source models. Took: " + (System.currentTimeMillis() - startedAt) + " ms.");
Iterable<String> allClassPathEntries = Iterables.concat(getSourceDirs(), getClassPathEntries());
if (needsJava) {
LOG.info("Installing type provider.");
installTypeProvider(allClassPathEntries, resourceSet, null);
}
IResourceClusteringPolicy strategy = null;
if (getClusteringConfig() != null) {
LOG.info("Clustering configured.");
DynamicResourceClusteringPolicy dynamicResourceClusteringPolicy = new DynamicResourceClusteringPolicy();
// Convert MB to byte to make it easier for the user
dynamicResourceClusteringPolicy.setMinimumFreeMemory(getClusteringConfig().getMinimumFreeMemory() * 1024 * 1024);
dynamicResourceClusteringPolicy.setMinimumClusterSize(getClusteringConfig().getMinimumClusterSize());
dynamicResourceClusteringPolicy.setMinimumPercentFreeMemory(getClusteringConfig().getMinimumPercentFreeMemory());
strategy = dynamicResourceClusteringPolicy;
} else {
strategy = new DisabledClusteringPolicy();
}
// Fill index
ResourceDescriptionsData index = new ResourceDescriptionsData(new ArrayList<>());
Iterator<URI> allResourceIterator = allResourcesURIs.iterator();
while (allResourceIterator.hasNext()) {
List<Resource> resources = new ArrayList<>();
int clusterIndex = 0;
boolean canContinue = true;
while (allResourceIterator.hasNext() && canContinue) {
URI uri = allResourceIterator.next();
Resource resource = resourceSet.getResource(uri, true);
resources.add(resource);
fillIndex(uri, resource, index);
clusterIndex++;
if (!strategy.continueProcessing(resourceSet, null, clusterIndex)) {
canContinue = false;
}
}
if (!canContinue) {
clearResourceSet(resourceSet);
}
}
installIndex(resourceSet, index);
// Generate Stubs
if (needsJava) {
String stubsClasses = compileStubs(generateStubs(index, sourceResourceURIs));
LOG.info("Installing type provider for stubs.");
installTypeProvider(Iterables.concat(allClassPathEntries, Lists.newArrayList(stubsClasses)), resourceSet,
jvmTypeAccess);
}
// Validate and generate
LOG.info("Validate and generate.");
Iterator<URI> sourceResourceIterator = sourceResourceURIs.iterator();
boolean hasValidationErrors = false;
while (sourceResourceIterator.hasNext()) {
List<Resource> resources = new ArrayList<>();
int clusterIndex = 0;
boolean canContinue = true;
while (sourceResourceIterator.hasNext() && canContinue) {
URI uri = sourceResourceIterator.next();
Resource resource = resourceSet.getResource(uri, true);
resources.add(resource);
resource.getContents(); // full initialize
EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
hasValidationErrors = !validate(resource) || hasValidationErrors;
clusterIndex++;
if (!strategy.continueProcessing(resourceSet, null, clusterIndex)) {
canContinue = false;
}
}
if (isFailOnValidationError() && hasValidationErrors) {
return !hasValidationErrors;
}
generate(resources);
if (!canContinue) {
clearResourceSet(resourceSet);
}
}
boolean success = !hasValidationErrors;
// END COPY PASTE OF ORIGINAL IMPLEMENTATION

LOG.info("Starting after all generation");
GeneratorContext context = new GeneratorContext();
Expand Down

0 comments on commit 0dd97e3

Please sign in to comment.