SBT如何创建自定义命令

时间:2016-02-19 09:09:49

标签: scala sbt

鉴于我有build.sbt

name := """app"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.7"

// <-- some other code -->

import Fixtures._

lazy val fixtures = inputKey[Unit]("Generating Cassandra fixtures")

fixtures := {
  Fixtures.generate()
}

project目录中的Fixtures.scala

object Fixtures {
  def generate (): Unit = {
    println("generating fixtures")
  }
}

我可以运行命令./activator fixtures,我正在&#34;生成灯具&#34;

但是如何调用某些服务,比如GenerateUserFixtureService.scala来自app/scala/com/MyProject/Service目录。导入包不起作用,因为project目录属于不同的包。我无法从Play

Fixtures.scala导入任何内容
|
|___app
|   |__scala
|      |__com
|         |__MyProject
|            |__Service
|               |--GenerateUserFixtureService.scala
|___project
|   |--Fixtures.scala
|___
    |--build.sbt

实际上问题是,为什么我只能从build.sbt目录导入project个文件。或者如何从app dir调用另一个文件?

或许我想的方式完全错了。我只是想创建一些命令upload:fixtures send:emails等,并调用一些scala类。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:8)

要创建自定义命令,必须指定与命令逻辑对应的函数。我们来看几个例子: 首先打印你好消息:

 def helloSbt = Command.command("hello") { state =>
        println("Hello, SBT")
        state
    }

commands += helloSbt

只需将此代码放入build.sbtcommandsproject keysbt.Keysval commands = SettingKey[Seq[Command]]

当然,您可以像成功或失败一样管理命令语句:

def failJustForFun = Command.single("fail-condidtion") {
        case (state, "true") => state.fail
        case (state, _) => state
    }

您可以通过利用DefaultParsers来改变控制台特定部分或该命令输出的控制台颜色:

    lazy val color = token( Space ~> ("blue" ^^^ "4" | "green" ^^^ "2") )
    lazy val select = token( "fg" ^^^ "3" | "bg" ^^^ "4" )
    lazy val setColor = (select ~ color) map { case (g, c) => "\033[" + g + c + "m" }

另外,您可以扩展xsbti.AppMain并实施 - your own logic