From ff77d0b0f2794b4b1f4e095a5f6b6037b2aedcca Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Mon, 24 Mar 2014 23:04:22 -0400 Subject: [PATCH] Remove def autoImport: Any from AutoPlugin. --- main/src/main/scala/sbt/BuildStructure.scala | 3 ++- main/src/main/scala/sbt/PluginDiscovery.scala | 2 +- main/src/main/scala/sbt/Plugins.scala | 26 +++++++++++-------- .../project/auto-plugins/project/Q.scala | 2 +- .../binary-plugin/changes/define/A.scala | 18 ++++++------- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/main/src/main/scala/sbt/BuildStructure.scala b/main/src/main/scala/sbt/BuildStructure.scala index f5c3200c7..da24a2444 100644 --- a/main/src/main/scala/sbt/BuildStructure.scala +++ b/main/src/main/scala/sbt/BuildStructure.scala @@ -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}) } diff --git a/main/src/main/scala/sbt/PluginDiscovery.scala b/main/src/main/scala/sbt/PluginDiscovery.scala index b28fed112..54939d6b9 100644 --- a/main/src/main/scala/sbt/PluginDiscovery.scala +++ b/main/src/main/scala/sbt/PluginDiscovery.scala @@ -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)) } diff --git a/main/src/main/scala/sbt/Plugins.scala b/main/src/main/scala/sbt/Plugins.scala index 3da012b1e..9451940c3 100644 --- a/main/src/main/scala/sbt/Plugins.scala +++ b/main/src/main/scala/sbt/Plugins.scala @@ -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 .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 } -} \ No newline at end of file +} diff --git a/sbt/src/sbt-test/project/auto-plugins/project/Q.scala b/sbt/src/sbt-test/project/auto-plugins/project/Q.scala index 044d7ab4d..5eb6f792c 100644 --- a/sbt/src/sbt-test/project/auto-plugins/project/Q.scala +++ b/sbt/src/sbt-test/project/auto-plugins/project/Q.scala @@ -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 } diff --git a/sbt/src/sbt-test/project/binary-plugin/changes/define/A.scala b/sbt/src/sbt-test/project/binary-plugin/changes/define/A.scala index b33a8e8d2..7906da1f2 100644 --- a/sbt/src/sbt-test/project/binary-plugin/changes/define/A.scala +++ b/sbt/src/sbt-test/project/binary-plugin/changes/define/A.scala @@ -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