|
|
TODO |
|
|
\ No newline at end of file |
|
|
# Create Logic
|
|
|
|
|
|
```Java
|
|
|
package org.xtext.example.mydsl.cli;
|
|
|
|
|
|
import com.google.inject.Inject;
|
|
|
import com.google.inject.Injector;
|
|
|
import com.google.inject.Provider;
|
|
|
import java.util.Map;
|
|
|
import org.eclipse.emf.ecore.resource.ResourceSet;
|
|
|
import org.eclipse.equinox.app.IApplication;
|
|
|
import org.eclipse.equinox.app.IApplicationContext;
|
|
|
import org.eclipse.xtext.generator.GeneratorDelegate;
|
|
|
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
|
|
|
import org.eclipse.xtext.validation.IResourceValidator;
|
|
|
import org.xtext.example.mydsl.MyDslStandaloneSetup;
|
|
|
|
|
|
/**
|
|
|
* This class controls all aspects of the application's execution
|
|
|
*/
|
|
|
public class Application implements IApplication {
|
|
|
|
|
|
@Inject
|
|
|
private Provider<ResourceSet> resourceSetProvider;
|
|
|
|
|
|
@Inject
|
|
|
private IResourceValidator validator;
|
|
|
|
|
|
@Inject
|
|
|
private GeneratorDelegate generator;
|
|
|
|
|
|
@Inject
|
|
|
private JavaIoFileSystemAccess fileAccess;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Object start(IApplicationContext context) throws Exception {
|
|
|
final Map<?, ?> args = context.getArguments();
|
|
|
final String[] appArgs = (String[]) args.get("application.args");
|
|
|
|
|
|
final Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
|
|
|
System.out.println("Hello");
|
|
|
MyGenerator.execute(appArgs[1]);
|
|
|
|
|
|
return IApplication.EXIT_OK;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void stop() {
|
|
|
// nothing to do
|
|
|
}
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
```Java
|
|
|
|
|
|
mport java.io.File;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
import com.google.inject.Inject;
|
|
|
import com.google.inject.Injector;
|
|
|
import com.google.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.generator.GeneratorContext;
|
|
|
import org.eclipse.xtext.generator.GeneratorDelegate;
|
|
|
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
|
|
|
import org.eclipse.xtext.util.CancelIndicator;
|
|
|
import org.eclipse.xtext.validation.CheckMode;
|
|
|
import org.eclipse.xtext.validation.IResourceValidator;
|
|
|
import org.eclipse.xtext.validation.Issue;
|
|
|
import org.xtext.example.mydsl.MyDslStandaloneSetup;
|
|
|
import org.xtext.example.mydsl.generator.MyDslGenerator;
|
|
|
|
|
|
public class MyGenerator {
|
|
|
|
|
|
@Inject
|
|
|
private Provider<ResourceSet> resourceSetProvider;
|
|
|
|
|
|
@Inject
|
|
|
private IResourceValidator validator;
|
|
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
private JavaIoFileSystemAccess fileAccess;
|
|
|
|
|
|
public static void execute(String path) {
|
|
|
final Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
|
|
|
final MyGenerator main = injector.getInstance(MyGenerator.class);
|
|
|
main.runGenerator(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
protected void runGenerator(String path) {
|
|
|
// Load the resources
|
|
|
final ResourceSet resourceSet = this.resourceSetProvider.get();
|
|
|
|
|
|
final Resource resource = resourceSet.getResource(URI.createFileURI(path),
|
|
|
true);
|
|
|
|
|
|
// Validate the resource
|
|
|
final List<Issue> list = this.validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
|
|
|
if (!list.isEmpty()) {
|
|
|
for (final Issue issue : list) {
|
|
|
System.err.println(issue);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
MyDslGenerator myDslGenerator = new MyDslGenerator();
|
|
|
// Configure and start the generator
|
|
|
myDslGenerator.doGenerate(resource, this.fileAccess, null);
|
|
|
|
|
|
System.out.println("Code generation finished.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
``` |
|
|
\ No newline at end of file |