-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAmmoniteClasspath.scala
38 lines (31 loc) · 1.2 KB
/
AmmoniteClasspath.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.thoughtworks.deeplearning.sbtammoniteclasspath
import com.thoughtworks.dsl.keywords.Each
import sbt._, Keys._
import sbt.plugins.JvmPlugin
import scala.meta._
/**
* @author 杨博 (Yang Bo)
*/
object AmmoniteClasspath extends AutoPlugin {
override def trigger = allRequirements
override def requires = JvmPlugin
object autoImport {
val exportToAmmoniteScript = taskKey[File](
"Export classpath as a .sc file, which can be loaded by another ammonite script or a Jupyter Scala notebook")
}
import autoImport._
override def projectSettings: Seq[Def.Setting[_]] = Seq {
val configuration = !Each(Seq(Compile, Test, Runtime, Provided))
val classpathKey = !Each(Seq(fullClasspath, dependencyClasspath, managedClasspath, unmanagedClasspath))
exportToAmmoniteScript in classpathKey in configuration := {
val code = {
def ammonitePaths =
List(q"_root_.ammonite.ops.Path(${(!Each((classpathKey in configuration).value)).data.toString})")
q"interp.load.cp(Seq(..$ammonitePaths))"
}
val file = (crossTarget in configuration).value / s"${classpathKey.key.label}-${configuration.id}.sc"
IO.write(file, code.syntax)
file
}
}
}