|
| 1 | +/* |
| 2 | + * Copyright 2020 Twitter, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | + * not use this file except in compliance with the License. You may obtain |
| 6 | + * a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.bazel.rules_scala.scrooge_support |
| 17 | + |
| 18 | +import com.twitter.scrooge.backend.{ |
| 19 | + GeneratorFactory, |
| 20 | + WithFinagle, |
| 21 | + WithJavaPassThrough, |
| 22 | + ServiceOption |
| 23 | +} |
| 24 | +import java.io.File |
| 25 | +import java.util.Properties |
| 26 | +import scopt.OptionParser |
| 27 | + |
| 28 | +case class ScroogeConfig( |
| 29 | + destFolder: String = ".", |
| 30 | + includePaths: List[String] = List(), |
| 31 | + thriftFiles: List[String] = List(), |
| 32 | + flags: Set[ServiceOption] = Set(), |
| 33 | + namespaceMappings: Map[String, String] = Map(), |
| 34 | + verbose: Boolean = false, |
| 35 | + strict: Boolean = true, |
| 36 | + genAdapt: Boolean = false, |
| 37 | + skipUnchanged: Boolean = false, |
| 38 | + languageFlags: Seq[String] = Seq(), |
| 39 | + fileMapPath: scala.Option[String] = None, |
| 40 | + dryRun: Boolean = false, |
| 41 | + language: String = "scala", |
| 42 | + defaultNamespace: String = "thrift", |
| 43 | + scalaWarnOnJavaNSFallback: Boolean = false, |
| 44 | + javaSerEnumType: Boolean = false) |
| 45 | + |
| 46 | +object ScroogeOptionParser { |
| 47 | + |
| 48 | + /** Optionally returns config with parsed values from args. |
| 49 | + * @param args command line arguments. |
| 50 | + * @param defaultConfig config with configurable defaults that is used to store parsed args. |
| 51 | + */ |
| 52 | + def parseOptions( |
| 53 | + args: Seq[String], |
| 54 | + defaultConfig: ScroogeConfig = ScroogeConfig() |
| 55 | + ): Option[ScroogeConfig] = { |
| 56 | + val buildProperties = new Properties |
| 57 | + scala.Option(getClass.getResource("build.properties")) foreach { resource => |
| 58 | + buildProperties.load(resource.openStream) |
| 59 | + } |
| 60 | + |
| 61 | + val parser = new OptionParser[ScroogeConfig]("scrooge") { |
| 62 | + help("help").text("show this help screen") |
| 63 | + |
| 64 | + override def showUsageOnError: Option[Boolean] = Some(true) |
| 65 | + |
| 66 | + opt[Unit]('V', "version") |
| 67 | + .action { (_, c) => |
| 68 | + println("scrooge " + buildProperties.getProperty("version", "0.0")) |
| 69 | + println(" build " + buildProperties.getProperty("build_name", "unknown")) |
| 70 | + println(" git revision " + buildProperties.getProperty("build_revision", "unknown")) |
| 71 | + System.exit(0) |
| 72 | + c |
| 73 | + } |
| 74 | + .text("print version and quit") |
| 75 | + |
| 76 | + opt[Unit]('v', "verbose") |
| 77 | + .action((_, c) => c.copy(verbose = true)) |
| 78 | + .text("log verbose messages about progress") |
| 79 | + |
| 80 | + opt[String]('d', "dest") |
| 81 | + .valueName("<path>") |
| 82 | + .action((d, c) => c.copy(destFolder = d)) |
| 83 | + .text("write generated code to a folder (default: %s)".format(defaultConfig.destFolder)) |
| 84 | + |
| 85 | + opt[String]("import-path") |
| 86 | + .unbounded() |
| 87 | + .valueName("<path>") |
| 88 | + .action { (path, c) => |
| 89 | + val includePaths = path.split(File.pathSeparator) ++: c.includePaths |
| 90 | + c.copy(includePaths = includePaths) |
| 91 | + } |
| 92 | + .text( |
| 93 | + "[DEPRECATED] path(s) to search for included thrift files (may be used multiple times)" |
| 94 | + ) |
| 95 | + |
| 96 | + opt[String]('i', "include-path") |
| 97 | + .unbounded() |
| 98 | + .valueName("<path>") |
| 99 | + .action { (path, c) => |
| 100 | + val includePaths = path.split(File.pathSeparator) ++: c.includePaths |
| 101 | + c.copy(includePaths = includePaths) |
| 102 | + } |
| 103 | + .text("path(s) to search for included thrift files (may be used multiple times)") |
| 104 | + |
| 105 | + opt[String]('n', "namespace-map") |
| 106 | + .unbounded() |
| 107 | + .valueName("<oldname>=<newname>") |
| 108 | + .action { (mapping, c) => |
| 109 | + mapping.split("=") match { |
| 110 | + case Array(from, to) => |
| 111 | + c.copy(namespaceMappings = c.namespaceMappings + (from -> to)) |
| 112 | + } |
| 113 | + } |
| 114 | + .text("map old namespace to new (may be used multiple times)") |
| 115 | + |
| 116 | + opt[String]("default-java-namespace") |
| 117 | + .unbounded() |
| 118 | + .valueName("<name>") |
| 119 | + .action((name, c) => c.copy(defaultNamespace = name)) |
| 120 | + .text( |
| 121 | + "Use <name> as default namespace if the thrift file doesn't define its own namespace. " + |
| 122 | + "If this option is not specified either, then use \"thrift\" as default namespace" |
| 123 | + ) |
| 124 | + |
| 125 | + opt[Unit]("disable-strict") |
| 126 | + .action((_, c) => c.copy(strict = false)) |
| 127 | + .text("issue warnings on non-severe parse errors instead of aborting") |
| 128 | + |
| 129 | + opt[String]("gen-file-map") |
| 130 | + .valueName("<path>") |
| 131 | + .action((path, c) => c.copy(fileMapPath = Some(path))) |
| 132 | + .text( |
| 133 | + "generate map.txt in the destination folder to specify the mapping from input thrift files to output Scala/Java files" |
| 134 | + ) |
| 135 | + |
| 136 | + opt[Unit]("dry-run") |
| 137 | + .action((_, c) => c.copy(dryRun = true)) |
| 138 | + .text( |
| 139 | + "parses and validates source thrift files, reporting any errors, but" + |
| 140 | + " does not emit any generated source code. can be used with " + |
| 141 | + "--gen-file-mapping to get the file mapping" |
| 142 | + ) |
| 143 | + |
| 144 | + opt[Unit]('s', "skip-unchanged") |
| 145 | + .action((_, c) => c.copy(skipUnchanged = true)) |
| 146 | + .text("Don't re-generate if the target is newer than the input") |
| 147 | + |
| 148 | + opt[String]('l', "language") |
| 149 | + .action((languageString, c) => c.copy(language = languageString)) |
| 150 | + .validate { language => |
| 151 | + if (GeneratorFactory.languages.toList contains language.toLowerCase) |
| 152 | + success |
| 153 | + else |
| 154 | + failure("language option %s not supported".format(language)) |
| 155 | + } |
| 156 | + .text( |
| 157 | + "name of language to generate code in (currently supported languages: " + |
| 158 | + GeneratorFactory.languages.toList.mkString(", ") + ")" |
| 159 | + ) |
| 160 | + |
| 161 | + opt[Unit]("java-ser-enum-type") |
| 162 | + .action((_, c) => c.copy(javaSerEnumType = true)) |
| 163 | + .text("Encode a thrift enum as o.a.t.p.TType.ENUM instead of TType.I32") |
| 164 | + |
| 165 | + opt[String]("language-flag") |
| 166 | + .valueName("<flag>") |
| 167 | + .unbounded() |
| 168 | + .action { (flag, c) => |
| 169 | + val languageFlags = c.languageFlags :+ flag |
| 170 | + c.copy(languageFlags = languageFlags) |
| 171 | + } |
| 172 | + .text( |
| 173 | + "Pass arguments to supported language generators" |
| 174 | + ) |
| 175 | + |
| 176 | + opt[Unit]("scala-warn-on-java-ns-fallback") |
| 177 | + .action((_, c) => c.copy(scalaWarnOnJavaNSFallback = true)) |
| 178 | + .text("Print a warning when the scala generator falls back to the java namespace") |
| 179 | + |
| 180 | + opt[Unit]("finagle") |
| 181 | + .action((_, c) => c.copy(flags = c.flags + WithFinagle)) |
| 182 | + .text("generate finagle classes") |
| 183 | + |
| 184 | + opt[Unit]("gen-adapt") |
| 185 | + .action((_, c) => c.copy(genAdapt = true)) |
| 186 | + .text("Generate code for adaptive decoding for scala.") |
| 187 | + |
| 188 | + opt[Unit]("java-passthrough") |
| 189 | + .action((_, c) => c.copy(flags = c.flags + WithJavaPassThrough)) |
| 190 | + .text("Generate Java code with PassThrough") |
| 191 | + |
| 192 | + arg[String]("<files...>") |
| 193 | + .unbounded() |
| 194 | + .action { (files, c) => |
| 195 | + // append files to preserve the order of thrift files from command line. |
| 196 | + val thriftFiles = c.thriftFiles :+ files |
| 197 | + c.copy(thriftFiles = thriftFiles) |
| 198 | + } |
| 199 | + .text("thrift files to compile") |
| 200 | + } |
| 201 | + parser.parse(args, defaultConfig) |
| 202 | + } |
| 203 | +} |
0 commit comments