xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XToolSupport / NewCommand.swift
NewCommand.swift179 lines · 5.5 KiBRaw
| 1 | import ArgumentParser |
| 2 | import Foundation |
| 3 | import PackLib |
| 4 | |
| 5 | struct NewCommand: AsyncParsableCommand { |
| 6 | static let configuration = CommandConfiguration( |
| 7 | commandName: "new", |
| 8 | abstract: "Create a new xtool SwiftPM project" |
| 9 | ) |
| 10 | |
| 11 | @Argument var name: String? |
| 12 | |
| 13 | @Flag( |
| 14 | help: ArgumentHelp( |
| 15 | "Skip setup steps. (default: false)", |
| 16 | discussion: """ |
| 17 | By default, this command first invokes `xtool setup` to complete \ |
| 18 | any missing setup steps, like authenticating and installing the SDK. Use \ |
| 19 | this flag to always skip setup. |
| 20 | """ |
| 21 | ) |
| 22 | ) var skipSetup = false |
| 23 | |
| 24 | // swiftlint:disable:next function_body_length |
| 25 | func run() async throws { |
| 26 | if !skipSetup { |
| 27 | // perform any remaining setup steps first |
| 28 | try await SetupOperation(quiet: true).run() |
| 29 | } |
| 30 | |
| 31 | let name = try await Console.promptRequired("Package name: ", existing: self.name) |
| 32 | |
| 33 | var allowedFirstCharacters: CharacterSet = ["_"] |
| 34 | allowedFirstCharacters.insert(charactersIn: "a"..."z") |
| 35 | allowedFirstCharacters.insert(charactersIn: "A"..."Z") |
| 36 | |
| 37 | var allowedOtherCharacters = allowedFirstCharacters |
| 38 | allowedOtherCharacters.insert(charactersIn: "0"..."9") |
| 39 | allowedOtherCharacters.insert("-") |
| 40 | |
| 41 | // promptRequired validates !isEmpty |
| 42 | let firstScalar = name.unicodeScalars.first! |
| 43 | guard allowedFirstCharacters.contains(firstScalar) else { |
| 44 | throw Console.Error(""" |
| 45 | Package name '\(name)' is invalid. \ |
| 46 | The package name must start with one of [a-z, A-Z, _]. Found '\(firstScalar)'. |
| 47 | """) |
| 48 | } |
| 49 | |
| 50 | if let firstInvalid = name.rangeOfCharacter(from: allowedOtherCharacters.inverted) { |
| 51 | let invalidValue = name[firstInvalid] |
| 52 | throw Console.Error(""" |
| 53 | Package name '\(name)' is invalid. \ |
| 54 | The package name may only contain [a-z, A-Z, 0-9, _, -]. Found '\(invalidValue)'. |
| 55 | """) |
| 56 | } |
| 57 | |
| 58 | let baseURL = URL(fileURLWithPath: name) |
| 59 | |
| 60 | guard !baseURL.exists else { |
| 61 | throw Console.Error("Cannot create \(name): a file already exists at that path.") |
| 62 | } |
| 63 | |
| 64 | print("Creating package: \(name)") |
| 65 | |
| 66 | let moduleName = name.replacingOccurrences(of: "-", with: "_") |
| 67 | |
| 68 | let files: [(String, String)] = [ |
| 69 | ( |
| 70 | "Package.swift", |
| 71 | """ |
| 72 | // swift-tools-version: 6.0 |
| 73 | |
| 74 | import PackageDescription |
| 75 | |
| 76 | let package = Package( |
| 77 | name: "\(name)", |
| 78 | platforms: [ |
| 79 | .iOS(.v17), |
| 80 | .macOS(.v14), |
| 81 | ], |
| 82 | products: [ |
| 83 | // An xtool project should contain exactly one library product, |
| 84 | // representing the main app. |
| 85 | .library( |
| 86 | name: "\(moduleName)", |
| 87 | targets: ["\(moduleName)"] |
| 88 | ), |
| 89 | ], |
| 90 | targets: [ |
| 91 | .target( |
| 92 | name: "\(moduleName)" |
| 93 | ), |
| 94 | ] |
| 95 | ) |
| 96 | """ |
| 97 | ), |
| 98 | |
| 99 | ( |
| 100 | "xtool.yml", |
| 101 | """ |
| 102 | version: 1 |
| 103 | bundleID: com.example.\(name) |
| 104 | """ |
| 105 | ), |
| 106 | |
| 107 | ( |
| 108 | ".gitignore", |
| 109 | """ |
| 110 | .DS_Store |
| 111 | /.build |
| 112 | /Packages |
| 113 | xcuserdata/ |
| 114 | DerivedData/ |
| 115 | .swiftpm/configuration/registries.json |
| 116 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata |
| 117 | .netrc |
| 118 | |
| 119 | /xtool |
| 120 | """ |
| 121 | ), |
| 122 | |
| 123 | ( |
| 124 | "Sources/\(moduleName)/\(moduleName)App.swift", |
| 125 | """ |
| 126 | import SwiftUI |
| 127 | |
| 128 | @main |
| 129 | struct \(moduleName)App: App { |
| 130 | var body: some Scene { |
| 131 | WindowGroup { |
| 132 | ContentView() |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | """ |
| 137 | ), |
| 138 | |
| 139 | ( |
| 140 | "Sources/\(moduleName)/ContentView.swift", |
| 141 | """ |
| 142 | import SwiftUI |
| 143 | |
| 144 | struct ContentView: View { |
| 145 | var body: some View { |
| 146 | VStack { |
| 147 | Image(systemName: "globe") |
| 148 | .imageScale(.large) |
| 149 | .foregroundStyle(.tint) |
| 150 | Text("Hello, world!") |
| 151 | } |
| 152 | .padding() |
| 153 | } |
| 154 | } |
| 155 | """ |
| 156 | ), |
| 157 | ] |
| 158 | |
| 159 | for (path, contents) in files { |
| 160 | let url = baseURL.appendingPathComponent(path) |
| 161 | try? FileManager.default.createDirectory( |
| 162 | at: url.deletingLastPathComponent(), |
| 163 | withIntermediateDirectories: true |
| 164 | ) |
| 165 | print("Creating \(path)") |
| 166 | try "\(contents)\n".write(to: url, atomically: true, encoding: .utf8) |
| 167 | } |
| 168 | |
| 169 | await LSPConfig.tryEnsure( |
| 170 | using: .default, |
| 171 | quiet: true, |
| 172 | currentDirectory: baseURL, |
| 173 | ) |
| 174 | |
| 175 | print("\nFinished generating project \(name). Next steps:") |
| 176 | print("- Enter the directory with `cd \(name)`") |
| 177 | print("- Build and run with `xtool dev`") |
| 178 | } |
| 179 | } |