Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an sbt plugin get a path to a file in the plugin?

Tags:

scala

sbt

I have an sbt (0.11.2) plugin that needs to get a path to text files inside the plugin. How do I do that? baseDirectory, sourceDirectories etc are set to the base of the project that's including the plugin, not the base of the plugin itself.

I'd like to provide a command to the plugin user that pulls defaults from a ruby file inside the plugin, and then allows the plugin user to override those defaults.

like image 816
James Moore Avatar asked Nov 13 '22 12:11

James Moore


1 Answers

Why don't you use good old Java's Class.getResource or Class.getResourceAsStream method? E.g. like this:

object TestPlugin extends Plugin {

  override def settings = super.settings ++ Seq(
    commands += testCommand
  )

  def testCommand = Command.command("test")(action)

  def action(state: State) = {
    try {
      val in = getClass.getResourceAsStream("/test.txt")
      val text = Source.fromInputStream(in).getLines mkString System.getProperty("line.separator")
      logger(state).info(text)
      in.close()
      state
    } catch {
      case e: Exception =>
        logger(state).error(e.getMessage)
        state.fail
    }
  }
}
like image 158
Heiko Seeberger Avatar answered Dec 20 '22 00:12

Heiko Seeberger