|
|
|
|
|
|
Re: "src-gen-once" output directory for impl class stubs [message #754069 is a reply to message #748481] |
Mon, 31 October 2011 16:06 |
Bee Messages: 17 Registered: November 2009 |
Junior Member |
|
|
Hi Matt,
I was looking into this as well the other week, and I indeed implemented a IFileSystemAccess to get this to work. It's really only a quick hack, and I will also wait for 2.1 to do this properly, but it helped me understand how the FileAccess works, maybe that will be useful in the future
Here's my code:
public class MyDslFileSystemAccess extends EclipseResourceFileSystemAccess {
protected static final String ONCE_FOLDER_NAME = "src-once";
// Just a quick hack - need access to the workspace root in here, and it's private in EclipseResourceFileSystemAccess
@Inject
private IWorkspaceRoot rootThis;
public void setRootThis(IWorkspaceRoot root) {
this.rootThis = root;
}
@Override
public void deleteFile(String fileName) {
// Only delete the file if it's not in src-once
if (!fileName.contains(ONCE_FOLDER_NAME)) {
super.deleteFile(fileName);
}
}
@Override
public void generateFile(String fileName, String slot, CharSequence contents) {
if (shouldGenerate(fileName, slot)) {
super.generateFile(fileName, slot, contents);
// Some redundancy here: This is a repeat of what super does to determine the file
// Needs to be done to set the file back to "not derived" so that it can be edited
String outletPath = getPathes().get(slot);
IFile file = rootThis.getFile(new Path(outletPath + "/" + fileName));
if (slot.equals(ONCE_FOLDER_NAME)) {
try {
file.setDerived(false);
} catch (CoreException e) {
e.printStackTrace();
}
}
}
}
private boolean shouldGenerate(String fileName, String slot) {
if (!slot.equals(ONCE_FOLDER_NAME)) {
return true;
}
String outletPath = getPathes().get(slot);
IFile file = rootThis.getFile(new Path(outletPath + "/" + fileName));
if (!file.exists()) {
return true;
}
return false;
}
}
Then I use this FileSystemAccess by extending the JavaProjectBasedBuilderParticipant:
public class MyDslBuilderParticipant extends JavaProjectBasedBuilderParticipant {
@Override
protected IFileSystemAccess getConfiguredFileSystemAccess(IFolder srcGenFolder, IAcceptor<String> newFileAcceptor) {
MyDslFileSystemAccess access = new MyDslFileSystemAccess();
access.setOutputPath(srcGenFolder.getFullPath().toString());
access.setOutputPath(
MyDslFileSystemAccess.ONCE_FOLDER_NAME,
srcGenFolder.getProject().getFolder(MyDslFileSystemAccess.ONCE_FOLDER_NAME)
.getFullPath().toString());
access.setNewFileAcceptor(newFileAcceptor);
access.setRoot(srcGenFolder.getWorkspace().getRoot());
access.setRootThis(srcGenFolder.getWorkspace().getRoot());
return access;
}
}
And then, in my doGenerate method in the xtend file, I do this:
// generate to default (src-gen)
fsa.generateFile(
"Interface.java",
g.compileInterface)
// Generate to src-once
fsa.generateFile(
"ClassImpl.java",
"src-once",
g.compileImpl)
Oh, and of course bind my own builder participant in MyDslUiModule:
public Class<? extends org.eclipse.xtext.builder.IXtextBuilderParticipant> bindIXtextBuilderParticipant() {
return MyDslBuilderParticipant.class;
}
Birgitta
|
|
|
Powered by
FUDForum. Page generated in 0.04187 seconds