@@ -15,56 +15,119 @@ import scala.meta.io.AbsolutePath
1515import com .sourcegraph .io .DeleteVisitor
1616import com .sourcegraph .lsif_java .Dependencies
1717import com .sourcegraph .lsif_java .SemanticdbPrinters
18+ import com .sourcegraph .lsif_java .LsifJava
19+ import java .nio .file .SimpleFileVisitor
20+ import java .nio .file .FileVisitResult
21+ import java .nio .file .attribute .BasicFileAttributes
22+ import java .io .PrintStream
23+ import java .io .ByteArrayOutputStream
24+ import moped .reporters .ConsoleReporter
1825
1926class LibrarySnapshotGenerator extends SnapshotGenerator {
2027 val scalaPattern = FileSystems .getDefault.getPathMatcher(" glob:**.scala" )
2128 val javaPattern = FileSystems .getDefault.getPathMatcher(" glob:**.java" )
2229 private case class IndexMetrics (occurrenceCount : Int , linesOfCode : Int )
30+ def runLsifJava (arguments : List [String ]): Unit = {
31+ val baos = new ByteArrayOutputStream
32+ val exitCode = LsifJava
33+ .app
34+ .withReporter(ConsoleReporter (new PrintStream (baos)))
35+ .withEnv(
36+ LsifJava
37+ .app
38+ .env
39+ .withStandardOutput(new PrintStream (baos))
40+ .withStandardError(new PrintStream (baos))
41+ .withExit(i => throw new RuntimeException (i.toString()))
42+ )
43+ .run(arguments)
44+ if (exitCode != 0 ) {
45+ throw new RuntimeException (baos.toString())
46+ }
47+ }
2348
2449 override def run (context : SnapshotContext , handler : SnapshotHandler ): Unit = {
2550 val gen = new Gen (context, handler)
2651 gen.checkLibrary(
27- " com.airbnb.android:epoxy:4.3.1" ,
28- isIncluded = jar => jar.contains(" epoxy" )
52+ " com.airbnb.android:epoxy:4.3.1"
53+ // isIncluded = jar => jar.contains("epoxy")
2954 )
55+ // gen.checkLibrary("org.jetbrains.kotlin:kotlin-allopen:1.4.32")
3056 gen.checkLibrary(
3157 " com.lihaoyi:ujson_2.13:1.4.0" ,
3258 provided = List (
3359 s " org.scala-lang:scala-library: ${Properties .versionNumberString}"
34- ),
35- isIncluded = jar => jar.contains(" ujson" )
60+ )
3661 )
3762 }
63+
3864 private class Gen (context : SnapshotContext , handler : SnapshotHandler ) {
39- def checkLibrary (
40- name : String ,
41- provided : List [String ] = Nil ,
42- isIncluded : String => Boolean = _ => true
43- ): Unit = {
65+ def checkLibrary (name : String , provided : List [String ] = Nil ): Unit = {
4466 println(s " indexing library ' $name' " )
45- val deps = Dependencies .resolveDependencies(name :: provided)
46- val counter = new AtomicInteger ()
67+ val providedArguments = provided.flatMap(p => List (" --provided" , p))
4768 val targetroot = Files .createTempDirectory(" semanticdb-javac" )
48-
49- val compiler =
50- new TestCompiler (
51- deps.classpathSyntax,
52- javacOptions = List (" -Xlint:none" ),
53- scalacOptions = Nil ,
54- targetroot
69+ val indexDir = Files .createTempDirectory(" semanticdb-javac" )
70+ runLsifJava(
71+ List (
72+ " index-dependency" ,
73+ " --dependency" ,
74+ name,
75+ " --output" ,
76+ indexDir.toString()
77+ ) ++ providedArguments
78+ )
79+ val snapshotDir = Files .createTempDirectory(" semanticdb-javac" )
80+ val dumpDir = indexDir.toFile().listFiles().head.toPath
81+ val outputDir = snapshotDir.resolve(dumpDir.getFileName())
82+ runLsifJava(
83+ List (
84+ " snapshot-lsif" ,
85+ " --cwd" ,
86+ dumpDir.toString(),
87+ " --output" ,
88+ outputDir.toString()
5589 )
56- val timer = new Timer ()
57- val toIndex = deps.sources.filter(p => isIncluded(p.getFileName.toString))
58- toIndex.foreach { source =>
59- val metrics = compileSourcesJar(source, compiler)
60- val i = counter.incrementAndGet()
61- val message =
62- f " $i%3s/ ${toIndex.size} jars; $timer%6s; " +
63- f " ${metrics.occurrenceCount}%,.0f occurrences; " +
64- f " ${metrics.linesOfCode}%,.0f loc; " + f " ${source.getFileName}"
65- println(message)
66- }
67- Files .walkFileTree(targetroot, new DeleteVisitor ())
90+ )
91+ Files .walkFileTree(
92+ outputDir,
93+ new SimpleFileVisitor [Path ] {
94+ override def visitFile (
95+ file : Path ,
96+ attrs : BasicFileAttributes
97+ ): FileVisitResult = {
98+ val print =
99+ new String (Files .readAllBytes(file), StandardCharsets .UTF_8 )
100+ val out = context
101+ .expectDirectory
102+ .resolve(outputDir.relativize(file))
103+ handler.onSnapshotTest(context, out, () => print)
104+ super .visitFile(file, attrs)
105+ }
106+ }
107+ )
108+ // Files.walkFileTree(indexDir, new DeleteVisitor())
109+ // Files.walkFileTree(snapshotDir, new DeleteVisitor())
110+ // val deps = Dependencies.resolveDependencies(name :: provided)
111+ // val counter = new AtomicInteger()
112+
113+ // val compiler =
114+ // new TestCompiler(
115+ // deps.classpathSyntax,
116+ // javacOptions = List("-Xlint:none"),
117+ // scalacOptions = Nil,
118+ // targetroot
119+ // )
120+ // val timer = new Timer()
121+ // val toIndex = deps.sources.filter(p => isIncluded(p.getFileName.toString))
122+ // toIndex.foreach { source =>
123+ // val metrics = compileSourcesJar(source, compiler)
124+ // val i = counter.incrementAndGet()
125+ // val message =
126+ // f"$i%3s/${toIndex.size} jars; $timer%6s; " +
127+ // f"${metrics.occurrenceCount}%,.0f occurrences; " +
128+ // f"${metrics.linesOfCode}%,.0f loc; " + f"${source.getFileName}"
129+ // println(message)
130+ // }
68131 }
69132
70133 private def compileSourcesJar (
0 commit comments