Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » "src-gen-once" output directory for impl class stubs
"src-gen-once" output directory for impl class stubs [message #748133] Mon, 24 October 2011 14:07 Go to next message
Matt Campbell is currently offline Matt CampbellFriend
Messages: 11
Registered: April 2011
Junior Member
In our Xtext 1.x projects, we always had a 'src-gen-once' outlet that wouldn't overwrite resources that already existed. We used this so that we could generate stubs for implementation classes that that the compiler would then say "hey, this class is missing some methods".

Looking around the forums, I see that the protected region concept isn't supported and I wondered if this is related to that? We tend not to use the protected regions, but I don't see any mechanism for specifying a second output directory and whether or not to overwrite a file if it already exists. Can someone point me at a way to generate something similar in Xtext 2 with Xtend 2.0?

I know its really just a convenience, but not having to make sure the developers know to go create the Impl class manually after generation has saved us some headache in the past. Thanks!
Re: "src-gen-once" output directory for impl class stubs [message #748317 is a reply to message #748133] Mon, 24 October 2011 16:21 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Xtext 2 does a very nice job of partitioning the 100% recreated src-gen
folder from the initialised once src folder, in which the are empty
derivations for the interesting classes.

I find it very easy to only edit src and never edit src-gen; if you need
to edit src-gen then write a Fragment.

The only minor limitation is the shared maintenance of MANIFEST.MF and
plugin.xml for which an autogenerated ..._gen sometimes need merging. It
would be nice if somehow the _gen files had warnings to highlight
inconsistencies.

Regards

Ed Willink



On 24/10/2011 15:07, Matt Campbell wrote:
> In our Xtext 1.x projects, we always had a 'src-gen-once' outlet that
> wouldn't overwrite resources that already existed. We used this so
> that we could generate stubs for implementation classes that that the
> compiler would then say "hey, this class is missing some methods".
>
> Looking around the forums, I see that the protected region concept
> isn't supported and I wondered if this is related to that? We tend
> not to use the protected regions, but I don't see any mechanism for
> specifying a second output directory and whether or not to overwrite a
> file if it already exists. Can someone point me at a way to generate
> something similar in Xtext 2 with Xtend 2.0?
>
> I know its really just a convenience, but not having to make sure the
> developers know to go create the Impl class manually after generation
> has saved us some headache in the past. Thanks!
Re: "src-gen-once" output directory for impl class stubs [message #748401 is a reply to message #748317] Mon, 24 October 2011 17:21 Go to previous messageGo to next message
Matt Campbell is currently offline Matt CampbellFriend
Messages: 11
Registered: April 2011
Junior Member
Yep - that's the behavior that I want, but how to I generate stuff into the src folder? For example, say I've got a grammar defining Resources that can have a bunch of available actions. I've got a model that ends up looking like:
resource MyResource {
    action list
    action inputData
    action test
    action generateData
    action sendTo
    ...
}


I can write an Xtend 2.0 template that gives me a java interface and abstract class representing a Resource, with its actions as methods. What I'd like to do is also generate a MyResourceImpl class that implements that generated interface if it doesn't already exist. When I regenerate in the future, if I've added actions it will break my Impl class because it will no longer implement the actions specified in the model and generated into the interface. I could go an make this Impl class by hand, but we're looking at defining models with lots of resources potentially and I'd like it generate the stubs. If we add twenty new resources to an already large model, I'd like there to be twenty breaking classes that tell the developer what needs their attention.

TL;DR version: Is there a way to have the MWE2 workflow or the eclipse auto-generate-on-save generate classes to the src directory, and if so, can I make sure it doesn't clobber files that already exist? Thanks for the help!
Re: "src-gen-once" output directory for impl class stubs [message #748471 is a reply to message #748401] Mon, 24 October 2011 18:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

Xtext 2.1 (coming 2. November) will bring a more sophisticed outlet support as you know it from Xpand.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: "src-gen-once" output directory for impl class stubs [message #748476 is a reply to message #748471] Mon, 24 October 2011 18:09 Go to previous messageGo to next message
Matt Campbell is currently offline Matt CampbellFriend
Messages: 11
Registered: April 2011
Junior Member
Excellent - I had heard that from another forum post so I knew this was being worked on, but I wanted to see if anyone had a workaround for the time being with 2.0.1. Thanks for the info, and we'll check back in with 2.1 next month.
Re: "src-gen-once" output directory for impl class stubs [message #748481 is a reply to message #748476] Mon, 24 October 2011 18:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

what about using a custom org.eclipse.xtext.generator.IFileSystemAccess

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: "src-gen-once" output directory for impl class stubs [message #754069 is a reply to message #748481] Mon, 31 October 2011 16:06 Go to previous message
Bee Friend
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 Smile

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
Previous Topic:ConcurrentModificationException
Next Topic:How to get Ecore file from concrete xtext model file?
Goto Forum:
  


Current Time: Fri Apr 26 08:47:52 GMT 2024

Powered by FUDForum. Page generated in 0.04187 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top