Programmatically invoke Scala Compiler

Hi,

is there any way to programmatically invoke the Scala compiler? The plugin classes (ScalaCompiler and the like) do not seem to be accessible, so my attempt was to reuse the "Compile '...'" entry (in fact, its action) in the context menu, however, I cannot find the proper action, implementing the compiler call.
The reason: I'm using some own Scala DSLs (models) from which I want to generate into some other language (Prolog). Thus, for parsing the model, I need a compiled class, however, so far, I have to do it manually, which in fact I want to avoid and do the compilation automatically prior to generating into the target language.

I hope, it's clear what I attempt to do. Any ideas anyone?

Cheers,

Phil

7 comments

Hi,

thanks for the answer, but writing a scala compiler plugin is not what i want to attempt. I only want to invoke the compiler progammatically and not change its behavior. As it states at the beginning of the second paragraph:

A compiler plugin lets you modify the behavior of the compiler     itself without needing to change the main Scala distribution.

This is not what I want to achieve.

Anyway, thanks for the suggestion.

Cheers,

Phil

0

1. You could run Ant or Maven before launching your app in IDEA (see attached screenshot).
2. Or you could do same when building your Artifact.
Screen Shot 2013-06-30 at 8.28.29 AM.png

0

Thanks again for your suggestion, but again, it does not solve my problem.
As specified in my initial question, I need to invoke the compiler programmatically inside an action which further triggers generation of some code
Calling my action shall invoke the compiler on the specified Scala file to then read the class file with a custom loader to parse it for further code generation.

What you suggest already works, as it's some base functionality of IDEA. However, I do nat want to trigger it manually, but instead progamatically.

I hope it's clear now what I want to do and why your suggestions don't work.
Anyway, thanks for suggesting.



Cheers,

Phil

0

Sorry, I didn't get from your 1st message that you are writing a plugin for IDEA.

0

The built-it compilation procedure is tightly integrated with the project model:

  • it relies on project configuration files,
  • it updates timestamps and dependencies (for incremental compilation),
  • it deletes orphan class files,
  • etc.


So, it's hardly practical to use the project compiler for purposes other than project compilation.

It seems that in your case, the better solution is to invoke Scalac directly:

  • determine a compiler libary using ScalaFacet.compiler,
  • use library classpath in ProcessBuilder to create a command line for Java VM:
    <java.home>/bin/java -cp <compiler library classpath> scala.tools.nsc.Main -d <output dir> <sources>
  • start a new process,
  • use something like OutputListener to handle process outputs and exist code.


You can see ScalacBackendCompiler for more details.

I hope this will help.

0

Hi Pavel,

thanks a lot for your reply...it put me on the right track!

Cheers,

Phil

0

Please sign in to leave a comment.