0

I'm working on extending Jan Berkel's Android Plugin for SBT.

Right now, I'm wondering, how can I convert sbt.SettingKey[sbt.package.File] to java.io.File? Is there a way to extract java.io.File from sbt.SettingKey[sbt.package.File]?

For example:

I have a function:

def isUpToDate(input: java.io.File): Boolean

which expects java.io.File as an argument.

I have a sbt.SettingKey[sbt.package.File] (named myFileKey) which is mapped to my File I need.

How do I call isUpToDate with the File mapped to myFileKey?

ioreskovic
  • 5,531
  • 5
  • 39
  • 70

2 Answers2

1

You will need to compose a dependency using <<=, extracting the file using the apply method of the settings key. E.g.

yourKey <<= fileKey { file => ... }

which is short for

yourKey <<= fileKey.apply { file => ... }

See the section "Computing a value based on other keys' values" of the sbt getting-started-guide.

Also note that sbt.File is merely a type alias for java.io.File.


For example to map some file:

val yourKey  = SettingKey[File]("yourKey", "Description")
val settings = Seq[Setting[_]](
  // ....
  yourKey <<= fileKey { f => f / "subdirectory" }
)
0__
  • 66,707
  • 21
  • 171
  • 266
  • Out of curiosity: where is ``sbt.package.File`` declared? I quickly searched through the sources (0.13 branch, cbc94f9cb6), but couldn't fine any such declaration. – Malte Schwerhoff Jul 16 '12 at 13:32
  • It is in the 0.11 branch, a mere type alias (`package object sbt { ... type File = java.io.File ... }`) – 0__ Jul 16 '12 at 13:43
  • But won't that simply return me another Key[something]? Sorry for dumb questions, I'm new to Scala/SBT – ioreskovic Jul 16 '12 at 13:53
  • @Lopina, it will turn it into a `Key[Something]`. But as a side effect you get to use the file to do something. – huynhjl Jul 16 '12 at 14:13
  • Well, yes. It defines the key for your own task that works on the given file. You will need to be more specific about what you try to do with it. – 0__ Jul 16 '12 at 14:15
  • I want a _File_ contained in SettingKey[File] – ioreskovic Jul 16 '12 at 14:18
  • _Where_ do you want it, and _what_ do you want to do _with_ it? You need to clarify that first. You know you can just ask for a key's value in the sbt shell, e.g. `> base-directory` or `> compile:target` – 0__ Jul 16 '12 at 14:20
  • I want to have in a val, so I can send it to a function that takes java.io.File – ioreskovic Jul 16 '12 at 14:28
  • I don't think we understood eachother, I updated my question with an example. – ioreskovic Jul 16 '12 at 14:52
1

Concerning the updated question. I am still assuming you are modifying an existing sbt plugin. And therefore, still you need to introduce a dependency. The value of a setting key only becomes valid at a particular stage of the build process. Therefore, to retrieve that value, you need to depend on the setting key.

Please read the section "Task Keys" in the .sbt Build Definition document, to decide whether you need to depend on a plain setting key (static) or on the result of another task (dynamic). It looks to me as if your isUpToDate might need to be re-evaluated over and over again. Thus you would need a task.

val isUpToDate = TaskKey[Boolean]("isUpToDate", "Description")
val settings = Seq[Setting[_]](
// ....
   isUpToDate <<= fileKey.map(checkUpToDate)
)

private def checkUpToDate(f: File): Boolean = { ... }

Note that you need map here instead of apply in order to construct a task from a setting key.

0__
  • 66,707
  • 21
  • 171
  • 266