Remove def autoImport: Any from AutoPlugin.

This commit is contained in:
Eugene Yokota 2014-03-24 23:04:22 -04:00
parent cc80d216ab
commit ff77d0b0f2
5 changed files with 27 additions and 24 deletions

View File

@ -90,7 +90,7 @@ final class DetectedModules[T](val modules: Seq[(String, T)])
}
/** Auto-detected auto plugin. */
case class DetectedAutoPlugin(val name: String, val value: AutoPlugin, val hasStableAutoImport: Boolean)
case class DetectedAutoPlugin(val name: String, val value: AutoPlugin, val hasAutoImport: Boolean)
/** Auto-discovered modules for the build definition project. These include modules defined in build definition sources
* as well as modules in binary dependencies.
@ -105,6 +105,7 @@ final class DetectedPlugins(val plugins: DetectedModules[Plugin], val autoPlugin
if (hasAutoImport) Some(name + ".autoImport")
else None
}))
/** A function to select the right [[AutoPlugin]]s from [[autoPlugins]] for a [[Project]]. */
lazy val deducePlugins: (Plugins, Logger) => Seq[AutoPlugin] = Plugins.deducer(autoPlugins.toList map {_.value})
}

View File

@ -35,7 +35,7 @@ object PluginDiscovery
)
val detectedAutoPugins = discover[AutoPlugin](AutoPlugins)
val allAutoPlugins = (defaultAutoPlugins ++ detectedAutoPugins.modules) map { case (name, value) =>
DetectedAutoPlugin(name, value, sbt.Plugins.hasStableAutoImport(value, loader))
DetectedAutoPlugin(name, value, sbt.Plugins.hasAutoImportGetter(value, loader))
}
new DetectedPlugins(discover[Plugin](Plugins), allAutoPlugins, discover[Build](Builds))
}

View File

@ -19,14 +19,19 @@ Steps for plugin authors:
1. Determine if the AutoPlugin should automatically be activated when all requirements are met, or should be opt-in.
2. Determine the [[AutoPlugins]]s that, when present (or absent), act as the requirements for the AutoPlugin.
3. Determine the settings/configurations to that the AutoPlugin injects when activated.
4. Determine the keys and other names to be automatically imported to *.sbt scripts.
For example, the following will automatically add the settings in `projectSettings`
to a project that has both the `Web` and `Javascript` plugins enabled.
object MyPlugin extends AutoPlugin {
object Plugin extends sbt.AutoPlugin {
def requires = Web && Javascript
def trigger = allRequirements
override def projectSettings = Seq(...)
object autoImport {
lazy val obfuscate = taskKey[Seq[File]]("Obfuscates the source.")
}
}
Steps for users:
@ -43,6 +48,7 @@ will activate `MyPlugin` defined above and have its settings automatically added
<Project>.addPlugins( Web && Javascript ).disablePlugins(MyPlugin)
then the `MyPlugin` settings (and anything that activates only when `MyPlugin` is activated) will not be added.
*/
abstract class AutoPlugin extends Plugins.Basic with PluginsFunctions
{
@ -63,10 +69,6 @@ abstract class AutoPlugin extends Plugins.Basic with PluginsFunctions
override def toString: String = label
/** When this method is overridden with a val or a lazy val, `autoImport._` is automatically
* imported to *.sbt scripts. */
def autoImport: Any = ()
/** The [[Configuration]]s to add to each project that activates this AutoPlugin.*/
def projectConfigurations: Seq[Configuration] = Nil
@ -309,15 +311,17 @@ ${listConflicts(conflicting)}""")
case ap: AutoPlugin => model(ap)
}
private[sbt] def hasStableAutoImport(ap: AutoPlugin, loader: ClassLoader): Boolean = {
private[sbt] def hasAutoImportGetter(ap: AutoPlugin, loader: ClassLoader): Boolean = {
import reflect.runtime.{universe => ru}
import util.control.Exception.catching
val m = ru.runtimeMirror(loader)
val im = m.reflect(ap)
val fmOpt = catching(classOf[ScalaReflectionException]) opt {
val autoImportSym = im.symbol.asType.toType.declaration(ru.newTermName("autoImport")).asTerm
im.reflectField(autoImportSym)
val hasGetterOpt = catching(classOf[ScalaReflectionException]) opt {
im.symbol.asType.toType.declaration(ru.newTermName("autoImport")) match {
case ru.NoSymbol => false
case sym => sym.asTerm.isGetter
}
}
fmOpt.isDefined
hasGetterOpt getOrElse false
}
}
}

View File

@ -24,7 +24,7 @@ object Imports
}
object X extends AutoPlugin {
override lazy val autoImport = Imports
val autoImport = Imports
def requires = Plugins.empty
def trigger = noTrigger
}

View File

@ -3,21 +3,19 @@ package sbttest // you need package http://stackoverflow.com/questions/9822008/
import sbt._
import Keys._
object Imports {
object bN extends AutoPlugin {
def requires = empty
def trigger = allRequirements
}
lazy val check = taskKey[Unit]("Checks that the AutoPlugin and Build are automatically added.")
}
object C extends AutoPlugin {
override lazy val autoImport = Imports
object autoImport {
object bN extends AutoPlugin {
def requires = empty
def trigger = allRequirements
}
lazy val check = taskKey[Unit]("Checks that the AutoPlugin and Build are automatically added.")
}
def requires = empty
def trigger = noTrigger
}
import Imports._
import C.autoImport._
object A extends AutoPlugin {
def requires = bN