Skip to content

Commit 20b9d26

Browse files
authored
Merge pull request #276 from yisraelU/RunnerExtraction
Runner Module extraction
2 parents 2efa5e8 + c32c1ac commit 20b9d26

File tree

13 files changed

+159
-104
lines changed

13 files changed

+159
-104
lines changed

build.sc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ trait OpenApiModule
9494
}
9595
}
9696

97+
object runners extends Cross[RunnersModule](scalaVersions)
98+
trait RunnersModule
99+
extends CrossScalaModule
100+
with BaseScalaModule
101+
with BasePublishModule {
102+
103+
def publishArtifactName = "smithytranslate-runners"
104+
105+
def ivyDeps = Agg(buildDeps.lihaoyi.oslib, buildDeps.lihaoyi.ujson,buildDeps.coursier(scalaVersion()))
106+
107+
def moduleDeps =
108+
Seq(`compiler-core`(), openapi(), proto(), `json-schema`(), formatter.jvm())
109+
}
110+
97111
object cli
98112
extends BaseScala213Module
99113
with buildinfo.BuildInfo
@@ -103,17 +117,11 @@ object cli
103117

104118
def moduleDeps =
105119
Seq(
106-
openapi(scala213),
107-
proto(scala213),
108-
`json-schema`(scala213),
109-
formatter.jvm(scala213)
120+
runners(scala213)
110121
)
111122

112123
def ivyDeps = Agg(
113124
buildDeps.decline,
114-
buildDeps.coursier,
115-
buildDeps.lihaoyi.oslib,
116-
buildDeps.lihaoyi.ujson,
117125
buildDeps.smithy.build
118126
)
119127

buildDeps.sc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ object scalapb {
5858
ivy"com.thesamet.scalapb::compilerplugin:$version"
5959
val protocCache = ivy"com.thesamet.scalapb::protoc-cache-coursier:0.9.6"
6060
}
61-
val coursier = ivy"io.get-coursier::coursier:2.1.8"
61+
def coursier(scalaVersion: String) =
62+
if (scalaVersion.startsWith("3."))
63+
ivy"io.get-coursier:coursier_2.13:2.1.8"
64+
else
65+
ivy"io.get-coursier::coursier:2.1.8"

modules/cli/src/Main.scala

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import smithytranslate.cli.opts.SmithyTranslateCommand.{
3030
ProtoTranslate,
3131
Version
3232
}
33-
import smithytranslate.cli.runners.{OpenApi, Proto}
34-
import smithytranslate.cli.runners.formatter.Formatter
33+
import smithytranslate.runners.{OpenApi, Proto}
34+
import smithytranslate.runners.formatter.Formatter
35+
import smithytranslate.formatter.parsers.op
3536

3637
object Main
3738
extends smithytranslate.cli.CommandApp(
@@ -48,15 +49,40 @@ object Main
4849
cli map {
4950
case OpenApiTranslate(opts) =>
5051
if (opts.isOpenapi)
51-
OpenApi.runOpenApi(opts)
52+
OpenApi.runOpenApi(
53+
opts.inputFiles,
54+
opts.outputPath,
55+
opts.useVerboseNames,
56+
opts.validateInput,
57+
opts.validateOutput,
58+
opts.useEnumTraitSyntax,
59+
opts.outputJson,
60+
opts.debug
61+
)
5262
else
53-
OpenApi.runJsonSchema(opts)
63+
OpenApi.runJsonSchema(
64+
opts.inputFiles,
65+
opts.outputPath,
66+
opts.useVerboseNames,
67+
opts.validateInput,
68+
opts.validateOutput,
69+
opts.useEnumTraitSyntax,
70+
opts.outputJson,
71+
opts.debug
72+
)
5473
SmithyBuildJsonWriter.writeDefault(opts.outputPath, opts.force)
5574

5675
case ProtoTranslate(opts) =>
57-
Proto.runFromCli(opts)
76+
Proto.runProto(
77+
opts.inputFiles.toList,
78+
opts.outputPath,
79+
opts.deps,
80+
opts.repositories
81+
)
5882

59-
case Format(opts) => Formatter.run(opts)
83+
case Format(opts) =>
84+
Formatter
85+
.run(opts.smithyFile.toList, opts.noClobber, opts.validateModel)
6086

6187
case Version => println(BuildInfo.cliVersion)
6288
}

modules/cli/src/runners/OpenApi.scala

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
* limitations under the License.
1414
*/
1515
package smithytranslate
16-
package cli
1716
package runners
18-
import cats.data
1917
import cats.data.NonEmptyList
2018
import smithytranslate.compiler.FileContents
2119

modules/runners/src/OpenApi.scala

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright 2022 Disney Streaming
2+
*
3+
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* https://disneystreaming.github.io/TOST-1.0.txt
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package smithytranslate.runners
17+
18+
import cats.data.NonEmptyList
19+
import smithytranslate.runners.transformer.TransformerLookup
20+
import smithytranslate.runners.openapi._
21+
22+
object OpenApi {
23+
24+
def runOpenApi(
25+
inputFiles: NonEmptyList[os.Path],
26+
outputPath: os.Path,
27+
useVerboseNames: Boolean,
28+
validateInput: Boolean,
29+
validateOutput: Boolean,
30+
useEnumTraitSyntax: Boolean,
31+
outputJson: Boolean,
32+
debug: Boolean
33+
): Unit = {
34+
val transformers = TransformerLookup.getAll()
35+
36+
val report = ReportResult(outputPath, outputJson).apply _
37+
38+
report(
39+
ParseAndCompile.openapi(
40+
inputFiles,
41+
useVerboseNames = useVerboseNames,
42+
validateInput = validateInput,
43+
validateOutput = validateOutput,
44+
transformers,
45+
useEnumTraitSyntax,
46+
debug
47+
),
48+
debug
49+
)
50+
}
51+
52+
def runJsonSchema(
53+
inputFiles: NonEmptyList[os.Path],
54+
outputPath: os.Path,
55+
useVerboseNames: Boolean,
56+
validateInput: Boolean,
57+
validateOutput: Boolean,
58+
useEnumTraitSyntax: Boolean,
59+
outputJson: Boolean,
60+
debug: Boolean
61+
): Unit = {
62+
val transformers = TransformerLookup.getAll()
63+
64+
val report = ReportResult(outputPath, outputJson).apply _
65+
report(
66+
ParseAndCompile.jsonSchema(
67+
inputFiles,
68+
useVerboseNames = useVerboseNames,
69+
validateInput = validateInput,
70+
validateOutput = validateOutput,
71+
transformers,
72+
useEnumTraitSyntax,
73+
debug
74+
),
75+
debug
76+
)
77+
}
78+
}
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
package smithytranslate.cli.runners
16+
package smithytranslate.runners
1717

18-
import smithytranslate.cli.opts.ProtoOpts
19-
import smithytranslate.cli.transformer.TransformerLookup
18+
import smithytranslate.runners.transformer.TransformerLookup
2019
import smithytranslate.proto3.*
2120
import java.net.URLClassLoader
2221

@@ -28,7 +27,12 @@ object Proto {
2827
/** Builds a model from the CLI options, transforms it and then run the
2928
* conversion.
3029
*/
31-
def runFromCli(opts: ProtoOpts): Unit = {
30+
def runProto(
31+
inputFiles: List[os.Path],
32+
outputPath: os.Path,
33+
deps: List[String],
34+
repositories: List[String]
35+
): Unit = {
3236
val transformers = TransformerLookup.getAll()
3337
val currentClassLoader = this.getClass().getClassLoader()
3438
val modelBuilder =
@@ -38,12 +42,12 @@ object Proto {
3842
.assemble()
3943
.unwrap()
4044
.toBuilder()
41-
Deps.forDeps(opts.deps, opts.repositories)(modelBuilder)
45+
Deps.forDeps(deps, repositories)(modelBuilder)
4246

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

4549
val model0 =
46-
opts.inputFiles
50+
inputFiles
4751
.foldLeft(modelAssembler.discoverModels) { case (m, path) =>
4852
m.addImport(path.toNIO)
4953
}
@@ -54,7 +58,7 @@ object Proto {
5458
transfomer.transform(TransformContext.builder().model(m).build())
5559
)
5660

57-
run(model, opts.outputPath)
61+
run(model, outputPath)
5862
}
5963

6064
/** Transforms the given model, then run the conversion.

modules/cli/src/runners/formatter/Formatter.scala renamed to modules/runners/src/formatter/Formatter.scala

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
*/
1515

1616
package smithytranslate
17-
package cli
1817
package runners
1918
package formatter
2019

2120
import cats.implicits.toBifunctorOps
2221
import os.Path
23-
import smithytranslate.cli.opts.FormatterOpts.FormatOpts
24-
import smithytranslate.cli.runners.formatter.FormatterError.{
22+
import smithytranslate.runners.formatter.FormatterError.{
2523
InvalidModel,
2624
UnableToParse,
2725
UnableToReadFile
@@ -37,15 +35,16 @@ import scala.collection.compat._
3735

3836
object Formatter {
3937

40-
def run(formatOpts: FormatOpts): Unit = {
41-
formatOpts match {
42-
case FormatOpts(files, noClobber, validateModel, _) =>
43-
files.toList.foreach(
44-
reformat(_, noClobber, validateModel).report()
45-
)
46-
47-
}
38+
def run(
39+
files: List[os.Path],
40+
noClobber: Boolean,
41+
validateModel: Boolean
42+
): Unit = {
43+
files.foreach(
44+
reformat(_, noClobber, validateModel).report()
45+
)
4846
}
47+
4948
private def reformat(
5049
smithyWorkspacePath: os.Path,
5150
noClobber: Boolean,

modules/cli/src/runners/formatter/Report.scala renamed to modules/runners/src/formatter/Report.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
*/
1515

1616
package smithytranslate
17-
package cli
1817
package runners
1918
package formatter
2019

2120
import os.Path
22-
import smithytranslate.cli.runners.formatter.FormatterError.{
21+
import smithytranslate.runners.formatter.FormatterError.{
2322
InvalidModel,
2423
UnableToParse,
2524
UnableToReadFile
2625
}
27-
import smithytranslate.cli.runners.formatter.Report.logError
26+
import smithytranslate.runners.formatter.Report.logError
2827

2928
case class Report(success: List[Path], errors: List[FormatterError]) {
3029
override def toString: String = {

modules/cli/src/runners/openapi/ParseAndCompile.scala renamed to modules/runners/src/openapi/ParseAndCompile.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
* limitations under the License.
1414
*/
1515

16-
package smithytranslate.cli.runners.openapi
16+
package smithytranslate.runners.openapi
1717

1818
import cats.data.NonEmptyList
19-
import smithytranslate.cli.runners.FileUtils.readAll
19+
import smithytranslate.runners.FileUtils.readAll
2020
import smithytranslate.compiler.openapi.OpenApiCompiler
21-
import smithytranslate.cli.transformer.TranslateTransformer
21+
import smithytranslate.runners.transformer.TranslateTransformer
2222
import software.amazon.smithy.model.Model
2323
import smithytranslate.compiler.json_schema.JsonSchemaCompiler
2424
import smithytranslate.compiler.ToSmithyResult

0 commit comments

Comments
 (0)