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