|
|
Re: Xtext Standalone: Add/Remove/Modify resources [message #1774556 is a reply to message #1774555] |
Tue, 17 October 2017 08:28 |
|
and i did some experiments figuring out what to do manually some years ago:
package org.xtext.example.mydsl;
import java.io.File;
import java.util.Set;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.build.BuildRequest;
import org.eclipse.xtext.build.IncrementalBuilder;
import org.eclipse.xtext.build.IndexState;
import org.eclipse.xtext.mwe.PathTraverser;
import org.eclipse.xtext.resource.IContainer;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.resource.containers.ProjectDescriptionBasedContainerManager;
import org.eclipse.xtext.resource.impl.ChunkedResourceDescriptions;
import org.eclipse.xtext.resource.impl.ProjectDescription;
import org.eclipse.xtext.resource.impl.ResourceDescriptionsData;
import org.eclipse.xtext.service.AbstractGenericModule;
import org.eclipse.xtext.util.UriUtil;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.util.Modules;
public class Main {
public static void main(String[] args) {
// Injector injector = Guice.createInjector(new StandaloneBuilderModule());
// StandaloneBuilder builder = injector.getInstance(StandaloneBuilder.class);
// builder.setClassPathEntries(ImmutableList.of());
// builder.setBaseDir("./spielwiese");
// builder.setSourceDirs(Lists.newArrayList("./spielwiese/src","./spielwiese/src-gen"));
// ILanguageConfiguration cfg = new ILanguageConfiguration() {
//
// @Override
// public boolean isJavaSupport() {
// // TODO Auto-generated method stub
// return false;
// }
//
// @Override
// public String getSetup() {
// return "org.xtext.example.mydsl.MyDslStandaloneSetup";
// }
//
// @Override
// public Set<OutputConfiguration> getOutputConfigurations() {
// OutputConfiguration config = new OutputConfiguration(IFileSystemAccess.DEFAULT_OUTPUT);
// config.setOutputDirectory("src-gen");
// return ImmutableSet.of(config);
// }
// };
// builder.setLanguages(new LanguageAccessFactory().createLanguageAccess(ImmutableList.of(cfg), Main.class.getClassLoader()));
//
//
// builder.launch();
Injector injector = new MyDslStandaloneSetup() {
public Injector createInjector() {
return Guice.createInjector(Modules.override(new MyDslRuntimeModule()).with(new AbstractGenericModule() {
public Class<? extends IContainer.Manager> bindIContainer$Manager() {
return ProjectDescriptionBasedContainerManager.class;
}
}));
}
}
.createInjectorAndDoEMFRegistration();
IResourceServiceProvider resourceServiceProvider = injector.getInstance(IResourceServiceProvider.class);
Injector injector2 = Guice.createInjector(new AbstractGenericModule() {
public Class<? extends IContainer.Manager> bindIContainer$Manager() {
return ProjectDescriptionBasedContainerManager.class;
}
});
IncrementalBuilder incrementalBuilder = injector2.getInstance(IncrementalBuilder.class);
IndexState indexState = injector2.getInstance(IndexState.class);
boolean firstRun = true;
long lastBuild = System.currentTimeMillis();
while (true) {
try {
final boolean firstRunFinal = firstRun;
final long lastBuildFinal = lastBuild;
firstRun = false;
Predicate<URI> isValidPredicate = new Predicate<URI>() {
@Override
public boolean apply(URI input) {
return firstRunFinal || new File(input.toFileString()).lastModified() > lastBuildFinal;
}
};
Set<URI> allChanged = new PathTraverser().findAllResourceUris("spielwiese/src", isValidPredicate);
BuildRequest request = new BuildRequest();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
request.setResourceSet(resourceSet);
request.setBaseDir(UriUtil.createFolderURI(new File("./spielwiese/")));
request.setDirtyFiles(Lists.newArrayList(allChanged));
ResourceDescriptionsData newIndex = indexState.getResourceDescriptions().copy();
ProjectDescription description = new ProjectDescription();
description.setName("demo");
description.attachToEmfObject(resourceSet);
ChunkedResourceDescriptions index = new ChunkedResourceDescriptions(CollectionLiterals.emptyMap(), resourceSet);
index.setContainer("demo", newIndex);
lastBuild = System.currentTimeMillis();
Function1<? super URI, ? extends IResourceServiceProvider> languages = new Function1<URI, IResourceServiceProvider>() {
@Override
public IResourceServiceProvider apply(URI uri) {
return IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(uri);
}
};
request.setState(new IndexState(newIndex, indexState.getFileMappings().copy()));
indexState = incrementalBuilder.build(request, languages).getIndexState();
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Xtext Standalone: Add/Remove/Modify resources [message #1774573 is a reply to message #1774560] |
Tue, 17 October 2017 12:57 |
Oliver Libutzki Messages: 40 Registered: September 2011 |
Member |
|
|
Reading the resources with the IncrementalBuilder works. Now I would like to access EObjects via Xtext's index.
After building I use the indexState to get the first IEObjectDescription. Unfortunately calling getEObjectOrProxy() returns a proxy insted of the resolved EObject.
I wrote a short JUnit Test in order to illustrate this:
import com.google.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.build.AbstractIncrementalBuilderTest
import org.eclipse.xtext.resource.IResourceServiceProvider
import org.eclipse.xtext.resource.IResourceServiceProvider.Registry
import org.junit.Test
import org.junit.runner.RunWith
import static org.junit.Assert.*
@InjectWith(ModulesTestInjectorProvider)
@RunWith(XtextRunner)
class MyIncrementalBuilderTest extends AbstractIncrementalBuilderTest {
@Inject IResourceServiceProvider.Registry registry
override protected Registry getLanguages() {
registry
}
@Test def void test() {
val buildRequest = newBuildRequest [
dirtyFiles = #[
'src/SmokeTestModules.module' - '''
Modules SmokeTestModules {
Module SmokeTestModule {
Predicate TruePredicate true
}
}
'''
]
]
val indexState = build(buildRequest)
val firstElement = indexState.resourceDescriptions.exportedObjects.iterator.next
println("IEObjectDescription: " + firstElement)
println("EObjectOrProxy: " + firstElement.EObjectOrProxy)
assertFalse(firstElement.EObjectOrProxy.eIsProxy)
}
}
The test fails. This is the console output:
IEObjectDescription: org.eclipse.xtext.resource.persistence.SerializableEObjectDescription@5af6e15e
EObjectOrProxy: <somepackage>.impl.ModuleContainerImpl@64610fa (eProxyURI: inmemory:/src/SmokeTestModules.module#/0/@modulecontainer)
Any idea why the proxy cannot be resolved?
[Updated on: Tue, 17 October 2017 12:58] Report message to a moderator
|
|
|
|
|
|
Re: Xtext Standalone: Add/Remove/Modify resources [message #1774581 is a reply to message #1774579] |
Tue, 17 October 2017 13:39 |
Oliver Libutzki Messages: 40 Registered: September 2011 |
Member |
|
|
Christian Dietrich wrote on Tue, 17 October 2017 09:35yes and no. the builder can survive a restart. if you have that index to the disk and load it from there
Ok, I'm fine with SerializableEObjectDescription, but the missing puzzle piece is how to resolve the proxy?
Got it:
val resolvedObject = EcoreUtil.resolve(firstElement.EObjectOrProxy, buildRequest.resourceSet)
[Updated on: Tue, 17 October 2017 13:41] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05645 seconds