|
| 1 | +package dotty.tools.dotc |
| 2 | +package plugins |
| 3 | + |
| 4 | +import core._ |
| 5 | +import Contexts._ |
| 6 | +import Phases._ |
| 7 | +import dotty.tools.io._ |
| 8 | +import transform.MegaPhase.MiniPhase |
| 9 | + |
| 10 | +import java.io.InputStream |
| 11 | +import java.util.Properties |
| 12 | + |
| 13 | +import scala.collection.mutable |
| 14 | +import scala.util.{ Try, Success, Failure } |
| 15 | + |
| 16 | +trait PluginPhase extends MiniPhase { |
| 17 | + def runsBefore: Set[String] = Set.empty |
| 18 | +} |
| 19 | + |
| 20 | +sealed trait Plugin { |
| 21 | + /** The name of this plugin */ |
| 22 | + def name: String |
| 23 | + |
| 24 | + /** A one-line description of the plugin */ |
| 25 | + def description: String |
| 26 | + |
| 27 | + /** Is this plugin a research plugin? |
| 28 | + * |
| 29 | + * Research plugin receives a phase plan and return a new phase plan, while |
| 30 | + * non-research plugin returns a list of phases to be inserted. |
| 31 | + */ |
| 32 | + def research: Boolean = isInstanceOf[ResearchPlugin] |
| 33 | + |
| 34 | + /** A description of this plugin's options, suitable as a response |
| 35 | + * to the -help command-line option. Conventionally, the options |
| 36 | + * should be listed with the `-P:plugname:` part included. |
| 37 | + */ |
| 38 | + val optionsHelp: Option[String] = None |
| 39 | +} |
| 40 | + |
| 41 | +trait StandardPlugin extends Plugin { |
| 42 | + /** Non-research plugins should override this method to return the phases |
| 43 | + * |
| 44 | + * @param options: commandline options to the plugin, `-P:plugname:opt1,opt2` becomes List(opt1, opt2) |
| 45 | + * @return a list of phases to be added to the phase plan |
| 46 | + */ |
| 47 | + def init(options: List[String]): List[PluginPhase] |
| 48 | +} |
| 49 | + |
| 50 | +trait ResearchPlugin extends Plugin { |
| 51 | + /** Research plugins should override this method to return the new phase plan |
| 52 | + * |
| 53 | + * @param options: commandline options to the plugin, `-P:plugname:opt1,opt2` becomes List(opt1, opt2) |
| 54 | + * @param plan: the given phase plan |
| 55 | + * @return the new phase plan |
| 56 | + */ |
| 57 | + def init(options: List[String], plan: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] |
| 58 | +} |
| 59 | + |
| 60 | +object Plugin { |
| 61 | + |
| 62 | + private val PluginFile = "plugin.properties" |
| 63 | + |
| 64 | + /** Create a class loader with the specified locations plus |
| 65 | + * the loader that loaded the Scala compiler. |
| 66 | + */ |
| 67 | + private def loaderFor(locations: Seq[Path]): ClassLoader = { |
| 68 | + val compilerLoader = classOf[Plugin].getClassLoader |
| 69 | + val urls = locations map (_.toURL) |
| 70 | + |
| 71 | + new java.net.URLClassLoader(urls.toArray, compilerLoader) |
| 72 | + } |
| 73 | + |
| 74 | + type AnyClass = Class[_] |
| 75 | + |
| 76 | + /** Use a class loader to load the plugin class. |
| 77 | + */ |
| 78 | + def load(classname: String, loader: ClassLoader): Try[AnyClass] = { |
| 79 | + import scala.util.control.NonFatal |
| 80 | + try { |
| 81 | + Success[AnyClass](loader loadClass classname) |
| 82 | + } catch { |
| 83 | + case NonFatal(e) => |
| 84 | + Failure(new PluginLoadException(classname, s"Error: unable to load class $classname: ${e.getMessage}")) |
| 85 | + case e: NoClassDefFoundError => |
| 86 | + Failure(new PluginLoadException(classname, s"Error: class not found: ${e.getMessage} required by $classname")) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** Load all plugins specified by the arguments. |
| 91 | + * Each location of `paths` must be a valid plugin archive or exploded archive. |
| 92 | + * Each of `paths` must define one plugin. |
| 93 | + * Each of `dirs` may be a directory containing arbitrary plugin archives. |
| 94 | + * Skips all plugins named in `ignoring`. |
| 95 | + * A classloader is created to load each plugin. |
| 96 | + */ |
| 97 | + def loadAllFrom( |
| 98 | + paths: List[List[Path]], |
| 99 | + dirs: List[Path], |
| 100 | + ignoring: List[String]): List[Try[Plugin]] = |
| 101 | + { |
| 102 | + |
| 103 | + def fromFile(inputStream: InputStream, path: Path): String = { |
| 104 | + val props = new Properties |
| 105 | + props.load(inputStream) |
| 106 | + |
| 107 | + val pluginClass = props.getProperty("pluginClass") |
| 108 | + |
| 109 | + if (pluginClass == null) throw new RuntimeException("Bad plugin descriptor: " + path) |
| 110 | + else pluginClass |
| 111 | + } |
| 112 | + |
| 113 | + def loadDescriptionFromDir(f: Path): Try[String] = { |
| 114 | + val path = f / PluginFile |
| 115 | + Try(fromFile(new java.io.FileInputStream(path.jpath.toFile), path)) |
| 116 | + } |
| 117 | + |
| 118 | + def loadDescriptionFromJar(jarp: Path): Try[String] = { |
| 119 | + // XXX Return to this once we have more ARM support |
| 120 | + def read(is: InputStream) = |
| 121 | + if (is == null) throw new PluginLoadException(jarp.path, s"Missing $PluginFile in $jarp") |
| 122 | + else fromFile(is, jarp) |
| 123 | + |
| 124 | + val fileEntry = new java.util.jar.JarEntry(PluginFile) |
| 125 | + Try(read(new Jar(jarp.jpath.toFile).getEntryStream(fileEntry))) |
| 126 | + } |
| 127 | + |
| 128 | + // List[(jar, Try(descriptor))] in dir |
| 129 | + def scan(d: Directory) = |
| 130 | + d.files.toList sortBy (_.name) filter (Jar isJarOrZip _) map (j => (j, loadDescriptionFromJar(j))) |
| 131 | + |
| 132 | + type PDResults = List[Try[(String, ClassLoader)]] |
| 133 | + |
| 134 | + // scan plugin dirs for jars containing plugins, ignoring dirs with none and other jars |
| 135 | + val fromDirs: PDResults = dirs filter (_.isDirectory) flatMap { d => |
| 136 | + scan(d.toDirectory) collect { |
| 137 | + case (j, Success(pd)) => Success((pd, loaderFor(Seq(j)))) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // scan jar paths for plugins, taking the first plugin you find. |
| 142 | + // a path element can be either a plugin.jar or an exploded dir. |
| 143 | + def findDescriptor(ps: List[Path]) = { |
| 144 | + def loop(qs: List[Path]): Try[String] = qs match { |
| 145 | + case Nil => Failure(new MissingPluginException(ps)) |
| 146 | + case p :: rest => |
| 147 | + if (p.isDirectory) loadDescriptionFromDir(p.toDirectory) orElse loop(rest) |
| 148 | + else if (p.isFile) loadDescriptionFromJar(p.toFile) orElse loop(rest) |
| 149 | + else loop(rest) |
| 150 | + } |
| 151 | + loop(ps) |
| 152 | + } |
| 153 | + |
| 154 | + val fromPaths: PDResults = paths map (p => findDescriptor(p) match { |
| 155 | + case Success(classname) => Success((classname, loaderFor(p))) |
| 156 | + case Failure(e) => Failure(e) |
| 157 | + }) |
| 158 | + |
| 159 | + val seen = mutable.HashSet[String]() |
| 160 | + val enabled = (fromPaths ::: fromDirs) map(_.flatMap { |
| 161 | + case (classname, loader) => |
| 162 | + Plugin.load(classname, loader).flatMap { clazz => |
| 163 | + val plugin = instantiate(clazz) |
| 164 | + if (seen(classname)) // a nod to scala/bug#7494, take the plugin classes distinctly |
| 165 | + Failure(new PluginLoadException(plugin.name, s"Ignoring duplicate plugin ${plugin.name} (${classname})")) |
| 166 | + else if (ignoring contains plugin.name) |
| 167 | + Failure(new PluginLoadException(plugin.name, s"Disabling plugin ${plugin.name}")) |
| 168 | + else { |
| 169 | + seen += classname |
| 170 | + Success(plugin) |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | + enabled // distinct and not disabled |
| 175 | + } |
| 176 | + |
| 177 | + /** Instantiate a plugin class, given the class and |
| 178 | + * the compiler it is to be used in. |
| 179 | + */ |
| 180 | + def instantiate(clazz: AnyClass): Plugin = clazz.newInstance.asInstanceOf[Plugin] |
| 181 | +} |
| 182 | + |
| 183 | +class PluginLoadException(val path: String, message: String, cause: Exception) extends Exception(message, cause) { |
| 184 | + def this(path: String, message: String) = this(path, message, null) |
| 185 | +} |
| 186 | + |
| 187 | +class MissingPluginException(path: String) extends PluginLoadException(path, s"No plugin in path $path") { |
| 188 | + def this(paths: List[Path]) = this(paths mkString File.pathSeparator) |
| 189 | +} |
0 commit comments