|
| 1 | +package dotty.tools.dotc |
| 2 | +package plugins |
| 3 | + |
| 4 | +import core._ |
| 5 | +import Contexts._ |
| 6 | +import Phases._ |
| 7 | +import dotty.tools.io._ |
| 8 | + |
| 9 | +import java.io.InputStream |
| 10 | + |
| 11 | +import scala.collection.mutable |
| 12 | +import scala.util.{ Try, Success, Failure } |
| 13 | + |
| 14 | +trait Plugin { |
| 15 | + /** The name of this plugin */ |
| 16 | + def name: String |
| 17 | + |
| 18 | + /** A one-line description of the plugin */ |
| 19 | + def description: String |
| 20 | + |
| 21 | + def options(implicit ctx: Context): List[String] = { |
| 22 | + // Process plugin options of form plugin:option |
| 23 | + def namec = name + ":" |
| 24 | + ctx.settings.pluginOptions.value filter (_ startsWith namec) map (_ stripPrefix namec) |
| 25 | + } |
| 26 | + |
| 27 | + /** Handle any plugin-specific options. |
| 28 | + * The user writes `-P:plugname:opt1,opt2`, |
| 29 | + * but the plugin sees `List(opt1, opt2)`. |
| 30 | + * The plugin can opt out of further processing |
| 31 | + * by returning false. For example, if the plugin |
| 32 | + * has an "enable" flag, now would be a good time |
| 33 | + * to sit on the bench. |
| 34 | + * @param options plugin arguments |
| 35 | + * @param error error function |
| 36 | + * @return true to continue, or false to opt out |
| 37 | + */ |
| 38 | + def init(phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = phases |
| 39 | + |
| 40 | + /** A description of this plugin's options, suitable as a response |
| 41 | + * to the -help command-line option. Conventionally, the options |
| 42 | + * should be listed with the `-P:plugname:` part included. |
| 43 | + */ |
| 44 | + val optionsHelp: Option[String] = None |
| 45 | +} |
| 46 | + |
| 47 | +/** ... |
| 48 | + * |
| 49 | + * @author Lex Spoon |
| 50 | + * @version 1.0, 2007-5-21 |
| 51 | + */ |
| 52 | +object Plugin { |
| 53 | + |
| 54 | + private val PluginXML = "scalac-plugin.xml" |
| 55 | + |
| 56 | + /** Create a class loader with the specified locations plus |
| 57 | + * the loader that loaded the Scala compiler. |
| 58 | + */ |
| 59 | + private def loaderFor(locations: Seq[Path]): ClassLoader = { |
| 60 | + val compilerLoader = classOf[Plugin].getClassLoader |
| 61 | + val urls = locations map (_.toURL) |
| 62 | + |
| 63 | + new java.net.URLClassLoader(urls.toArray, compilerLoader) |
| 64 | + } |
| 65 | + |
| 66 | + /** Try to load a plugin description from the specified location. |
| 67 | + */ |
| 68 | + private def loadDescriptionFromJar(jarp: Path): Try[PluginDescription] = { |
| 69 | + // XXX Return to this once we have more ARM support |
| 70 | + def read(is: InputStream) = |
| 71 | + if (is == null) throw new PluginLoadException(jarp.path, s"Missing $PluginXML in $jarp") |
| 72 | + else PluginDescription.fromXML(is) |
| 73 | + |
| 74 | + val xmlEntry = new java.util.jar.JarEntry(PluginXML) |
| 75 | + Try(read(new Jar(jarp.jfile).getEntryStream(xmlEntry))) |
| 76 | + } |
| 77 | + |
| 78 | + private def loadDescriptionFromFile(f: Path): Try[PluginDescription] = |
| 79 | + Try(PluginDescription.fromXML(new java.io.FileInputStream(f.jfile))) |
| 80 | + |
| 81 | + type AnyClass = Class[_] |
| 82 | + |
| 83 | + /** Use a class loader to load the plugin class. |
| 84 | + */ |
| 85 | + def load(classname: String, loader: ClassLoader): Try[AnyClass] = { |
| 86 | + import scala.util.control.NonFatal |
| 87 | + try { |
| 88 | + Success[AnyClass](loader loadClass classname) |
| 89 | + } catch { |
| 90 | + case NonFatal(e) => |
| 91 | + Failure(new PluginLoadException(classname, s"Error: unable to load class: $classname")) |
| 92 | + case e: NoClassDefFoundError => |
| 93 | + Failure(new PluginLoadException(classname, s"Error: class not found: ${e.getMessage} required by $classname")) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** Load all plugins specified by the arguments. |
| 98 | + * Each location of `paths` must be a valid plugin archive or exploded archive. |
| 99 | + * Each of `paths` must define one plugin. |
| 100 | + * Each of `dirs` may be a directory containing arbitrary plugin archives. |
| 101 | + * Skips all plugins named in `ignoring`. |
| 102 | + * A classloader is created to load each plugin. |
| 103 | + */ |
| 104 | + def loadAllFrom( |
| 105 | + paths: List[List[Path]], |
| 106 | + dirs: List[Path], |
| 107 | + ignoring: List[String]): List[Try[AnyClass]] = |
| 108 | + { |
| 109 | + // List[(jar, Try(descriptor))] in dir |
| 110 | + def scan(d: Directory) = |
| 111 | + d.files.toList sortBy (_.name) filter (Jar isJarOrZip _) map (j => (j, loadDescriptionFromJar(j))) |
| 112 | + |
| 113 | + type PDResults = List[Try[(PluginDescription, ClassLoader)]] |
| 114 | + |
| 115 | + // scan plugin dirs for jars containing plugins, ignoring dirs with none and other jars |
| 116 | + val fromDirs: PDResults = dirs filter (_.isDirectory) flatMap { d => |
| 117 | + scan(d.toDirectory) collect { |
| 118 | + case (j, Success(pd)) => Success((pd, loaderFor(Seq(j)))) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // scan jar paths for plugins, taking the first plugin you find. |
| 123 | + // a path element can be either a plugin.jar or an exploded dir. |
| 124 | + def findDescriptor(ps: List[Path]) = { |
| 125 | + def loop(qs: List[Path]): Try[PluginDescription] = qs match { |
| 126 | + case Nil => Failure(new MissingPluginException(ps)) |
| 127 | + case p :: rest => |
| 128 | + if (p.isDirectory) loadDescriptionFromFile(p.toDirectory / PluginXML) orElse loop(rest) |
| 129 | + else if (p.isFile) loadDescriptionFromJar(p.toFile) orElse loop(rest) |
| 130 | + else loop(rest) |
| 131 | + } |
| 132 | + loop(ps) |
| 133 | + } |
| 134 | + val fromPaths: PDResults = paths map (p => (p, findDescriptor(p))) map { |
| 135 | + case (p, Success(pd)) => Success((pd, loaderFor(p))) |
| 136 | + case (_, Failure(e)) => Failure(e) |
| 137 | + } |
| 138 | + |
| 139 | + val seen = mutable.HashSet[String]() |
| 140 | + val enabled = (fromPaths ::: fromDirs) map { |
| 141 | + case Success((pd, loader)) if seen(pd.classname) => |
| 142 | + // a nod to scala/bug#7494, take the plugin classes distinctly |
| 143 | + Failure(new PluginLoadException(pd.name, s"Ignoring duplicate plugin ${pd.name} (${pd.classname})")) |
| 144 | + case Success((pd, loader)) if ignoring contains pd.name => |
| 145 | + Failure(new PluginLoadException(pd.name, s"Disabling plugin ${pd.name}")) |
| 146 | + case Success((pd, loader)) => |
| 147 | + seen += pd.classname |
| 148 | + Plugin.load(pd.classname, loader) |
| 149 | + case Failure(e) => |
| 150 | + Failure(e) |
| 151 | + } |
| 152 | + enabled // distinct and not disabled |
| 153 | + } |
| 154 | + |
| 155 | + /** Instantiate a plugin class, given the class and |
| 156 | + * the compiler it is to be used in. |
| 157 | + */ |
| 158 | + def instantiate(clazz: AnyClass): Plugin = clazz.newInstance.asInstanceOf[Plugin] |
| 159 | +} |
| 160 | + |
| 161 | +class PluginLoadException(val path: String, message: String, cause: Exception) extends Exception(message, cause) { |
| 162 | + def this(path: String, message: String) = this(path, message, null) |
| 163 | +} |
| 164 | + |
| 165 | +class MissingPluginException(path: String) extends PluginLoadException(path, s"No plugin in path $path") { |
| 166 | + def this(paths: List[Path]) = this(paths mkString File.pathSeparator) |
| 167 | +} |
0 commit comments