Skip to content

Commit

Permalink
Add environment validations
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Sep 27, 2020
1 parent a39468c commit 9e9097d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions Sources/lynx/Template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ protocol Template {
var productName: String { get }
var bundleId: String { get }

func validate() throws
func vivify() throws
func cleanup() throws
func printInstructions()
Expand Down
5 changes: 4 additions & 1 deletion Sources/lynx/Templates/KaMPKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ struct KaMPKit: Template {
let productName: String
let bundleId: String

func validate() throws {
guard Pod.canInstall() else { throw PodError.cocoapodsNotFound }
}

func vivify() throws {
// 1. download KMP template project from DS.
print("Fetching template")
Expand Down Expand Up @@ -90,7 +94,6 @@ struct KaMPKit: Template {
print("Installing iOS dependencies".cyan().bold())
let iosRoot = "\(productName)/ios"

guard Pod.canInstall() else { throw PodError.cocoapodsNotFound }
let podInstall = [FileUtils.cd(directory: iosRoot), Pod.install()]

// We will pod install twice because of a bug in how the shared cocoapod is setup.
Expand Down
32 changes: 23 additions & 9 deletions Sources/lynx/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import Darwin
import FileUtils
import SwiftShell

let version = "0.0.1"
let version = "0.0.2"

enum ExecutionError: Error, CustomStringConvertible {
case invalidIdentifier
case xcodeMissing
case androidDeveloperToolsMissing

var description: String {
switch self {
case .invalidIdentifier:
return "The bundle identifier prefix is invalid. Example: com.doublesymmetry"
case .xcodeMissing:
return "Xcode nor Xcode Command Line Tools is installed on this machine."
case .androidDeveloperToolsMissing:
return "Android developer environment not setup correctly. Are things added to $PATH?"
}
}
}
Expand All @@ -31,16 +37,26 @@ Group {
Argument<String>("name", description: "The name to give the project"),
Argument<String>("bundleId", description: "The company bundle prefix to use (i.e. com.doublesymmetry)"),
Option("template", default: "kampkit")) { productName, bundleId, templateName in
// 0. get chosen template
let template = KaMPKit(productName: productName, bundleId: bundleId)
defer { try? template.cleanup() }

do {
// 1. do validations
guard bundleId.split(separator: ".").count == 2 else { throw ExecutionError.invalidIdentifier }

print("Creating project \(productName)".cyan().bold())
print("Setting up".cyan().bold())
let template = KaMPKit(productName: productName, bundleId: bundleId)
defer { try? template.cleanup() }

print("Checking environment")
let xcodeCheckCommand = run("which", "xcodebuild")
if !xcodeCheckCommand.succeeded { throw ExecutionError.xcodeMissing }

let androidStudioCheck = run("which", "adb")
if !androidStudioCheck.succeeded { throw ExecutionError.androidDeveloperToolsMissing }

print("Running template validations")
try template.validate()

// 2. make project directory
print("Creating project \(productName)".cyan().bold())
print("Creating folder \(productName)")
try FileUtils.mkdir(name: productName).execute()

Expand All @@ -59,7 +75,5 @@ Group {
}
}

$0.command("version") {
print(version)
}
$0.command("version") { print(version) }
}.run(version)

0 comments on commit 9e9097d

Please sign in to comment.