@@ -8,6 +8,7 @@ import dotty.tools.io._
8
8
import transform .MegaPhase .MiniPhase
9
9
10
10
import java .io .InputStream
11
+ import java .util .Scanner
11
12
12
13
import scala .collection .mutable
13
14
import scala .util .{ Try , Success , Failure }
@@ -58,7 +59,7 @@ trait ResearchPlugin extends Plugin {
58
59
59
60
object Plugin {
60
61
61
- private val PluginXML = " scalac- plugin.xml "
62
+ private val PluginFile = " plugin"
62
63
63
64
/** Create a class loader with the specified locations plus
64
65
* the loader that loaded the Scala compiler.
@@ -96,27 +97,34 @@ object Plugin {
96
97
def loadAllFrom (
97
98
paths : List [List [Path ]],
98
99
dirs : List [Path ],
99
- ignoring : List [String ]): List [Try [AnyClass ]] =
100
+ ignoring : List [String ]): List [Try [Plugin ]] =
100
101
{
101
102
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 )
104
105
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 ] = {
106
114
// XXX Return to this once we have more ARM support
107
115
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)
110
118
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 )))
113
121
}
114
122
115
123
// List[(jar, Try(descriptor))] in dir
116
124
def scan (d : Directory ) =
117
125
d.files.toList sortBy (_.name) filter (Jar isJarOrZip _) map (j => (j, loadDescriptionFromJar(j)))
118
126
119
- type PDResults = List [Try [(PluginDescription , ClassLoader )]]
127
+ type PDResults = List [Try [(String , ClassLoader )]]
120
128
121
129
// scan plugin dirs for jars containing plugins, ignoring dirs with none and other jars
122
130
val fromDirs : PDResults = dirs filter (_.isDirectory) flatMap { d =>
@@ -128,7 +136,7 @@ object Plugin {
128
136
// scan jar paths for plugins, taking the first plugin you find.
129
137
// a path element can be either a plugin.jar or an exploded dir.
130
138
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 {
132
140
case Nil => Failure (new MissingPluginException (ps))
133
141
case p :: rest =>
134
142
if (p.isDirectory) loadDescriptionFromDir(p.toDirectory) orElse loop(rest)
@@ -137,21 +145,27 @@ object Plugin {
137
145
}
138
146
loop(ps)
139
147
}
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
+ })
144
153
145
154
val seen = mutable.HashSet [String ]()
146
155
val enabled = (fromPaths ::: fromDirs) map(_.flatMap {
147
- case (pd , loader) if seen(pd.classname) =>
156
+ case (classname , loader) =>
148
157
// 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
+ }
155
169
})
156
170
enabled // distinct and not disabled
157
171
}
0 commit comments