Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xtext 2.0] protected region support?(How to migrate a Xpand-based generator with protected regions?)
[xtext 2.0] protected region support? [message #690241] Wed, 29 June 2011 05:00 Go to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
Hello,
in my Xtext 1 generator written in Xpand, I used the protected region functionality.
I can't seem to find how to migrate this functionality into Xtext 2 (or Xtend 2, for that matter).
Can anyone help?
Thanks
Mio
(no subject) [message #690394 is a reply to message #690241] Wed, 29 June 2011 08:55 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven Efftinge
Messages: 1761
Registered: July 2009
Senior Member
No, there is no such thing, but since in Xtend 2 the generated text is
not passed somewhere behind the scenes but is just really a CharSequence
you can build your own protected regions mechanism.

Am 6/29/11 11:00 AM, schrieb forums-noreply@eclipse.org:
> Hello,
> in my Xtext 1 generator written in Xpand, I used the protected region
> functionality.
> I can't seem to find how to migrate this functionality into Xtext 2 (or
> Xtend 2, for that matter).
> Can anyone help?
> Thanks Mio


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de


--
Need professional support on Xtext or Xtend?
Mail to: xtext (at) itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Re: (no subject) [message #690597 is a reply to message #690394] Wed, 29 June 2011 15:40 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
Hm, ok...
But, how do I get to the contents of already generated files from the Xtend2 template then?
Aren't there any plans to support something like protected regions in next releases of Xtext 2 directly inside the framework?
Thanks for the replies
Mio
Re: (no subject) [message #690633 is a reply to message #690597] Wed, 29 June 2011 17:14 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven Efftinge
Messages: 1761
Registered: July 2009
Senior Member
Am 6/29/11 9:40 PM, schrieb forums-noreply@eclipse.org:
> Hm, ok... But, how do I get to the contents of already generated files
> from the Xtend2 template then? Aren't there any plans to support
> something like protected regions in next releases of Xtext 2 directly
> inside the framework?
> Thanks for the replies
> Mio

In Xtend 2 a fucntion using a template expression really jsut returns a
CharSequence so you can easily postprocess it (i.e. replace a certain
marker with your protected regions contents).
We don't have plans to add support for it.
Sven

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de


--
Need professional support on Xtext or Xtend?
Mail to: xtext (at) itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Re: (no subject) [message #690995 is a reply to message #690633] Thu, 30 June 2011 10:15 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
Well, I understand that, the problem is, how do I retrieve the protected regions contents from previously generated files?
The IFileSystemAccess where I would expect it to be does not have such a function...
Re: (no subject) [message #690998 is a reply to message #690995] Thu, 30 June 2011 10:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Hi,

you have to do this yourself Sad

~Christian
Re: (no subject) [message #691001 is a reply to message #690998] Thu, 30 June 2011 10:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Btw did you consider other approaches like GenerationGap Pattern?

~Christian
Re: (no subject) [message #691002 is a reply to message #690998] Thu, 30 June 2011 10:27 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
I know I have to do the parsing myself, I just meant it would be nice to have something like

public String getFileContents(String fileName) {
...
}

inside the IFileSystemAccess interface...

Anyway, is it sufficient to use File with a relative path inside the Xtend2 template , i.e. will the base directory always be the target directory where the resources are generated?
Or, how do I retrieve the base dir?

Thanks
Re: (no subject) [message #691022 is a reply to message #691001] Thu, 30 June 2011 11:04 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
Generation Gap - that might be tricky, as we are not generating Java code (or any other OO language for that matter)...

[Updated on: Thu, 30 June 2011 11:05]

Report message to a moderator

Re: (no subject) [message #691029 is a reply to message #691022] Thu, 30 June 2011 11:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
So why don't you simply use good old Xpand 1.1.0?
Re: (no subject) [message #691035 is a reply to message #691029] Thu, 30 June 2011 11:20 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
I didn't know that was possible with Xtext 2.0.0, I thought you have to migrate to Xtend2...
Anyway, I'd still like to know how to get the path to the previously generated file (or its contents) in Xtend2 (when using the JavaProjectBasedBuilderParticipant). Is there any way to achieve this (other than hardcoding the path inside the Xtend2 template)?
Thank you

[Updated on: Fri, 01 July 2011 10:13]

Report message to a moderator

Re: (no subject) [message #692051 is a reply to message #690633] Sun, 03 July 2011 06:32 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

Sven Efftinge wrote on Thu, 30 June 2011 04:14
Am 6/29/11 9:40 PM, schrieb forums-noreply@eclipse.org:
> Hm, ok... But, how do I get to the contents of already generated files
> from the Xtend2 template then? Aren't there any plans to support
> something like protected regions in next releases of Xtext 2 directly
> inside the framework?
> Thanks for the replies
> Mio

In Xtend 2 a fucntion using a template expression really jsut returns a
CharSequence so you can easily postprocess it (i.e. replace a certain
marker with your protected regions contents).
We don't have plans to add support for it.
Sven


Aw.. Sad

Do you have plans to support IFileSystemAccess::getFileContents() ?
Re: (no subject) [message #692057 is a reply to message #692051] Sun, 03 July 2011 07:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Hi,

what about filing a enhancement request for this into bugzilla

~Christian
Re: (no subject) [message #692069 is a reply to message #692051] Sun, 03 July 2011 07:30 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven Efftinge
Messages: 1761
Registered: July 2009
Senior Member
Am 7/3/11 12:32 PM, schrieb Hendy Irawan:
> Sven Efftinge wrote on Thu, 30 June 2011 04:14
>> Am 6/29/11 9:40 PM, schrieb forums-noreply@eclipse.org:
>> > Hm, ok... But, how do I get to the contents of already generated files
>> > from the Xtend2 template then? Aren't there any plans to support
>> > something like protected regions in next releases of Xtext 2 directly
>> > inside the framework?
>> > Thanks for the replies
>> > Mio
>>
>> In Xtend 2 a fucntion using a template expression really jsut returns a
>> CharSequence so you can easily postprocess it (i.e. replace a certain
>> marker with your protected regions contents).
>> We don't have plans to add support for it.
>> Sven
>
>
> Aw.. :(
>
> Do you have plans to support IFileSystemAccess::getFileContents() ?
>

You could use your own implementation of IFileSystemAccess in order to
process the contents. Just add different guice binding.
Because that might propable something a couple of people will do, it's
good to keep the interface small.

Sven

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de


--
Need professional support on Xtext or Xtend?
Mail to: xtext (at) itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Re: (no subject) [message #693402 is a reply to message #692069] Wed, 06 July 2011 08:01 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing name
Messages: 13
Registered: June 2010
Junior Member
Hm, but because JavaProjectBasedBuilderParticipant uses a different file system access than is used in the Java-based or MWE2-based generators, having such a function in their common ancestor makes sense...

Anyway, how about providing a method for returning the output path for a slot in the AbstractFileSystemAccess class? Doesn't mess up the interface and existing implementations, and would be available in all of them, no matter which way the artifacts are generated...
Re: (no subject) [message #696647 is a reply to message #693402] Thu, 14 July 2011 09:39 Go to previous messageGo to next message
David Uebelacker is currently offline David Uebelacker
Messages: 2
Registered: July 2011
Junior Member
Hi

i run into the same problem. My generator should only generate a file if the file does not exists. After reading this post i was a little bit disappointed, but solved/hacked it as follows:

public class FileSystemUtils {

	public void generateFileIfNotExists(IFileSystemAccess fsa, String fileName,
			CharSequence contents) {
		File tFile = new File(getBasePath(fsa).getAbsolutePath()
				+ File.separator + fileName.substring(3));

		if (!tFile.exists()) {
			fsa.generateFile(fileName, contents);
		}
	}

	public File getBasePath(IFileSystemAccess fsa) {

		try {
			Field rootField = fsa.getClass().getDeclaredField("root");
			rootField.setAccessible(true);
			Object rootObject = rootField.get(fsa);

			Method folderMethod = rootObject.getClass().getMethod("getFolder",
					String.class);
			Object folder = folderMethod.invoke(rootObject, getProjectName(fsa)
					+ File.separator + "tmp");

			URI locationURI = (URI) folder.getClass()
					.getMethod("getLocationURI").invoke(folder);
			String tPath = locationURI.getPath();

			File basePath = new File(tPath);

			return basePath.getParentFile();

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public String getProjectName(IFileSystemAccess fsa) {

		try {
			Field pathesField = AbstractFileSystemAccess.class
					.getDeclaredField("pathes");
			pathesField.setAccessible(true);
			@SuppressWarnings("unchecked")
			Map<String, String> pathes = (Map<String, String>) pathesField
					.get(fsa);
			return pathes.get(IFileSystemAccess.DEFAULT_OUTPUT).split(
					File.separator)[1];
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

}


@Inject extension FileSystemUtils fsu
	
	def doGenerate(Entity entity, IFileSystemAccess fsa){
		
		var basePath = fsa.basePath;
		
		fsa.generateFileIfNotExists("myProject/" + entity.name + ".h", entity.compileHeader)


It's not nice and works only with the EclipseResourceFileSystemAccess implementation, so i also would really be happy about an getBasePath method in IFileSystemAccess.

David
(no subject) [message #696651 is a reply to message #693402] Thu, 14 July 2011 09:39 Go to previous messageGo to next message
David Uebelacker is currently offline David Uebelacker
Messages: 2
Registered: July 2011
Junior Member
Hi

i run into the same problem. My generator should only generate a file if the file does not exists. After reading this post i was a little bit disappointed, but solved/hacked it as follows:


public class FileSystemUtils {

public void generateFileIfNotExists(IFileSystemAccess fsa, String fileName,
CharSequence contents) {
File tFile = new File(getBasePath(fsa).getAbsolutePath()
+ File.separator + fileName.substring(3));

if (!tFile.exists()) {
fsa.generateFile(fileName, contents);
}
}

public File getBasePath(IFileSystemAccess fsa) {

try {
Field rootField = fsa.getClass().getDeclaredField("root");
rootField.setAccessible(true);
Object rootObject = rootField.get(fsa);

Method folderMethod = rootObject.getClass().getMethod("getFolder",
String.class);
Object folder = folderMethod.invoke(rootObject, getProjectName(fsa)
+ File.separator + "tmp");

URI locationURI = (URI) folder.getClass()
.getMethod("getLocationURI").invoke(folder);
String tPath = locationURI.getPath();

File basePath = new File(tPath);

return basePath.getParentFile();

} catch (Exception e) {
throw new RuntimeException(e);
}
}

public String getProjectName(IFileSystemAccess fsa) {

try {
Field pathesField = AbstractFileSystemAccess.class
.getDeclaredField("pathes");
pathesField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, String> pathes = (Map<String, String>) pathesField
.get(fsa);
return pathes.get(IFileSystemAccess.DEFAULT_OUTPUT).split(
File.separator)[1];
} catch (Exception e) {
throw new RuntimeException(e);
}

}

}



@Inject extension FileSystemUtils fsu

def doGenerate(Entity entity, IFileSystemAccess fsa){

var basePath = fsa.basePath;

fsa.generateFileIfNotExists("myProject/" + entity.name + ".h", entity.compileHeader)


It's not nice and works only with the EclipseResourceFileSystemAccess implementation, so i also would really be happy about an getBasePath method in IFileSystemAccess.

David
Re: (no subject) [message #696748 is a reply to message #696651] Thu, 14 July 2011 14:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Hi David,

feel free to file a enhancement request into bugzilla
regarding the interface of IFileSystemAccess
and the missing override=false possibility in the "new" outlets.

~Christian

[Updated on: Thu, 14 July 2011 14:47]

Report message to a moderator

icon14.gif  [xtext 2.0] protected region support? [message #697026 is a reply to message #696748] Fri, 15 July 2011 06:46 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

+1 on enhancement request.

I disagree on getBasePath() though, it limits the functionality to the local filesystem.

A getBaseURI() would be more like it. A filesystem resource would return a "file:" URI while others, maybe virtual filesystems or inside Eclipse workspace will return a different scheme (a WebDAV resource could return "http:".. Smile.
Re: [xtext 2.0] protected region support? [message #728447 is a reply to message #697026] Fri, 23 September 2011 05:52 Go to previous messageGo to next message
Daniel Dietrich is currently offline Daniel Dietrich
Messages: 5
Registered: July 2009
Junior Member
Just finished coding experimental protected region support. Perhaps it is useful for you. Published it at github under my account danieldietrich. (Can't post urls yet)

- Daniel
icon14.gif  Re: [xtext 2.0] protected region support? [message #728464 is a reply to message #728447] Fri, 23 September 2011 06:21 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

Thank you Daniel ! Smile

For those wondering, here it is:

https://github.com/danieldietrich/xtext-protectedregions

[Updated on: Thu, 01 December 2011 04:53]

Report message to a moderator

Re: [xtext 2.0] protected region support? [message #728707 is a reply to message #728464] Fri, 23 September 2011 13:40 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

I just realized that the built-in IFileSystemAccess implementations are (most common):

* JavaIoFileSystemAccess
* EclipseResourceFileSystemAccess

both contain a way to provide protected region support, albeit protected visibility (e.g. JavaIoFileSystemAccess.getPathes()) and needs to be subclassed.

Making getFileContents() and/or getFileUri() part of IFileSystemAccess would be great. Implementations that can't support it can just return UnsupportedOperationException (inherited from AbstractFilesystemAccess).
Re: [xtext 2.0] protected region support? [message #728714 is a reply to message #728707] Fri, 23 September 2011 14:24 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

I've just done a proof of concept using Daniel's work here:

https://github.com/soluvas/saentity

Works very well Smile

[Updated on: Thu, 01 December 2011 04:53]

Report message to a moderator

Re: [xtext 2.0] protected region support? [message #729672 is a reply to message #728714] Mon, 26 September 2011 14:25 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

For interested parties,
Protected regions + FileSystemAccess implementations, see:

https://github.com/danieldietrich/xtext-protectedregions

[Updated on: Thu, 01 December 2011 04:52]

Report message to a moderator

Re: [xtext 2.0] protected region support? [message #760091 is a reply to message #690241] Thu, 01 December 2011 04:48 Go to previous messageGo to next message
Sven Kasten is currently offline Sven Kasten
Messages: 8
Registered: May 2011
Junior Member
Hey,

all Github links are down now. Why? Smile

I think Protected Regions is one of the core concepts of code generation. How do I extend my generated code now without using inheritance or AOP?

Do you plan to add this feature in future versions? For my opinion XTend2 is hard to learn and it's hard to migrate. It brings cool features but why did you drop e. g. Protected Regions?

Greetings
Sven
Re: [xtext 2.0] protected region support? [message #760095 is a reply to message #760091] Thu, 01 December 2011 04:52 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy Irawan
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

Sven.

The project has been merged to here:

https://github.com/danieldietrich/xtext-protectedregions
Re: [xtext 2.0] protected region support? [message #760097 is a reply to message #760095] Thu, 01 December 2011 05:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Just again a short remark,

i guess "Protected Regions" are current stated as everything else than a "cool" feature in the modeling community

~Christian
Re: [xtext 2.0] protected region support? [message #760099 is a reply to message #760097] Thu, 01 December 2011 05:21 Go to previous message
Sven Kasten is currently offline Sven Kasten
Messages: 8
Registered: May 2011
Junior Member
Thanks Hendy!

Christian: maybe... I'am new to modeling and code generation stuff (half year experience), but I found PR very useful. Can you tell me other methods for extending generated code (without inheritance or APO, i already mentioned)?
Previous Topic:Handwritten formatter?
Next Topic:How do I write a search-path based scope?
Goto Forum:
  


Current Time: Wed Aug 27 11:17:42 EDT 2014

Powered by FUDForum. Page generated in 0.10665 seconds