@@ -8,6 +8,7 @@ import dotty.tools.io._
88import transform .MegaPhase .MiniPhase
99
1010import java .io .InputStream
11+ import java .util .Scanner
1112
1213import scala .collection .mutable
1314import scala .util .{ Try , Success , Failure }
@@ -58,7 +59,7 @@ trait ResearchPlugin extends Plugin {
5859
5960object Plugin {
6061
61- private val PluginXML = " scalac- plugin.xml "
62+ private val PluginFile = " plugin"
6263
6364 /** Create a class loader with the specified locations plus
6465 * the loader that loaded the Scala compiler.
@@ -96,27 +97,34 @@ object Plugin {
9697 def loadAllFrom (
9798 paths : List [List [Path ]],
9899 dirs : List [Path ],
99- ignoring : List [String ]): List [Try [AnyClass ]] =
100+ ignoring : List [String ]): List [Try [Plugin ]] =
100101 {
101102
102- def loadDescriptionFromDir ( f : Path ): Try [ PluginDescription ] =
103- Try ( PluginDescription .fromXML( new java.io. FileInputStream ((f / PluginXML ).jpath.toFile)) )
103+ def fromFile ( inputStream : InputStream ): String = {
104+ val s = new Scanner (inputStream )
104105
105- def loadDescriptionFromJar (jarp : Path ): Try [PluginDescription ] = {
106+ if (s.hasNext) s.nextLine.trim
107+ else throw new RuntimeException (" Bad plugin descriptor." )
108+ }
109+
110+ def loadDescriptionFromDir (f : Path ): Try [String ] =
111+ Try (fromFile(new java.io.FileInputStream ((f / PluginFile ).jpath.toFile)))
112+
113+ def loadDescriptionFromJar (jarp : Path ): Try [String ] = {
106114 // XXX Return to this once we have more ARM support
107115 def read (is : InputStream ) =
108- if (is == null ) throw new PluginLoadException (jarp.path, s " Missing $PluginXML in $jarp" )
109- else PluginDescription .fromXML (is)
116+ if (is == null ) throw new PluginLoadException (jarp.path, s " Missing $PluginFile in $jarp" )
117+ else fromFile (is)
110118
111- val xmlEntry = new java.util.jar.JarEntry (PluginXML )
112- Try (read(new Jar (jarp.jpath.toFile).getEntryStream(xmlEntry )))
119+ val fileEntry = new java.util.jar.JarEntry (PluginFile )
120+ Try (read(new Jar (jarp.jpath.toFile).getEntryStream(fileEntry )))
113121 }
114122
115123 // List[(jar, Try(descriptor))] in dir
116124 def scan (d : Directory ) =
117125 d.files.toList sortBy (_.name) filter (Jar isJarOrZip _) map (j => (j, loadDescriptionFromJar(j)))
118126
119- type PDResults = List [Try [(PluginDescription , ClassLoader )]]
127+ type PDResults = List [Try [(String , ClassLoader )]]
120128
121129 // scan plugin dirs for jars containing plugins, ignoring dirs with none and other jars
122130 val fromDirs : PDResults = dirs filter (_.isDirectory) flatMap { d =>
@@ -128,7 +136,7 @@ object Plugin {
128136 // scan jar paths for plugins, taking the first plugin you find.
129137 // a path element can be either a plugin.jar or an exploded dir.
130138 def findDescriptor (ps : List [Path ]) = {
131- def loop (qs : List [Path ]): Try [PluginDescription ] = qs match {
139+ def loop (qs : List [Path ]): Try [String ] = qs match {
132140 case Nil => Failure (new MissingPluginException (ps))
133141 case p :: rest =>
134142 if (p.isDirectory) loadDescriptionFromDir(p.toDirectory) orElse loop(rest)
@@ -137,21 +145,27 @@ object Plugin {
137145 }
138146 loop(ps)
139147 }
140- val fromPaths : PDResults = paths map (p => (p, findDescriptor(p))) map {
141- case (p, Success (pd)) => Success ((pd, loaderFor(p)))
142- case (_, Failure (e)) => Failure (e)
143- }
148+
149+ val fromPaths : PDResults = paths map (p => findDescriptor(p) match {
150+ case Success (classname) => Success ((classname, loaderFor(p)))
151+ case Failure (e) => Failure (e)
152+ })
144153
145154 val seen = mutable.HashSet [String ]()
146155 val enabled = (fromPaths ::: fromDirs) map(_.flatMap {
147- case (pd , loader) if seen(pd.classname) =>
156+ case (classname , loader) =>
148157 // a nod to scala/bug#7494, take the plugin classes distinctly
149- Failure (new PluginLoadException (pd.name, s " Ignoring duplicate plugin ${pd.name} ( ${pd.classname}) " ))
150- case (pd, loader) if ignoring contains pd.name =>
151- Failure (new PluginLoadException (pd.name, s " Disabling plugin ${pd.name}" ))
152- case (pd, loader) =>
153- seen += pd.classname
154- Plugin .load(pd.classname, loader)
158+ Plugin .load(classname, loader).flatMap { clazz =>
159+ val plugin = instantiate(clazz)
160+ if (seen(classname))
161+ Failure (new PluginLoadException (plugin.name, s " Ignoring duplicate plugin ${plugin.name} ( ${classname}) " ))
162+ else if (ignoring contains plugin.name)
163+ Failure (new PluginLoadException (plugin.name, s " Disabling plugin ${plugin.name}" ))
164+ else {
165+ seen += classname
166+ Success (plugin)
167+ }
168+ }
155169 })
156170 enabled // distinct and not disabled
157171 }
0 commit comments