added opts for input

This commit is contained in:
Tony Tam 2014-05-15 08:51:41 -04:00
parent 58b985ba02
commit 9e6ae6eef8
5 changed files with 73 additions and 23 deletions

View File

@ -42,12 +42,15 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
override def apiPackage: Option[String] = Some("com.wordnik.client.api")
var codegen = new Codegen(this)
var fileMap: Option[String] = None
@deprecated(message = "please use the generate function", since = "2.0.16")
def generateClient(args: Array[String]): Unit = {
generateClientWithoutExit(args)
System.exit(0)
}
@deprecated(message = "please use the generate function", since = "2.0.16")
def generateClientWithoutExit(args: Array[String]): Seq[File] = {
if (args.length == 0) {
throw new RuntimeException("Need url to resource listing as argument. You can also specify VM Argument -DfileMap=/path/to/folder/containing.resources.json/")
@ -55,9 +58,27 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
val host = args(0)
val apiKey = if(args.length > 1) Some(args(1)) else None
val authorization = authenticate(apiKey)
val opts = new ClientOpts()
opts.uri = host
opts.auth = authorization
opts.properties = Map("fileMap" -> sys.props("fileMap"))
generate(opts)
}
def generate(opts: ClientOpts) = {
if (opts == null) {
throw new RuntimeException("Need url to resource listing as argument. You can also specify VM Argument -DfileMap=/path/to/folder/containing.resources.json/")
}
val host = opts.uri
val authorization = opts.auth
fileMap = Option(opts.properties.getOrElse("fileMap", null))
val doc = {
try {
ResourceExtractor.fetchListing(getResourcePath(host), authorization)
ResourceExtractor.fetchListing(getResourcePath(host, fileMap), authorization)
} catch {
case e: Exception => throw new Exception("unable to read from " + host, e)
}
@ -78,20 +99,23 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
}
case 0 =>
}
implicit val basePath = getBasePath(host, doc.basePath)
implicit val basePath = getBasePath(host, doc.basePath, fileMap)
new SwaggerSpecValidator(doc, apis).validate()
val allModels = new HashMap[String, Model]
val operations = extractApiOperations(apis, allModels)
val operationMap = groupOperationsToFiles(operations)
val modelBundle = prepareModelMap(allModels.toMap)
val modelFiles = bundleToSource(modelBundle, modelTemplateFiles.toMap)
val modelInfo = bundleToSource(modelBundle, modelTemplateFiles.toMap)
val modelFiles = new ListBuffer[File]()
modelFiles.map(m => {
modelInfo.map(m => {
val filename = m._1
val file = new java.io.File(filename)
modelFiles += file
file.getParentFile().mkdirs
val fw = new FileWriter(filename, false)
@ -101,12 +125,14 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
})
val apiBundle = prepareApiBundle(operationMap.toMap)
val apiFiles = bundleToSource(apiBundle, apiTemplateFiles.toMap)
val apiInfo = bundleToSource(apiBundle, apiTemplateFiles.toMap)
val apiFiles = new ListBuffer[File]()
apiFiles.map(m => {
apiInfo.map(m => {
val filename = m._1
val file = new java.io.File(filename)
apiFiles += file
file.getParentFile().mkdirs
val fw = new FileWriter(filename, false)
@ -115,12 +141,12 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
println("wrote api " + filename)
})
codegen.writeSupportingClasses(operationMap, allModels.toMap, doc.apiVersion)
codegen.writeSupportingClasses(operationMap, allModels.toMap, doc.apiVersion) ++
modelFiles ++ apiFiles
}
def getApis(host: String, doc: ResourceListing, authorization: Option[ApiKeyValue]): List[ApiListing] = {
implicit val basePath = getBasePath(host, doc.basePath)
implicit val basePath = getBasePath(host, doc.basePath, fileMap)
println("base path is " + basePath)
val apiReferences = doc.apis

View File

@ -17,16 +17,16 @@
package com.wordnik.swagger.codegen
trait PathUtil {
def getResourcePath(host: String) = {
System.getProperty("fileMap") match {
case s: String => s
def getResourcePath(host: String, fileMap: Option[String] = None) = {
fileMap match {
case Some(s) => s
case _ => host
}
}
def getBasePath(host: String, basePath: String) = {
System.getProperty("fileMap") match {
case s: String => {
def getBasePath(host: String, basePath: String, fileMap: Option[String] = None) = {
fileMap match {
case Some(s) => {
// return the parent folder
val f = new java.io.File(s)
f.getParent

View File

@ -275,11 +275,10 @@ class ScalaAsyncClientGenerator(cfg: SwaggerGenConfig) extends BasicGenerator {
codegen = new AsyncClientCodegen(cfg.api.clientName, this, Some(cfg.projectRoot))
override def getBasePath(host: String, basePath: String): String =
cfg.api.baseUrl.getOrElse(super.getBasePath(host, basePath))
override def getBasePath(host: String, basePath: String, fileMap: Option[String]): String =
cfg.api.baseUrl.getOrElse(super.getBasePath(host, basePath, fileMap))
override def generateClient(args: Array[String]) = {
val host = cfg.api.resourceUrl
val authorization = {
val apiKey = cfg.api.apiKey
@ -291,13 +290,13 @@ class ScalaAsyncClientGenerator(cfg: SwaggerGenConfig) extends BasicGenerator {
val doc = {
try {
ResourceExtractor.fetchListing(getResourcePath(host), authorization)
ResourceExtractor.fetchListing(getResourcePath(host, fileMap), authorization)
} catch {
case e: Exception => throw new Exception("unable to read from " + host, e)
}
}
implicit val basePath = getBasePath(host, doc.basePath)
implicit val basePath = getBasePath(host, doc.basePath, fileMap)
val apiReferences = doc.apis
if (apiReferences == null)

View File

@ -0,0 +1,23 @@
package com.wordnik.swagger.codegen.model
import scala.reflect.BeanProperty
import scala.collection.JavaConverters._
class ClientOpts(
@BeanProperty var uri: String,
@BeanProperty var auth: Option[ApiKeyValue],
@BeanProperty var properties: java.util.Map[String, String]) {
def this() = this(null, None, new java.util.HashMap[String, String]())
@BeanProperty var outputDirectory: String = _
override def toString() = {
val sb = new StringBuilder()
sb.append("ClientOpts: {\n")
sb.append(" uri: ").append(uri).append(",")
sb.append(" auth: ").append(auth).append(",")
sb.append(properties.asScala.mkString(" ", ",", "\n"))
sb.append("}")
sb.toString
}
}

View File

@ -28,6 +28,8 @@ object Validator extends PathUtil {
throw new RuntimeException("Need url to Resource Listing as argument. You can also specify VM Argument -DfileMap=/path/to/resourceListing")
}
val host = args(0)
val fileMap = Option(sys.props("fileMap"))
val authorization = {
Option (System.getProperty("header")) match {
case Some(e) => {
@ -50,13 +52,13 @@ object Validator extends PathUtil {
}
val doc = {
try {
ResourceExtractor.fetchListing(getResourcePath(host), authorization)
ResourceExtractor.fetchListing(getResourcePath(host, fileMap), authorization)
} catch {
case e: Exception => throw new Exception("unable to read from " + host, e)
}
}
val basePath = getBasePath(host, doc.basePath)
val basePath = getBasePath(host, doc.basePath, fileMap)
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, doc.apis, authorization)
val swaggerSpecValidator = new SwaggerSpecValidator(doc, apis, false)
swaggerSpecValidator.validate()