Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ trait OpenApiModule
}
}

object runners extends Cross[RunnersModule](scalaVersions)
trait RunnersModule
extends CrossScalaModule
with BaseScalaModule
with BasePublishModule {

def publishArtifactName = "smithytranslate-runners"

def ivyDeps = Agg(buildDeps.lihaoyi.oslib, buildDeps.lihaoyi.ujson,buildDeps.coursier(scalaVersion()))

def moduleDeps =
Seq(`compiler-core`(), openapi(), proto(), `json-schema`(), formatter.jvm())
}

object cli
extends BaseScala213Module
with buildinfo.BuildInfo
Expand All @@ -103,17 +117,11 @@ object cli

def moduleDeps =
Seq(
openapi(scala213),
proto(scala213),
`json-schema`(scala213),
formatter.jvm(scala213)
runners(scala213)
)

def ivyDeps = Agg(
buildDeps.decline,
buildDeps.coursier,
buildDeps.lihaoyi.oslib,
buildDeps.lihaoyi.ujson,
buildDeps.smithy.build
)

Expand Down
6 changes: 5 additions & 1 deletion buildDeps.sc
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ object scalapb {
ivy"com.thesamet.scalapb::compilerplugin:$version"
val protocCache = ivy"com.thesamet.scalapb::protoc-cache-coursier:0.9.6"
}
val coursier = ivy"io.get-coursier::coursier:2.1.8"
def coursier(scalaVersion: String) =
if (scalaVersion.startsWith("3."))
ivy"io.get-coursier:coursier_2.13:2.1.8"
else
ivy"io.get-coursier::coursier:2.1.8"
38 changes: 32 additions & 6 deletions modules/cli/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import smithytranslate.cli.opts.SmithyTranslateCommand.{
ProtoTranslate,
Version
}
import smithytranslate.cli.runners.{OpenApi, Proto}
import smithytranslate.cli.runners.formatter.Formatter
import smithytranslate.runners.{OpenApi, Proto}
import smithytranslate.runners.formatter.Formatter
import smithytranslate.formatter.parsers.op

object Main
extends smithytranslate.cli.CommandApp(
Expand All @@ -48,15 +49,40 @@ object Main
cli map {
case OpenApiTranslate(opts) =>
if (opts.isOpenapi)
OpenApi.runOpenApi(opts)
OpenApi.runOpenApi(
opts.inputFiles,
opts.outputPath,
opts.useVerboseNames,
opts.validateInput,
opts.validateOutput,
opts.useEnumTraitSyntax,
opts.outputJson,
opts.debug
)
else
OpenApi.runJsonSchema(opts)
OpenApi.runJsonSchema(
opts.inputFiles,
opts.outputPath,
opts.useVerboseNames,
opts.validateInput,
opts.validateOutput,
opts.useEnumTraitSyntax,
opts.outputJson,
opts.debug
)
SmithyBuildJsonWriter.writeDefault(opts.outputPath, opts.force)

case ProtoTranslate(opts) =>
Proto.runFromCli(opts)
Proto.runProto(
opts.inputFiles.toList,
opts.outputPath,
opts.deps,
opts.repositories
)

case Format(opts) => Formatter.run(opts)
case Format(opts) =>
Formatter
.run(opts.smithyFile.toList, opts.noClobber, opts.validateModel)

case Version => println(BuildInfo.cliVersion)
}
Expand Down
60 changes: 0 additions & 60 deletions modules/cli/src/runners/OpenApi.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
* limitations under the License.
*/
package smithytranslate
package cli
package runners
import cats.data
import cats.data.NonEmptyList
import smithytranslate.compiler.FileContents

Expand Down
78 changes: 78 additions & 0 deletions modules/runners/src/OpenApi.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smithytranslate.runners

import cats.data.NonEmptyList
import smithytranslate.runners.transformer.TransformerLookup
import smithytranslate.runners.openapi._

object OpenApi {

def runOpenApi(
inputFiles: NonEmptyList[os.Path],
outputPath: os.Path,
useVerboseNames: Boolean,
validateInput: Boolean,
validateOutput: Boolean,
useEnumTraitSyntax: Boolean,
outputJson: Boolean,
debug: Boolean
): Unit = {
val transformers = TransformerLookup.getAll()

val report = ReportResult(outputPath, outputJson).apply _

report(
ParseAndCompile.openapi(
inputFiles,
useVerboseNames = useVerboseNames,
validateInput = validateInput,
validateOutput = validateOutput,
transformers,
useEnumTraitSyntax,
debug
),
debug
)
}

def runJsonSchema(
inputFiles: NonEmptyList[os.Path],
outputPath: os.Path,
useVerboseNames: Boolean,
validateInput: Boolean,
validateOutput: Boolean,
useEnumTraitSyntax: Boolean,
outputJson: Boolean,
debug: Boolean
): Unit = {
val transformers = TransformerLookup.getAll()

val report = ReportResult(outputPath, outputJson).apply _
report(
ParseAndCompile.jsonSchema(
inputFiles,
useVerboseNames = useVerboseNames,
validateInput = validateInput,
validateOutput = validateOutput,
transformers,
useEnumTraitSyntax,
debug
),
debug
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* limitations under the License.
*/

package smithytranslate.cli.runners
package smithytranslate.runners

import smithytranslate.cli.opts.ProtoOpts
import smithytranslate.cli.transformer.TransformerLookup
import smithytranslate.runners.transformer.TransformerLookup
import smithytranslate.proto3.*
import java.net.URLClassLoader

Expand All @@ -28,7 +27,12 @@ object Proto {
/** Builds a model from the CLI options, transforms it and then run the
* conversion.
*/
def runFromCli(opts: ProtoOpts): Unit = {
def runProto(
inputFiles: List[os.Path],
outputPath: os.Path,
deps: List[String],
repositories: List[String]
): Unit = {
val transformers = TransformerLookup.getAll()
val currentClassLoader = this.getClass().getClassLoader()
val modelBuilder =
Expand All @@ -38,12 +42,12 @@ object Proto {
.assemble()
.unwrap()
.toBuilder()
Deps.forDeps(opts.deps, opts.repositories)(modelBuilder)
Deps.forDeps(deps, repositories)(modelBuilder)

val modelAssembler = Model.assembler().addModel(modelBuilder.build())

val model0 =
opts.inputFiles
inputFiles
.foldLeft(modelAssembler.discoverModels) { case (m, path) =>
m.addImport(path.toNIO)
}
Expand All @@ -54,7 +58,7 @@ object Proto {
transfomer.transform(TransformContext.builder().model(m).build())
)

run(model, opts.outputPath)
run(model, outputPath)
}

/** Transforms the given model, then run the conversion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
*/

package smithytranslate
package cli
package runners
package formatter

import cats.implicits.toBifunctorOps
import os.Path
import smithytranslate.cli.opts.FormatterOpts.FormatOpts
import smithytranslate.cli.runners.formatter.FormatterError.{
import smithytranslate.runners.formatter.FormatterError.{
InvalidModel,
UnableToParse,
UnableToReadFile
Expand All @@ -37,15 +35,16 @@ import scala.collection.compat._

object Formatter {

def run(formatOpts: FormatOpts): Unit = {
formatOpts match {
case FormatOpts(files, noClobber, validateModel, _) =>
files.toList.foreach(
reformat(_, noClobber, validateModel).report()
)

}
def run(
files: List[os.Path],
noClobber: Boolean,
validateModel: Boolean
): Unit = {
files.foreach(
reformat(_, noClobber, validateModel).report()
)
}

private def reformat(
smithyWorkspacePath: os.Path,
noClobber: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
*/

package smithytranslate
package cli
package runners
package formatter

import os.Path
import smithytranslate.cli.runners.formatter.FormatterError.{
import smithytranslate.runners.formatter.FormatterError.{
InvalidModel,
UnableToParse,
UnableToReadFile
}
import smithytranslate.cli.runners.formatter.Report.logError
import smithytranslate.runners.formatter.Report.logError

case class Report(success: List[Path], errors: List[FormatterError]) {
override def toString: String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* limitations under the License.
*/

package smithytranslate.cli.runners.openapi
package smithytranslate.runners.openapi

import cats.data.NonEmptyList
import smithytranslate.cli.runners.FileUtils.readAll
import smithytranslate.runners.FileUtils.readAll
import smithytranslate.compiler.openapi.OpenApiCompiler
import smithytranslate.cli.transformer.TranslateTransformer
import smithytranslate.runners.transformer.TranslateTransformer
import software.amazon.smithy.model.Model
import smithytranslate.compiler.json_schema.JsonSchemaCompiler
import smithytranslate.compiler.ToSmithyResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
* limitations under the License.
*/

package smithytranslate.cli.runners.openapi
package smithytranslate.runners.openapi

import smithytranslate.compiler.openapi.OpenApiCompiler
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.SmithyIdlModelSerializer
import scala.jdk.CollectionConverters._
Expand Down
Loading