Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Code generation problem(Can't avoid automatic rebuilds)
Code generation problem [message #1128150] Mon, 07 October 2013 11:49 Go to next message
Maksim Rubanov is currently offline Maksim RubanovFriend
Messages: 1
Registered: October 2013
Junior Member
Hello everybody.

I am developing plugins for Eclipse using Xtext and I have to use code-generation quite a lot. For example, whenever I add\change some source files of type 'A', I have to generate a source file 'B' for each of A's. Obviously, I would like to avoid rebuilding the workspace after saving every B-file. However, I can't get it right.

From manuals\FAQs I have learnt that for performing multiple modifications I should use IWorkspaceRunnable type of job in such a situation, however, it does not work the way I want. Namely, the task gets cancelled after saving a single file, and then gets restarted again, which causes an auto-build on each generated file. No surprise, if I have 200 'A' sources, auto-build job is launched 200 times.

Thus, right now the structure of my code is the following (pseudo):

class MyBuilderParticipant implements IXtextBuilderParticipant {

  public void build(final IBuildContext context, final IProgressMonitor monitor) {
    if (!isEnabled(context)) {
      return;
    }

    final int numberOfDeltas = context.getDeltas().size();
    SubMonitor progress = SubMonitor.convert(monitor, numberOfDeltas);

    for (int i = 0; i < numberOfDeltas; i++) {
      if (progress.isCanceled()) {
        throw new OperationCanceledException();
      }
          
        // generate and store in memory a file for each delta
    }

    // store generator results
    final IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
      public void run(final IProgressMonitor monitor) {
        try {
          foreach (GeneratedFile file : generatedFiles) {
            if (progress.isCanceled()) {
              throw new OperationCanceledException();
            }
            
            // store all the generated files in workspace

            progress.worked(1);
          }
        } finally {
          if (monitor != null) {
            monitor.done();
          }
        }
      }
    };

    ISchedulingRule rule = context.getBuiltProject();
    try {
      ResourcesPlugin.getWorkspace().run(runnable, rule, IWorkspace.AVOID_UPDATE, monitor);
    } catch (CoreException e) {
      throw new WrappedException(e);
    }
  }
}


For some reason, on the second iteration of the loop in IWorkspaceRunnable the task gets cancelled and build method gets invoked again later on. What do I do wrong?
Re: Code generation problem [message #1149549 is a reply to message #1128150] Tue, 22 October 2013 06:35 Go to previous message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
The BuilderParticipant is called during a build, in which you can change
the workspace as it seems fit, that is you shouldn't wrap the code
generation in a runnable. The changes would even be ignored (no deltas
and subsequent builds) unless you call needsRebuild on the IBuildContext.

Am 10/7/13 2:29 PM, schrieb Maksim Rubanov:
> Hello everybody.
>
> I am developing plugins for Eclipse using Xtext and I have to use
> code-generation quite a lot. For example, whenever I add\change some
> source files of type 'A', I have to generate a source file 'B' for each
> of A's. Obviously, I would like to avoid rebuilding the workspace after
> saving every B-file. However, I can't get it right.
>
> From manuals\FAQs I have learnt that for performing multiple
> modifications I should use IWorkspaceRunnable type of job in such a
> situation, however, it does not work the way I want. Namely, the task
> gets cancelled after saving a single file, and then gets restarted
> again, which causes an auto-build on each generated file. No surprise,
> if I have 200 'A' sources, auto-build job is launched 200 times.
>
> Thus, right now the structure of my code is the following (pseudo):
>
>
> class MyBuilderParticipant implements IXtextBuilderParticipant {
>
> public void build(final IBuildContext context, final IProgressMonitor
> monitor) {
> if (!isEnabled(context)) {
> return;
> }
>
> final int numberOfDeltas = context.getDeltas().size();
> SubMonitor progress = SubMonitor.convert(monitor, numberOfDeltas);
>
> for (int i = 0; i < numberOfDeltas; i++) {
> if (progress.isCanceled()) {
> throw new OperationCanceledException();
> }
> // generate and store in memory a file for each delta
> }
>
> // store generator results
> final IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
> public void run(final IProgressMonitor monitor) {
> try {
> foreach (GeneratedFile file : generatedFiles) {
> if (progress.isCanceled()) {
> throw new OperationCanceledException();
> }
> // store all the generated files in workspace
>
> progress.worked(1);
> }
> } finally {
> if (monitor != null) {
> monitor.done();
> }
> }
> }
> };
>
> ISchedulingRule rule = context.getBuiltProject();
> try {
> ResourcesPlugin.getWorkspace().run(runnable, rule,
> IWorkspace.AVOID_UPDATE, monitor);
> } catch (CoreException e) {
> throw new WrappedException(e);
> }
> }
> }
>
>
> For some reason, on the second iteration of the loop in
> IWorkspaceRunnable the task gets cancelled and build method gets invoked
> again later on. What do I do wrong?
>


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Previous Topic:xtext and object identity with reparsed parts
Next Topic:Railroad image for grammar documentation
Goto Forum:
  


Current Time: Fri Mar 29 11:10:02 GMT 2024

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

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

Back to the top