|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 12/7/21. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import SwiftCLI |
| 10 | +import mustache |
| 11 | + |
| 12 | +class ScaffoldCommand: Command { |
| 13 | + let name = "scaffold" |
| 14 | + let shortDescription = "Create baseline implementation of a given code artifact" |
| 15 | + |
| 16 | + @Flag("-p", "--plugin", description: "Generate an analytics plugin (objc/swift/java/kotlin/ts)") |
| 17 | + var plugin: Bool |
| 18 | + |
| 19 | + @Flag("-e", "--edgefn", description: "Generate an edge function (js)") |
| 20 | + var edgeFn: Bool |
| 21 | + |
| 22 | + @Flag("-i", "--importer", description: "Generate a CSV importer script (js)") |
| 23 | + var importer: Bool |
| 24 | + |
| 25 | + @Flag("--objc", description: "Use Objective-C for generated plugin") |
| 26 | + var useObjc: Bool |
| 27 | + @Flag("--swift", description: "Use Swift for generated plugin") |
| 28 | + var useSwift: Bool |
| 29 | + @Flag("--java", description: "Use Java for generated plugin") |
| 30 | + var useJava: Bool |
| 31 | + @Flag("--kotlin", description: "Use Kotlin for generated plugin") |
| 32 | + var useKotlin: Bool |
| 33 | + @Flag("--ts", description: "Use Typescript for generated plugin") |
| 34 | + var useTypescript: Bool |
| 35 | + @Flag("--js", description: "Use Javascript for generated edgefn/importer") |
| 36 | + var useJavascript: Bool |
| 37 | + |
| 38 | + @Key("-n", "--name", description: "Optionally specify a name for the generated scaffold") |
| 39 | + var nameParam: String? |
| 40 | + |
| 41 | + var scaffoldName: String? |
| 42 | + let fileManager = FileManager.default |
| 43 | + |
| 44 | + var optionGroups: [OptionGroup] { |
| 45 | + return [ |
| 46 | + .atLeastOne($plugin, $edgeFn, $importer), |
| 47 | + .atMostOne($plugin, $edgeFn, $importer), |
| 48 | + .atLeastOne($useObjc, $useSwift, $useJava, $useKotlin, $useTypescript, $useJavascript)] |
| 49 | + } |
| 50 | + |
| 51 | + func execute() throws { |
| 52 | + if nameParam == nil { |
| 53 | + if plugin { |
| 54 | + scaffoldName = "MyPlugin" |
| 55 | + } else if edgeFn { |
| 56 | + scaffoldName = "MyEdgeFunction" |
| 57 | + } else if importer { |
| 58 | + scaffoldName = "MyCSVImporter" |
| 59 | + } |
| 60 | + } else { |
| 61 | + scaffoldName = nameParam |
| 62 | + } |
| 63 | + |
| 64 | + if plugin && useSwift { |
| 65 | + generateSwiftPlugin() |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func generateSwiftPlugin() { |
| 70 | + guard let scaffoldName = scaffoldName else { |
| 71 | + exitWithError("Could not determine a plugin name to use.") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + let filename = scaffoldName + ".swift" |
| 76 | + |
| 77 | + print("Generating a Swift Plugin from template...") |
| 78 | + |
| 79 | + for file in plugin_templates_swift { |
| 80 | + if fileManager.fileExists(atPath: filename) { |
| 81 | + let overwrite = Input.readBool(prompt: "\(filename) exists. Overwrite? [y/N]: ", defaultValue: false) |
| 82 | + if overwrite == false { |
| 83 | + exitWithError(code: .commandFailed) |
| 84 | + } |
| 85 | + } |
| 86 | + let generate = Mustache(file) |
| 87 | + let result = generate(name: scaffoldName, filename: filename) |
| 88 | + do { |
| 89 | + try result.write(toFile: filename, atomically: true, encoding: .utf8) |
| 90 | + print("Created \(filename).") |
| 91 | + } catch { |
| 92 | + exitWithError("Unable to write \(filename)") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + print("\n") |
| 97 | + } |
| 98 | +} |
0 commit comments