Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830498] |
Mon, 27 July 2020 20:51 |
Ben Holland Messages: 34 Registered: April 2019 |
Member |
|
|
Hello my apologies for asking what is probably a duplicate question but after lots of searching I have not found the answer myself.
I have a DSL that leverages XBase for code generation to Java source. In Eclipse I setup an XText project that has additional a) Java source and b) Jar library dependencies. I am making a standalone compiler now and I know I need to populate the resource set myself because I will not be using Eclipse to do it. How can I create the resources for a) everything in Jar libraries and b) additional Java source files (originally provided in the project)?
Here is what I have tried so far, with TODOs where I am unsure what to do next. Any advice is very welcome!
// dslFile is a .dsl file (loading this resource works as expected)
public static void generateCode(File dslFile) throws IOException {
try {
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
File myDslRuntime = new File("MyDslRuntime.jar");
// TODO: how to load resources from jar file???? a jar handler is not defined
resourceSet.getResource(URI.createFileURI(myDslRuntime.getAbsolutePath()), true);
File javaSourceFiles = new File("input-path/src");
Arrays.stream(javaSourceFiles.listFiles()).filter(file -> file.isFile() && file.getName().endsWith(".java")).forEach(file -> {
// TODO: how to load resources from java source file???? a .java handler is not defined
resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
});
XtextResource resource = (XtextResource) resourceSet.getResource(URI.createFileURI(source.getAbsolutePath()), true);
// TODO: is there a better way to create a simple FSA?
class FSA implements IFileSystemAccess {
private File root;
public FSA(File root) {
this.root = root;
this.root.mkdirs();
}
@Override
public void generateFile(String fileName, CharSequence contents) {
this.generateFile(fileName, null, contents);
}
@Override
public void generateFile(String fileName, String outputConfigurationName, CharSequence contents) {
File file = new File(root.getAbsolutePath() + File.separator + fileName);
file.getParentFile().mkdirs();
try {
FileWriter fw = new FileWriter(file);
fw.write(contents.toString());
fw.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteFile(String fileName) {
File file = new File(root.getAbsolutePath() + File.separator + fileName);
if(file.exists()) {
file.delete();
}
}
}
// this works but fails when it cannot find the types in the runtime/Java source resources
MyDslGenerators generators = injector.getInstance(MyDslGenerators.class);
generators.doGenerate(resource, new FSA(new File("output-path/src-gen/")));
} catch (Exception e) {
e.printStackTrace();
}
}
[Updated on: Mon, 27 July 2020 21:49] Report message to a moderator
|
|
|
Re: Standalone XBase DSl with Compile Time Java Source (.java) and Jar (.class file) Dependencies [message #1830500 is a reply to message #1830498] |
Mon, 27 July 2020 21:24 |
Ben Holland Messages: 34 Registered: April 2019 |
Member |
|
|
In case my question wasn't clear I want to implement the following TODOs.
private static void loadFiles(File directory, XtextResourceSet resourceSet) {
Arrays.stream(directory.listFiles()).forEach(file -> {
if(file.isDirectory()) {
loadFiles(file, resourceSet);
} else if (file.getName().endsWith(".dsl")){
loadDSLFile(resourceSet, file);
} else if (file.getName().endsWith(".java")){
// TODO: how to load java source resources?
} else if (file.getName().endsWith(".jar")){
// TODO: how to load jar resources?
} else {
throw new IllegalArgumentException("Unhandled resource type");
}
});
}
private static void loadDSLFile(XtextResourceSet resourceSet, File file) {
System.out.println("Loading: " + file.getAbsolutePath());
resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
}
P.S. Right after I posted I noticed another example used the JavaIoFileSystemAccess which works beautifully, so now I just to figure out how to load .java and .jar resources properly.
JavaIoFileSystemAccess fsa = injector.getInstance(JavaIoFileSystemAccess.class);
fsa.setOutputPath("output-path/src-gen/");
[Updated on: Mon, 27 July 2020 21:48] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02185 seconds