|
| 1 | +#!/usr/bin/env scala |
| 2 | +//> using toolkit 0.6.0 |
| 3 | +//> using scala 3.3.1 |
| 4 | + |
| 5 | +import os.* |
| 6 | + |
| 7 | +object Debug: |
| 8 | + var enabled = false |
| 9 | + |
| 10 | +enum Command: |
| 11 | + case Clean, Generate, GenerateStrict, Upstream, Strict, Exit |
| 12 | + |
| 13 | +enum Target: |
| 14 | + case Specific(name: String) |
| 15 | + case All |
| 16 | + |
| 17 | +def say(message: String)(using pwd: Path) = |
| 18 | + val command = List("say", message) |
| 19 | + if Debug.enabled then println(s"Running command: ${command.mkString(" ")}") |
| 20 | + os.proc(command).call(stdout = os.Inherit, stderr = os.Inherit) |
| 21 | + |
| 22 | +case class Project( |
| 23 | + project: String, |
| 24 | + projectName: String, |
| 25 | + projectGroupId: String, |
| 26 | + projectArtifactId: String, |
| 27 | + projectMainPackage: String, |
| 28 | + projectVersion: String, |
| 29 | + generatorName: String, |
| 30 | + skipValidate: Boolean, |
| 31 | + additionalProps: String = "", |
| 32 | + schemaMappings: String = "", |
| 33 | + typeMappings: String = "", |
| 34 | + importMappings: String = "" |
| 35 | +) |
| 36 | + |
| 37 | +val projects = Map( |
| 38 | + "mattermost" -> Project( |
| 39 | + project = "mm-sttp4", |
| 40 | + projectName = "mattermost-scala", |
| 41 | + projectGroupId = "ma.chinespirit", |
| 42 | + projectArtifactId = "mattermost-scala", |
| 43 | + projectMainPackage = "ma.chinespirit.mm", |
| 44 | + projectVersion = "1.0.0-SNAPSHOT", |
| 45 | + generatorName = "scala-sttp4-jsoniter", |
| 46 | + skipValidate = false |
| 47 | + ), |
| 48 | + "kubernetes" -> Project( |
| 49 | + project = "kube-sttp4", |
| 50 | + projectName = "kubeapi-scala", |
| 51 | + projectGroupId = "ma.chinespirit", |
| 52 | + projectArtifactId = "kubeapi-scala", |
| 53 | + projectMainPackage = "ma.chinespirit.kube", |
| 54 | + projectVersion = "1.0.0-SNAPSHOT", |
| 55 | + generatorName = "scala-sttp4-jsoniter", |
| 56 | + skipValidate = false, |
| 57 | + schemaMappings = "io.k8s.apimachinery.pkg.util.intstr.IntOrString=IntOrString", |
| 58 | + typeMappings = "IntOrString=IntOrString", |
| 59 | + importMappings = "IntOrString=ma.chinespirit.kube.ext.IntOrString" |
| 60 | + ), |
| 61 | + "stripe" -> Project( |
| 62 | + project = "stripe-sttp4", |
| 63 | + projectName = "stripe-scala", |
| 64 | + projectGroupId = "ma.chinespirit", |
| 65 | + projectArtifactId = "stripe-scala", |
| 66 | + projectMainPackage = "ma.chinespirit.stripe", |
| 67 | + projectVersion = "1.0.0-SNAPSHOT", |
| 68 | + generatorName = "scala-sttp4-jsoniter", |
| 69 | + skipValidate = false |
| 70 | + ), |
| 71 | + "github" -> Project( |
| 72 | + project = "github-sttp4", |
| 73 | + projectName = "github-scala", |
| 74 | + projectGroupId = "ma.chinespirit", |
| 75 | + projectArtifactId = "github-scala", |
| 76 | + projectMainPackage = "ma.chinespirit.github", |
| 77 | + projectVersion = "1.0.0-SNAPSHOT", |
| 78 | + generatorName = "scala-sttp4-jsoniter", |
| 79 | + skipValidate = false |
| 80 | + ), |
| 81 | + "spotify" -> Project( |
| 82 | + project = "spotify-sttp4", |
| 83 | + projectName = "spotify-scala", |
| 84 | + projectGroupId = "ma.chinespirit", |
| 85 | + projectArtifactId = "spotify-scala", |
| 86 | + projectMainPackage = "ma.chinespirit.spotify", |
| 87 | + projectVersion = "1.0.0-SNAPSHOT", |
| 88 | + generatorName = "scala-sttp4-jsoniter", |
| 89 | + skipValidate = false |
| 90 | + ) |
| 91 | +) |
| 92 | + |
| 93 | +def cleanMaven(using pwd: Path) = |
| 94 | + val command = List("./mvnw", "clean") |
| 95 | + if Debug.enabled then println(s"Running command: ${command.mkString(" ")}") |
| 96 | + os.proc(command).call(stdout = os.Inherit, stderr = os.Inherit) |
| 97 | + |
| 98 | +def installMaven(using pwd: Path) = |
| 99 | + val command = List("./mvnw", "install", "-DskipTests", "-Dmaven.javadoc.skip=true") |
| 100 | + if Debug.enabled then println(s"Running command: ${command.mkString(" ")}") |
| 101 | + os.proc(command).call(stdout = os.Inherit, stderr = os.Inherit) |
| 102 | + |
| 103 | +def cleanupGeneratedFiles(project: Project, projectRootPath: Path) = |
| 104 | + val basePackage = os.SubPath(project.projectMainPackage.replace(".", "/")) |
| 105 | + os.remove.all(projectRootPath / "build.sbt") |
| 106 | + os.remove.all(projectRootPath / "target") |
| 107 | + os.remove.all(projectRootPath / "project") |
| 108 | + os.remove.all(projectRootPath / "README.md") |
| 109 | + os.remove.all(projectRootPath / "src" / "main" / "scala" / basePackage / "api") |
| 110 | + os.remove.all(projectRootPath / "src" / "main" / "scala" / basePackage / "model") |
| 111 | + os.remove.all(projectRootPath / "src" / "main" / "scala" / basePackage / "core") |
| 112 | + |
| 113 | +def runGeneratorJsoniter(project: Project, projectRootPath: Path, strict: Boolean = false)(using pwd: Path) = |
| 114 | + val additionalProps = { |
| 115 | + val base = s"mainPackage=${project.projectMainPackage},groupId=${project.projectGroupId},artifactId=${project.projectArtifactId},artifactVersion=${project.projectVersion}" |
| 116 | + val extended = if project.additionalProps.nonEmpty then s"$base,${project.additionalProps}" else base |
| 117 | + |
| 118 | + List(s"--additional-properties", extended) |
| 119 | + } |
| 120 | + val validateFlag = if project.skipValidate && !strict then List("--skip-validate-spec") else Nil |
| 121 | + val schemaMappings = if project.schemaMappings.nonEmpty then List("--schema-mappings", project.schemaMappings) else Nil |
| 122 | + val typeMappings = if project.typeMappings.nonEmpty then List("--type-mappings", project.typeMappings) else Nil |
| 123 | + val importMappings = if project.importMappings.nonEmpty then List("--import-mappings", project.importMappings) else Nil |
| 124 | + |
| 125 | + val command = List( |
| 126 | + List("java", "-jar", "modules/openapi-generator-cli/target/openapi-generator-cli.jar", "generate", |
| 127 | + "-i", s"$projectRootPath/openapi.json", |
| 128 | + "--generator-name", project.generatorName, |
| 129 | + "-o", projectRootPath.toString), |
| 130 | + validateFlag, |
| 131 | + additionalProps, |
| 132 | + schemaMappings, |
| 133 | + typeMappings, |
| 134 | + importMappings |
| 135 | + ).flatten |
| 136 | + |
| 137 | + if Debug.enabled then println(s"Running command: ${command.mkString(" ")}") |
| 138 | + os.proc(command).call(stdout = os.Inherit, stderr = os.Inherit) |
| 139 | + |
| 140 | +def runGeneratorUpstream(project: Project, projectRootPath: Path)(using pwd: Path) = |
| 141 | + val additionalProps = { |
| 142 | + val base = s"mainPackage=${project.projectMainPackage},groupId=${project.projectGroupId},artifactId=${project.projectArtifactId},artifactVersion=${project.projectVersion},jsonLibrary=circe" |
| 143 | + val extended = if project.additionalProps.nonEmpty then s"$base,${project.additionalProps}" else base |
| 144 | + |
| 145 | + List("--additional-properties", extended) |
| 146 | + } |
| 147 | + val validateFlag = if project.skipValidate then List("--skip-validate-spec") else Nil |
| 148 | + val schemaMappings = if project.schemaMappings.nonEmpty then List("--schema-mappings", project.schemaMappings) else Nil |
| 149 | + val typeMappings = if project.typeMappings.nonEmpty then List("--type-mappings", project.typeMappings) else Nil |
| 150 | + val importMappings = if project.importMappings.nonEmpty then List("--import-mappings", project.importMappings) else Nil |
| 151 | + |
| 152 | + val command = List( |
| 153 | + List("openapi-generator-cli", "generate", |
| 154 | + "-i", s"$projectRootPath/openapi.json", |
| 155 | + "--generator-name", "scala-sttp4", |
| 156 | + "-o", projectRootPath.toString), |
| 157 | + validateFlag, |
| 158 | + additionalProps, |
| 159 | + schemaMappings, |
| 160 | + typeMappings, |
| 161 | + importMappings |
| 162 | + ).flatten |
| 163 | + |
| 164 | + if Debug.enabled then println(s"Running command: ${command.mkString(" ")}") |
| 165 | + os.proc(command).call(stdout = os.Inherit, stderr = os.Inherit) |
| 166 | + |
| 167 | +def processProject(project: Project, cmd: Command)(using pwd: Path) = |
| 168 | + val projectRootPath = pwd / os.up / project.project |
| 169 | + cmd match |
| 170 | + case Command.Clean => |
| 171 | + cleanMaven |
| 172 | + cleanupGeneratedFiles(project, projectRootPath) |
| 173 | + installMaven |
| 174 | + runGeneratorJsoniter(project, projectRootPath) |
| 175 | + case Command.Generate => |
| 176 | + runGeneratorJsoniter(project, projectRootPath) |
| 177 | + case Command.GenerateStrict => |
| 178 | + runGeneratorJsoniter(project, projectRootPath, strict = true) |
| 179 | + case Command.Upstream => |
| 180 | + cleanupGeneratedFiles(project, projectRootPath) |
| 181 | + runGeneratorUpstream(project, projectRootPath) |
| 182 | + case Command.Strict => |
| 183 | + cleanMaven |
| 184 | + cleanupGeneratedFiles(project, projectRootPath) |
| 185 | + installMaven |
| 186 | + runGeneratorJsoniter(project, projectRootPath, strict = true) |
| 187 | + case Command.Exit => |
| 188 | + // Do nothing |
| 189 | + |
| 190 | +def main(): Unit = |
| 191 | + given pwd: Path = os.pwd |
| 192 | + |
| 193 | + println("Args: " + args.mkString(" ")) |
| 194 | + |
| 195 | + try |
| 196 | + // Handle debug flag |
| 197 | + val filteredArgs = args.toList.filter { arg => |
| 198 | + if arg == "--debug-script" then |
| 199 | + Debug.enabled = true |
| 200 | + false |
| 201 | + else true |
| 202 | + } |
| 203 | + |
| 204 | + val (cmdStr, target) = filteredArgs match |
| 205 | + case cmd :: "all" :: _ => (cmd, Target.All) |
| 206 | + case cmd :: name :: _ => (cmd, Target.Specific(name)) |
| 207 | + case cmd :: Nil => (cmd, Target.All) |
| 208 | + case _ => ("exit", Target.All) |
| 209 | + |
| 210 | + val cmd = cmdStr match |
| 211 | + case "clean" => Command.Clean |
| 212 | + case "generate" => Command.Generate |
| 213 | + case "generate-strict" => Command.GenerateStrict |
| 214 | + case "upstream" => Command.Upstream |
| 215 | + case "strict" => Command.Strict |
| 216 | + case "exit" => Command.Exit |
| 217 | + case _ => Command.Exit |
| 218 | + |
| 219 | + (target, cmd) match |
| 220 | + case (_, Command.Exit) => |
| 221 | + println("Usage: scala rebuild.sc <command> [project]") |
| 222 | + println("Available commands: clean, generate, generate-strict, upstream, strict") |
| 223 | + case (Target.All, _) => |
| 224 | + projects.values.foreach(project => processProject(project, cmd)) |
| 225 | + say("All done, mortal") |
| 226 | + case (Target.Specific(projectName), _) => |
| 227 | + projects.get(projectName) match |
| 228 | + case Some(project) => |
| 229 | + processProject(project, cmd) |
| 230 | + say("All done, mortal") |
| 231 | + case None => |
| 232 | + System.err.println(s"Project '$projectName' not found. Available projects: ${projects.keys.mkString(", ")}") |
| 233 | + sys.exit(1) |
| 234 | + catch |
| 235 | + case e: Exception => |
| 236 | + say("You dun goofed, Mortal") |
| 237 | + throw e |
| 238 | + |
| 239 | +main() |
0 commit comments