Skip to content

Commit 461a562

Browse files
author
Eric Miller
committed
Add generate and refresh arguments
1 parent 1fa6186 commit 461a562

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

Sources/UBKit/Models/UBKitError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import Foundation
2525

2626
enum UBKitError: Error, CustomStringConvertible {
27+
case invalidNumberOfArguments
28+
case invalidArguments(String)
2729
case error(Error)
2830
case unableToCreateFile(String)
2931
case invalidFolder(String)
@@ -38,6 +40,10 @@ enum UBKitError: Error, CustomStringConvertible {
3840

3941
var description: String {
4042
switch self {
43+
case .invalidNumberOfArguments:
44+
return "Invalid number of command line arguments"
45+
case .invalidArguments(let str):
46+
return "Invalid argument: \(str)"
4147
case .error(let err):
4248
return err.localizedDescription
4349
case .unableToCreateFile(let str):

Sources/UBKit/UBKit.swift

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,46 @@ public final class UBKit {
3030

3131
private let fileManager = FileManager.default
3232

33-
public init(arguments: [String] = CommandLine.arguments) {
33+
public init(arguments: [String] = CommandLine.arguments) throws {
34+
guard arguments.count == 2 else {
35+
throw UBKitError.invalidNumberOfArguments
36+
}
37+
3438
self.arguments = arguments
3539
}
3640

3741
public func run(_ completion: @escaping ((Error?) -> Void)) {
42+
guard let arg = getArgument(for: arguments[1]) else {
43+
completion(UBKitError.invalidArguments(arguments[1]))
44+
return
45+
}
46+
47+
switch arg {
48+
case .generate:
49+
generate(completion)
50+
case .refresh:
51+
refresh(completion)
52+
}
53+
}
54+
55+
class func validatePath(_ path: String, isDirectory: Bool) -> Bool {
56+
if isDirectory {
57+
var directory = ObjCBool(false)
58+
let folderExists = FileManager.default.fileExists(atPath: path, isDirectory: &directory)
59+
return folderExists && directory.boolValue
60+
} else {
61+
return FileManager.default.fileExists(atPath: path)
62+
}
63+
}
64+
}
65+
66+
private extension UBKit {
67+
68+
enum Argument: String {
69+
case generate, refresh
70+
}
71+
72+
func generate(_ completion: @escaping ((Error?) -> Void)) {
3873
let workingPath = fileManager.currentDirectoryPath
3974
do {
4075
guard let fileData = fileManager.contents(atPath: workingPath.appending("/ubconfig.json")) else {
@@ -60,16 +95,20 @@ public final class UBKit {
6095
return
6196
}
6297

98+
print("\n----------")
99+
print("👍 Successfully created your iOS and Unity projects")
100+
print("The iOS project is located under iOS/")
101+
print("The Unity project is located under Unity/")
63102
completion(nil)
64103
}
65104

66-
class func validatePath(_ path: String, isDirectory: Bool) -> Bool {
67-
if isDirectory {
68-
var directory = ObjCBool(false)
69-
let folderExists = FileManager.default.fileExists(atPath: path, isDirectory: &directory)
70-
return folderExists && directory.boolValue
71-
} else {
72-
return FileManager.default.fileExists(atPath: path)
73-
}
105+
func refresh(_ completion: @escaping ((Error?) -> Void)) {
106+
print("\n----------")
107+
print("👍 Successfully refreshed your iOS project")
108+
completion(nil)
109+
}
110+
111+
func getArgument(for arg: String) -> Argument? {
112+
return Argument(rawValue: arg)
74113
}
75114
}

Sources/UnityBuildKit/main.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@
2424
import Foundation
2525
import UBKit
2626

27-
let kit = UBKit()
27+
var kit: UBKit
28+
do {
29+
kit = try UBKit()
30+
} catch {
31+
print("\n----------")
32+
print("💥 An error was encountered while creating your projects")
33+
print(error.localizedDescription)
34+
exit(1)
35+
}
36+
37+
2838
kit.run { (error) in
2939
guard error == nil else {
3040
print("\n----------")
@@ -33,10 +43,6 @@ kit.run { (error) in
3343
exit(1)
3444
}
3545

36-
print("\n----------")
37-
print("👍 Successfully created your iOS and Unity projects!")
38-
print("The iOS project is located under iOS/")
39-
print("The Unity project is located under Unity/")
4046
print("For more information, visit <https://github.com/handsomecode/UnityBuildKit>\n\n")
4147
exit(0)
4248
}

0 commit comments

Comments
 (0)