-
Notifications
You must be signed in to change notification settings - Fork 224
/
Main.scala
165 lines (154 loc) · 5.49 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package scala.meta.internal.metacp
import java.nio.file._
import java.nio.file.attribute.BasicFileAttributes
import java.util.concurrent.ConcurrentLinkedQueue
import scala.collection.JavaConverters._
import scala.meta.internal.index._
import scala.meta.internal.javacp._
import scala.meta.internal.scalacp._
import scala.meta.internal.io._
import scala.meta.io._
import scala.meta.metacp._
import scala.util.control.NonFatal
import java.util.concurrent.atomic.AtomicBoolean
import scala.collection.GenSeq
class Main(settings: Settings, reporter: Reporter) {
def process(): Option[Classpath] = {
val success = new AtomicBoolean(true)
val classpath: GenSeq[AbsolutePath] =
if (settings.par) settings.classpath.entries.par
else settings.classpath.entries
val buffer = new ConcurrentLinkedQueue[AbsolutePath]()
if (!Files.exists(Files.createDirectories(settings.cacheDir.toNIO))) {
Files.createDirectories(settings.cacheDir.toNIO)
}
// Writes to a temporary jar file and atomically moves the resulting jar file to cacheEntry once processing
// completes. This guarantees the cached jar is never available in half-processed form.
def createCachedJar(cacheEntry: AbsolutePath)(f: AbsolutePath => Boolean): Unit = {
val tmp = Files.createTempDirectory("metacp").resolve(cacheEntry.toNIO.getFileName)
PlatformFileIO.withJarFileSystem(AbsolutePath(tmp), create = true) { out =>
val res = f(out)
success.compareAndSet(true, res)
buffer.add(cacheEntry)
}
try {
Files.move(
tmp,
cacheEntry.toNIO,
StandardCopyOption.ATOMIC_MOVE,
StandardCopyOption.REPLACE_EXISTING
)
} catch {
case _: AtomicMoveNotSupportedException =>
Files.move(
tmp,
cacheEntry.toNIO,
StandardCopyOption.REPLACE_EXISTING
)
case _: AccessDeniedException if scala.util.Properties.isWin =>
// AccessDeniedException seems to be thrown on Windows when ATOMIC_MOVE is provided
// and the target file is being used elsewhere, see https://github.com/scalameta/scalameta/issues/1499
Files.move(
tmp,
cacheEntry.toNIO,
StandardCopyOption.REPLACE_EXISTING
)
}
}
classpath.foreach { entry =>
if (entry.isDirectory) {
val out = AbsolutePath(Files.createTempDirectory("semanticdb"))
val res = convertClasspathEntry(entry, out)
success.compareAndSet(true, res)
buffer.add(out)
} else if (entry.isFile) {
val cacheEntry = {
val checksum = Checksum(entry)
settings.cacheDir.resolve(
entry.toFile.getName.stripSuffix(".jar") + "-" + checksum + ".jar")
}
if (cacheEntry.toFile.exists) {
buffer.add(cacheEntry)
} else {
createCachedJar(cacheEntry)(out => convertClasspathEntry(entry, out))
}
}
}
if (settings.scalaLibrarySynthetics) {
val cacheEntry = settings.cacheDir.resolve("scala-library-synthetics.jar")
if (cacheEntry.toFile.exists) {
buffer.add(cacheEntry)
} else {
createCachedJar(cacheEntry)(dumpScalaLibrarySynthetics)
}
}
if (success.get) {
import scala.collection.JavaConverters._
Some(Classpath(buffer.iterator().asScala.toList))
} else {
None
}
}
private def convertClasspathEntry(in: AbsolutePath, out: AbsolutePath): Boolean = {
var success = true
val index = new Index
val classpath = Classpath(in)
classpath.visit { base =>
new SimpleFileVisitor[Path] {
override def visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult = {
if (PathIO.extension(path) == "class") {
try {
val abspath = AbsolutePath(path)
val node = abspath.toClassNode
val result = {
val attrs = if (node.attrs != null) node.attrs.asScala else Nil
if (attrs.exists(_.`type` == "ScalaSig")) {
val classfile = ToplevelClassfile(base, abspath, node)
Scalacp.parse(classfile)
} else if (attrs.exists(_.`type` == "Scala")) {
None
} else {
val innerClassNode = node.innerClasses.asScala.find(_.name == node.name)
if (innerClassNode.isEmpty) {
val classfile = ToplevelClassfile(base, abspath, node)
Javacp.parse(classfile)
} else {
None
}
}
}
result.foreach { infos =>
index.append(infos.uri, infos.toplevels)
infos.save(out)
}
} catch {
case NonFatal(ex) =>
reporter.out.println(s"error: can't convert $path")
ex.printStackTrace(reporter.out)
success = false
}
}
FileVisitResult.CONTINUE
}
}
}
index.save(out)
success
}
private def dumpScalaLibrarySynthetics(out: AbsolutePath): Boolean = {
val index = new Index
val synthetics = List(
Scalalib.anyClass,
Scalalib.anyValClass,
Scalalib.anyRefClass,
Scalalib.nothingClass,
Scalalib.nullClass,
Scalalib.singletonTrait)
synthetics.foreach { infos =>
index.append(infos.uri, infos.toplevels)
infos.save(out)
}
index.save(out)
true
}
}