ioreskovic
ioreskovic

Reputation: 5699

Conversion from sbt.package.File to java.io.File in SBT

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?

Upvotes: 0

Views: 1112

Answers (2)

0__
0__

Reputation: 67280

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" }
)

Upvotes: 1

0__
0__

Reputation: 67280

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.

Upvotes: 1

Related Questions