@@ -5,59 +5,99 @@ import scala.tasty.file._
5
5
import scala .NotImplementedError
6
6
7
7
import dotty .tools .dotc .Driver
8
+ import dotty .tools .dotc .reporting .Reporter
9
+
10
+ import java .io .File
11
+ import java .nio .file ._
8
12
9
13
object Main {
10
- def parseArguments (args : Array [String ]): (Map [String , String ], Boolean ) = {
11
- def nextArgument (optionMap : Map [String , String ], args : List [String ]): Map [String , String ] = args match {
12
- case " --out" :: file :: tail => nextArgument(optionMap + (" outfile" -> file), tail)
13
- case " -o" :: file :: tail => nextArgument(optionMap + (" outfile" -> file), tail)
14
+ val userHome = System .getProperty(" user.home" )
15
+ val classpaths =
16
+ userHome + " /.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.8.jar" ::
17
+ " out/bootstrap/dotty-library-bootstrapped/scala-0.12/dotty-library_0.12-0.12.0-bin-SNAPSHOT.jar" :: Nil
18
+
19
+ val help = """ Usage semanticdb [options] [file]
20
+ |Generate semanticdb's information related to the source file [file]
21
+ |Options are:
22
+ | -h,--help Show help
23
+ | -o <file>, --out <file> Place the output into <file> (default: out.semanticdb)
24
+ | -t <folder>, --temp <folder> Use <folder> as the temp directory to store build artifacts
25
+ """ .stripMargin
26
+
27
+ type CliArgs = Map [String , String ]
28
+
29
+ def parseArguments (args : Array [String ]): Option [CliArgs ] = {
30
+ val optRegex = " $-.*" .r
31
+ def nextArgument (optionMap : CliArgs , args : List [String ]): Option [CliArgs ] = args match {
32
+ case " --out" :: file :: tail => nextArgument(optionMap + (" out" -> file), tail)
33
+ case " -o" :: file :: tail => nextArgument(optionMap + (" out" -> file), tail)
14
34
case " --help" :: tail => nextArgument(optionMap + (" help" -> " " ), tail)
15
35
case " -h" :: tail => nextArgument(optionMap + (" help" -> " " ), tail)
16
- case " --temp" :: folder :: tail => nextArgument(optionMap + (" temp" -> folder), tail)
17
- case " -t" :: folder :: tail => nextArgument(optionMap + (" temp" -> folder), tail)
36
+ case " --classpath" :: folder :: tail => nextArgument(optionMap + (" classpath" -> folder), tail)
37
+ case " -c" :: folder :: tail => nextArgument(optionMap + (" classpath" -> folder), tail)
38
+ case optRegex(_) :: _=> None
18
39
case file :: tail => nextArgument(optionMap + (" input" -> file), tail)
19
- case Nil => optionMap
40
+ case Nil => Some ( optionMap)
20
41
}
21
42
22
- val help = """ Usage semanticdb [options] [file]
23
- |Generate semanticdb's information related to the source file [file]
24
- |Options are:
25
- | -h,--help Show help
26
- | -o <file>, --out <file> Place the output into <file>
27
- | -t <folder>, --temp <folder> Use <folder> as the temp directory to store build artifacts
28
- """ .stripMargin
29
-
30
- var optionMap = nextArgument(Map (), args.toList)
31
- if (optionMap.contains(" help" ) || ! optionMap.contains(" input" )) {
32
- println(help)
33
- return (optionMap, false )
34
- } else {
35
- if (! optionMap.contains(" temp" )) {
36
- optionMap += (" temp" -> " /home/pierre/epfl/semester/dotty/semanticdb/testClass/" )
43
+ nextArgument(Map (), args.toList) match {
44
+ case Some (args : CliArgs ) => {
45
+ var cleanedArgs = args
46
+ cleanedArgs += " out" -> cleanedArgs.getOrElse(" out" , " out.semanticdb" )
47
+ if (cleanedArgs.contains(" help" ) || ! cleanedArgs.contains(" input" )) {
48
+ return None
49
+ } else {
50
+ cleanedArgs += " classpath" -> cleanedArgs.getOrElse(" classpath" , Files .createTempDirectory(" semanticdb" ).toString)
51
+ val tempFolder = new File (cleanedArgs(" classpath" ));
52
+ if (! tempFolder.exists()){
53
+ tempFolder.mkdir();
54
+ }
55
+ return Some (cleanedArgs)
37
56
}
38
- val driver = new Driver
39
- val compilerParams : List [String ] =
40
- optionMap(" input" ) ::
41
- " -Yno-inline" ::
42
- " -d" :: optionMap(" temp" ) ::
43
- " -classpath" :: " /home/pierre/epfl/semester/dotty/library/../out/bootstrap/dotty-library-bootstrapped/scala-0.12/dotty-library_0.12-0.12.0-bin-SNAPSHOT.jar" ::
44
- Nil
45
-
46
- val foo = driver.process(compilerParams.toArray)
47
- return (optionMap, true )
48
57
}
58
+ case None => None
59
+ }
60
+ }
61
+
62
+ def compile (cliArgs : CliArgs ) : Reporter
63
+ = {
64
+ val driver = new Driver
65
+ val compilerParams : List [String ] =
66
+ " -classpath" :: classpaths.mkString(" :" ) ::
67
+ " -Yno-inline" ::
68
+ " -d" :: cliArgs(" classpath" ) ::
69
+ cliArgs(" input" ) ::
70
+ Nil
71
+
72
+ val reporter = driver.process(compilerParams.toArray)
73
+ return reporter
49
74
}
50
75
51
76
52
77
def main (args : Array [String ]): Unit = {
53
78
val extraClasspath = " ." // TODO allow to set it from the args with -classpath XYZ
54
79
val classes = args.toList
55
- val (optionMap, canTreat) = parseArguments(args)
56
- if (args.isEmpty) {
57
- println(" Dotty Semantic DB: No classes where passed as argument" )
58
- } else {
59
- println(" Running Dotty Semantic DB on: " + args.mkString(" " ))
60
- // ConsumeTasty(extraClasspath, classes, new SemanticdbConsumer())
80
+
81
+
82
+ parseArguments(args) match {
83
+ case None => println(help)
84
+ case Some (cliArgs) => {
85
+ val reporter = compile(cliArgs)
86
+
87
+ if (reporter.hasErrors) {
88
+ println(" Compile error:" )
89
+ println(reporter)
90
+ } else {
91
+ val classNames = Utils .getClassNames(cliArgs(" classpath" ), cliArgs(" input" ))
92
+ val scalaFile = Paths .get(cliArgs(" input" )).toAbsolutePath
93
+ val sdbconsumer = new SemanticdbConsumer (scalaFile)
94
+ val _ = ConsumeTasty (cliArgs(" classpath" ), classNames, sdbconsumer)
95
+ val textDocument = sdbconsumer.toSemanticdb()
96
+ val os = Files .newOutputStream(Paths .get(cliArgs(" out" )))
97
+ try textDocument.writeTo(os)
98
+ finally os.close()
99
+ }
100
+ }
61
101
}
62
102
}
63
103
}
0 commit comments