/
InitCommand.swift
392 lines (336 loc) · 15.1 KB
/
InitCommand.swift
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import Basic
import Foundation
import TuistCore
import Utility
enum InitCommandError: FatalError, Equatable {
case ungettableProjectName(AbsolutePath)
case nonEmptyDirectory(AbsolutePath)
var type: ErrorType {
return .abort
}
var description: String {
switch self {
case let .ungettableProjectName(path):
return "Couldn't infer the project name from path \(path.asString)."
case let .nonEmptyDirectory(path):
return "Can't initialize a project in the non-empty directory at path \(path.asString)."
}
}
static func == (lhs: InitCommandError, rhs: InitCommandError) -> Bool {
switch (lhs, rhs) {
case let (.ungettableProjectName(lhsPath), .ungettableProjectName(rhsPath)):
return lhsPath == rhsPath
case let (.nonEmptyDirectory(lhsPath), .nonEmptyDirectory(rhsPath)):
return lhsPath == rhsPath
default:
return false
}
}
}
// swiftlint:disable:next type_body_length
class InitCommand: NSObject, Command {
// MARK: - Attributes
static let command = "init"
static let overview = "Bootstraps a project."
let platformArgument: OptionArgument<String>
let productArgument: OptionArgument<String>
let pathArgument: OptionArgument<String>
let nameArgument: OptionArgument<String>
let fileHandler: FileHandling
let printer: Printing
let infoplistProvisioner: InfoPlistProvisioning
let playgroundGenerator: PlaygroundGenerating
// MARK: - Init
public required convenience init(parser: ArgumentParser) {
self.init(parser: parser,
fileHandler: FileHandler(),
printer: Printer(),
infoplistProvisioner: InfoPlistProvisioner(),
playgroundGenerator: PlaygroundGenerator())
}
init(parser: ArgumentParser,
fileHandler: FileHandling,
printer: Printing,
infoplistProvisioner: InfoPlistProvisioning,
playgroundGenerator: PlaygroundGenerating) {
let subParser = parser.add(subparser: InitCommand.command, overview: InitCommand.overview)
productArgument = subParser.add(option: "--product",
shortName: nil,
kind: String.self,
usage: "The product (application or framework) the generated project will build (Default: application).",
completion: ShellCompletion.values([
(value: "application", description: "Application"),
(value: "framework", description: "Framework"),
]))
platformArgument = subParser.add(option: "--platform",
shortName: nil,
kind: String.self,
usage: "The platform (ios, tvos or macos) the product will be for (Default: ios).",
completion: ShellCompletion.values([
(value: "ios", description: "iOS platform"),
(value: "tvos", description: "tvOS platform"),
(value: "macos", description: "macOS platform"),
]))
pathArgument = subParser.add(option: "--path",
shortName: "-p",
kind: String.self,
usage: "The path to the folder where the project will be generated (Default: Current directory).",
completion: .filename)
nameArgument = subParser.add(option: "--name",
shortName: "-n",
kind: String.self,
usage: "The name of the project. If it's not passed (Default: Name of the directory).",
completion: nil)
self.fileHandler = fileHandler
self.printer = printer
self.infoplistProvisioner = infoplistProvisioner
self.playgroundGenerator = playgroundGenerator
}
func run(with arguments: ArgumentParser.Result) throws {
let product = try self.product(arguments: arguments)
let platform = try self.platform(arguments: arguments)
let path = try self.path(arguments: arguments)
let name = try self.name(arguments: arguments, path: path)
try verifyDirectoryIsEmpty(path: path)
try generateProjectSwift(name: name, platform: platform, product: product, path: path)
try generateSources(name: name, platform: platform, product: product, path: path)
try generateTests(name: name, path: path)
try generatePlists(platform: platform, product: product, path: path)
try generatePlaygrounds(name: name, path: path, platform: platform)
try generateGitIgnore(path: path)
try generateSetup(path: path)
printer.print(success: "Project generated at path \(path.asString).")
}
// MARK: - Fileprivate
/// Checks if the given directory is empty, essentially that it doesn't contain any file or directory.
///
/// - Parameter path: Directory to be checked.
/// - Throws: An InitCommandError.nonEmptyDirectory error when the directory is not empty.
private func verifyDirectoryIsEmpty(path: AbsolutePath) throws {
if !path.glob("*").isEmpty {
throw InitCommandError.nonEmptyDirectory(path)
}
}
private func generateProjectSwift(name: String, platform: Platform, product: Product, path: AbsolutePath) throws {
let content = """
import ProjectDescription
let project = Project(name: "\(name)",
targets: [
Target(name: "\(name)",
platform: .\(platform.caseValue),
product: .\(product.caseValue),
bundleId: "io.tuist.\(name)",
infoPlist: "Info.plist",
sources: ["Sources/**"],
resources: ["Resources/**"],
dependencies: [
/* Target dependencies can be defined here */
/* .framework(path: "framework") */
]),
Target(name: "\(name)Tests",
platform: .\(platform.caseValue),
product: .unitTests,
bundleId: "io.tuist.\(name)Tests",
infoPlist: "Tests.plist",
sources: "Tests/**",
dependencies: [
.target(name: "\(name)")
])
])
"""
try content.write(to: path.appending(component: "Project.swift").url, atomically: true, encoding: .utf8)
}
private func generatePlists(platform: Platform, product: Product, path: AbsolutePath) throws {
try infoplistProvisioner.generate(path: path.appending(component: "Info.plist"),
platform: platform,
product: product)
try infoplistProvisioner.generate(path: path.appending(component: "Tests.plist"),
platform: platform,
product: .unitTests)
}
// swiftlint:disable:next function_body_length
private func generateGitIgnore(path: AbsolutePath) throws {
let path = path.appending(component: ".gitignore")
let content = """
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## User settings
xcuserdata/
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
### Projects ###
*.xcodeproj
*.xcworkspace
"""
try content.write(to: path.url, atomically: true, encoding: .utf8)
}
/// Generates a Setup.swift file in the given directory.
///
/// - Parameter path: Path where the Setup.swift file will be created.
/// - Throws: An error if the file cannot be created.
private func generateSetup(path: AbsolutePath) throws {
let content = """
import ProjectDescription
let setup = Setup([
// .homebrew(packages: ["swiftlint", "carthage"]),
// .carthage()
])
"""
let setupPath = path.appending(component: "Setup.swift")
try content.write(to: setupPath.url, atomically: true, encoding: .utf8)
}
// swiftlint:disable:next function_body_length
private func generateSources(name: String, platform: Platform, product: Product, path: AbsolutePath) throws {
let path = path.appending(component: "Sources")
try fileHandler.createFolder(path)
var content: String!
var filename: String!
if platform == .macOS, product == .app {
filename = "AppDelegate.swift"
content = """
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
"""
} else if [.iOS, .tvOS].contains(platform), product == .app {
filename = "AppDelegate.swift"
content = """
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let viewController = UIViewController()
viewController.view.backgroundColor = .white
window?.rootViewController = viewController
window?.makeKeyAndVisible()
return true
}
}
"""
} else {
filename = "\(name).swift"
content = """
import Foundation
class \(name) {
}
"""
}
try content.write(to: path.appending(component: filename).url, atomically: true, encoding: .utf8)
}
private func generateTests(name: String, path: AbsolutePath) throws {
let path = path.appending(component: "Tests")
try fileHandler.createFolder(path)
let content = """
import Foundation
import XCTest
@testable import \(name)
final class \(name)Tests: XCTestCase {
}
"""
try content.write(to: path.appending(component: "\(name)Tests.swift").url, atomically: true, encoding: .utf8)
}
private func generatePlaygrounds(name: String, path: AbsolutePath, platform: Platform) throws {
let playgroundsPath = path.appending(component: "Playgrounds")
try fileHandler.createFolder(playgroundsPath)
try playgroundGenerator.generate(path: playgroundsPath,
name: name,
platform: platform,
content: PlaygroundGenerator.defaultContent())
}
private func name(arguments: ArgumentParser.Result, path: AbsolutePath) throws -> String {
if let name = arguments.get(nameArgument) {
return name
} else if let name = path.components.last {
return name
} else {
throw InitCommandError.ungettableProjectName(AbsolutePath.current)
}
}
private func path(arguments: ArgumentParser.Result) throws -> AbsolutePath {
if let path = arguments.get(pathArgument) {
return AbsolutePath(path, relativeTo: fileHandler.currentPath)
} else {
return fileHandler.currentPath
}
}
private func product(arguments: ArgumentParser.Result) throws -> Product {
if let productString = arguments.get(self.productArgument) {
let valid = ["application", "framework"]
if valid.contains(productString) {
return (productString == "application") ? .app : .framework
} else {
throw ArgumentParserError.invalidValue(argument: "product", error: .custom("Product should be either app or framework"))
}
} else {
return .app
}
}
private func platform(arguments: ArgumentParser.Result) throws -> Platform {
if let platformString = arguments.get(self.platformArgument) {
if let platform = Platform(rawValue: platformString) {
return platform
} else {
throw ArgumentParserError.invalidValue(argument: "platform", error: .custom("Platform should be either ios, tvos, or macos"))
}
} else {
return .iOS
}
}
}