Files generation (e.g. java files) to source root folders via an external builder

Hi,

I want to create an external builder that produces Java files, which should be compiled by Java external builder afterwards.

At the moment the builder generates only one Test.txt file in src-gen source root with the current timestamp as a content on each change:

     @Override

     public ExitCode build(

               CompileContext context,

               ModuleChunk chunk,

               DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget> dirtyFilesHolder,

               OutputConsumer outputConsumer) throws ProjectBuildException,

               IOException {

          // find all mydsl files

          List<File> filesToCompile = getFilesToCompile(chunk);

          if (filesToCompile.isEmpty()) {

               return ExitCode.NOTHING_DONE;

          }

          // find src-gen source root

          File output = getOutput(chunk);

          File outputFile = new File(output, "Test.txt");

          if (!outputFile.exists()) {

               outputFile.createNewFile();

          }

          PrintStream printStream = new PrintStream(new FileOutputStream(outputFile));

          printStream.print(System.currentTimeMillis());

          printStream.close();

          

          // it seems to be used to notify IntelliJ about generated files...

                // but our output file actually does not belong to the output folder...

                // so not sure should we call it or not

          outputConsumer.registerOutputFile(chunk.representativeTarget(), outputFile, Arrays.asList(filesToCompile.get(0).getPath()));

          return ExitCode.OK;

     }

The problem is that changes are not picked up by VFS and I need to synchronize ‘src-gen’ to get them.

How will the right approach look like?

Regards,

Anton

5 comments
Comment actions Permalink

I was able to overcome that issue by using a custom message to pass files, which require refreshing, from the external builder's process to Idea's process.

But now I stumbled across another issue:

com.intellij.compiler.server.AutoMakeMessageHandler.handleBuildEvent(UUID, BuildEvent) ignores custom messages, so refreshing only works if auto make is disabled. Why is that? Could you fix it?


Also MPS uses the same approach and I guess other plugins which want to generate or delete source files will use it. Could you provide API to refresh generated source files, like OutputConsumer.registerSourceFile(target, generatedSourceFile, sourcePaths) to register generated and deleted source files, and then you can refresh them?

0
Comment actions Permalink

Hey, any suggestions how to tackle the issue described in the previous post? How to refresh source files generated by the external builder during auto-make?

0
Comment actions Permalink

I know how to refresh files: CompilerUtil.refreshIOFiles(generatedFiles);

The problem is how to notify Idea about generated source files from an external builder.
I wanted to use custom messages for it, similiar to what MPS does.
It works well for manual build, but during auto build custom messages are ignored: com.intellij.compiler.server.AutoMakeMessageHandler.handleBuildEvent(UUID, BuildEvent). I guess it is a bug.

Is there a workaround for it or maybe another approach which does not involve using custom messages? Generally I will expect that it should work out of the box: If one generates a source file then it should be refreshed by Idea (maybe by usign custom messages under the hood).

0
Comment actions Permalink

I've filed bugs for both issues:

  1. http://youtrack.jetbrains.com/issue/IDEA-128703 - IDEA does not refresh sources files generated by an external builder
  2. http://youtrack.jetbrains.com/issue/IDEA-128702 - During auto-make custom builder messages are ignored

Could somebody have a look at them?
0

Please sign in to leave a comment.