scalasbtsbt-android-plugin

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?


Solution

  • 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.