-
Notifications
You must be signed in to change notification settings - Fork 18
/
Main.scala
182 lines (146 loc) · 5.89 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package tscfg
import java.io.{File, PrintWriter}
import java.util.Date
import com.typesafe.config.ConfigFactory
import tscfg.generators.java.JavaGen
import tscfg.generators.scala.ScalaGen
import tscfg.generators.{GenOpts, Generator, TemplateOpts, TemplateGenerator}
/**
* The main program. Run with no arguments to see usage.
*/
object Main {
val version: String = ConfigFactory.load().getString("tscfg.version")
val defaultGenOpts = GenOpts(
packageName = "tscfg.example",
className = "ExampleCfg",
j7 = false
)
val defaultDestDir: String = "/tmp"
var templateOpts = TemplateOpts()
val usage: String =
s"""
|tscfg $version
|Usage: tscfg.Main --spec inputFile [options]
|Options (default):
| --pn <packageName> (${defaultGenOpts.packageName})
| --cn <className> (${defaultGenOpts.className})
| --dd <destDir> ($defaultDestDir)
| --j7 generate code for java <= 7 (8)
| --scala generate scala code (java)
| --scala:` use backsticks (false)
| --java generate java code (the default)
| --tpl <filename> generate config template (no default)
| --tpl.ind <string> template indentation string ("${templateOpts.indent}")
| --tpl.cp <string> prefix for template comments ("${templateOpts.commentPrefix}")
|Output is written to $$destDir/$$className.ext
""".stripMargin
case class CmdLineOpts(inputFilename: Option[String] = None,
packageName: String = defaultGenOpts.packageName,
className: String = defaultGenOpts.className,
destDir: String = defaultDestDir,
j7: Boolean = false,
language: String = "java",
useBacksticks: Boolean = false,
tplFilename: Option[String] = None
)
def main(args: Array[String]): Unit = {
generate(getOpts(args.toList))
}
private def getOpts(args: List[String]): CmdLineOpts = {
if (args.isEmpty) {
println(usage)
sys.exit(0)
}
def traverseList(list: List[String], opts: CmdLineOpts = CmdLineOpts()): CmdLineOpts = {
def chkVal(v: String): String = {
if (!v.startsWith("-")) v
else {
println( s"""error: argument looks like a switch: $v""")
sys.exit(0)
}
}
list match {
case "--spec" :: filename :: rest =>
traverseList(rest, opts.copy(inputFilename = Some(chkVal(filename))))
case "--pn" :: packageName :: rest =>
traverseList(rest, opts.copy(packageName = chkVal(packageName)))
case "--cn" :: className :: rest =>
traverseList(rest, opts.copy(className = chkVal(className)))
case "--dd" :: destDir :: rest =>
traverseList(rest, opts.copy(destDir = chkVal(destDir)))
case "--j7" :: rest =>
traverseList(rest, opts.copy(j7 = true))
case "--scala" :: rest =>
traverseList(rest, opts.copy(language = "scala"))
case "--scala:`" :: rest =>
traverseList(rest, opts.copy(useBacksticks = true))
case "--java" :: rest =>
traverseList(rest, opts.copy(language = "java"))
case "--tpl" :: filename :: rest =>
traverseList(rest, opts.copy(tplFilename = Some(filename)))
case "--tpl.ind" :: indent :: rest =>
templateOpts = templateOpts.copy(indent = indent)
traverseList(rest, opts)
case "--tpl.cp" :: prefixComment :: rest =>
templateOpts = templateOpts.copy(commentPrefix = prefixComment)
traverseList(rest, opts)
case opt :: _ =>
println( s"""missing argument or unknown option: $opt""")
sys.exit(0)
case Nil => opts
}
}
val opts = traverseList(args)
opts.inputFilename.getOrElse {
println("--spec not given")
sys.exit(1)
}
opts
}
private def generate(opts: CmdLineOpts): Unit = {
require(opts.inputFilename.isDefined)
val ext = opts.language
val inputFilename = opts.inputFilename.get
val destFilename = s"${opts.destDir}/${opts.className}.$ext"
val destFile = new File(destFilename)
val out = new PrintWriter(destFile)
val genOpts = GenOpts(opts.packageName, opts.className, opts.j7,
useBacksticks = opts.useBacksticks)
println(s"parsing: $inputFilename")
val source = io.Source.fromFile(new File(inputFilename)).mkString.trim
val buildResult = ModelBuilder(source)
val objectType = buildResult.objectType
//println("\nobjectType:\n |" + objectType.format().replaceAll("\n", "\n |"))
if (buildResult.warnings.nonEmpty) {
println("WARNINGS:")
buildResult.warnings.foreach(w ⇒ println(s" line ${w.line}: ${w.source}: ${w.message}"))
}
println(s"generating: $destFile")
val pw = out match {
case w: PrintWriter => w
case w => new PrintWriter(w)
}
val generator: Generator = opts.language match {
case "java" => new JavaGen(genOpts)
case "scala" => new ScalaGen(genOpts)
}
val results = generator.generate(objectType)
pw.println(
s"""// generated by tscfg $version on ${new Date()}
|// source: $inputFilename
|
|${results.code}
""".stripMargin
)
out.close()
opts.tplFilename foreach { filename ⇒
println(s"generating template $filename")
val destFile = new File(filename)
val out = new PrintWriter(destFile)
val templater = new TemplateGenerator(templateOpts)
val template = templater.generate(objectType)
out.println(template)
out.close()
}
}
}