On Thu, Aug 2, 2012 at 9:41 PM, Mark Harrah <
dmha...@gmail.com> wrote:
> On Tue, 31 Jul 2012 22:22:09 +0200
> Paolo Giarrusso <
pgiar...@mathematik.uni-marburg.de> wrote:
>
>> On Fri, Jul 20, 2012 at 8:11 AM, Jason Zaugg <
jza...@gmail.com> wrote:
>> > On Friday, July 20, 2012 2:21:31 AM UTC+2, Paolo Giarrusso wrote:
>> >>
>> >> How do I learn programmatically, within build.sbt, the scalaVersion used
>> >> for running SBT? For instance, 2.9.2 for SBT 0.12, 2.9.1 for SBT 0.11.3,
>> >> etc.
>> >>
>> >> But when I switched to scalaVersion := "2.10.0-M5" with SBT 0.12, I've
>> >> found the hard way that the right Scala version there is "2.9.2", the one
>> >> used to run SBT.
>> >>
>> >> So, any suggestion?
>> >
>> >
>> > How did you switch? To do this interactively in the console, use "++
>> > 2.10.0-M5".
>>
>> Ah thanks - but I had to change both scalaVersion and the version of
>> ScalaTest I use, and then use reload.
>> But I tried to ask a different question: how do I learn the version of
>> Scala which runs SBT (which is different from what you talk about)
>> within build.sbt?
>
> There isn't a specific setting, but you can process the launched application's AppConfiguration (that is, sbt's AppConfiguration) as is done to obtain the default Scala version:
>
> scalaVersion in GlobalScope <<= appConfiguration(_.provider.scalaProvider.version)
> I could see needing the Scala version in a project/plugins.sbt, but why do you need it in build.sbt?
To configure a library (Scalate) used by source code generators, I
need to pass it its own Scala version, so that it can configure the
classpath to run the generated code. In fact, it's a Scalate bug and
this solution is a hack, but no good solution is yet known.
The first message of this thread includes a more detailed answer; I
guess that the high-level information was obscured by all the details
I gave.
# The final answer I used.
For the question I asked, the answer is using `scalaVersion in
GlobalScope` directly (since it's already initialized):
sourceGenerators in Compile <+= (sourceManaged in Compile,
baseDirectory, scalaVersion in GlobalScope) map { (dir, baseDir,
sbtScalaVersion ) =>
scala.Console.err.printf("sbtScalaVersion %s\n", sbtScalaVersion)
val gen = new Generator(sbtScalaVersion)
//use gen for code generation...
}
However, what I would in fact need would be scalaBinaryVersion in
GlobalScope, which is not available; luckily,
appConfiguration(_.provider.scalaProvider.libraryJar) is in fact
exactly what I need for Scalate.
Calling map on `appConfiguration(_.provider.scalaProvider.libraryJar)`
fails, since map returns not `SettingKey` but `Initialize` - the trick
I learned is to call map on appConfiguration before any other
processing:
sourceGenerators in Compile <+= (sourceManaged in Compile,
baseDirectory, appConfiguration) map { (dir, baseDir, appConfig ) =>
val libraryJar = appConfig.provider.scalaProvider.libraryJar
val gen = new Generator(libraryJar) //I changed the type of the constructor.
//use gen for code generation...
}
>> Or maybe, the right question is: can a Scala program learn the version
>> of the Scala library it's running on? If yes, how?
> You probably want scala.util.Properties and one of the version methods.
That's close enough; but since I need the binary version as normalized
by SBT, I'll use the SBT-specific solution.
Thanks for the help and best regards