Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Experimenting with Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kdawgwilk committed Sep 11, 2017
1 parent 45d804f commit a849efb
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 37 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ matrix:
- export PATH=${PWD}/swift-4.0-DEVELOPMENT-SNAPSHOT-2017-09-08-a-ubuntu14.04/usr/bin:"${PATH}"
script:
- swift build
- .build/debug/autobahn drive buildRelease
- .build/debug/autobahn drive test
- .build/debug/autobahn drive ci
- os: osx
osx_image: xcode9
sudo: required
script:
- swift build
- .build/debug/autobahn drive buildRelease
- .build/debug/autobahn drive test
- .build/debug/autobahn drive ci
10 changes: 7 additions & 3 deletions Autobahn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import Foundation
import AutobahnDescription

enum Highway: String, AutobahnDescription.Highway {
case build, buildRelease, test, deploy, release
case build, buildRelease
case test
case ci
case deploy, release
}

Autobahn(Highway.self)

.beforeAll { highway in
print("Driving highway: \(highway)")

}

.highway(.build) {
Expand All @@ -17,14 +20,15 @@ Autobahn(Highway.self)
}

.highway(.buildRelease) {
print("Building...")
try sh("swift", "build", "-c", "release")
}

.highway(.test) {
try sh("swift", "test")
}

.highway(.ci, dependsOn: [.buildRelease, .test])

.highway(.deploy) {
print("Deploying...")
}
Expand Down
50 changes: 50 additions & 0 deletions Sources/Actions/Action.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Foundation

public enum ActionCategory {
case testing
case building
case screenshots
case project
case codeSigning
case documentation
case beta
case push
case production
case sourceControl
case notifications
case misc
case deprecated
}

public struct Option {
let key: String
let envName: String?
let description: String?

init(key: String, envName: String? = nil, description: String? = nil) {
self.key = key
self.envName = envName
self.description = description
}
}

public protocol ActionError: Error {

}

public protocol Action {
var id: String { get }
var description: String { get }
var codeExamples: [String] { get }
var authors: [String] { get }
var options: [Option] { get }
}

public extension Action {
var id: String {
return String(describing: Self.self)
}
var description: String { return "" }
var codeExamples: [String] { return [] }
var authors: [String] { return [] }
}
8 changes: 6 additions & 2 deletions Sources/Actions/Fastlane.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Foundation

public enum FastlaneError: ActionError {

}

enum Fastlane {
static func lane(_ name: String) throws {
try Shell.run("fastlane", args: [name])
try ShellCommand.run("fastlane", args: [name])
}

static func lane(_ name: String, platform: String) throws {
try Shell.run("fastlane", args: [platform, name])
try ShellCommand.run("fastlane", args: [platform, name])
}
}
11 changes: 9 additions & 2 deletions Sources/Actions/Shell.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import Foundation

public enum ShellError: Error {
public enum ShellError: ActionError {
case nonZeroExitCode(String)
}

public enum Shell {
public struct ShellCommand: Action {
public let description = ""
public var authors = ["Kaden Wilkinson"]

public var options: [Option] = [
Option(key: "cmd")
]

public static func run(_ cmd: String, args: [String]) throws {
let proc = Process()
proc.launchPath = "/usr/bin/env"
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Xcode.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum XcodeError {
public enum XcodeError: ActionError {

}

Expand Down
63 changes: 39 additions & 24 deletions Sources/AutobahnDescription/AutobahnDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public extension Highway {
}

public final class Autobahn<H: Highway> {
let version = "0.1.0"
public let version = "0.1.0"

private var highwayStart: Date
private var highwayEnd: Date?
private var beforeAllHandler: ((H) throws -> Void)?
private var highways = [H: () throws -> Void]()
private var dependings = [H: [H]]()
Expand All @@ -60,37 +62,41 @@ public final class Autobahn<H: Highway> {

// MARK: - Public Functions

public init(_ highwayType: H.Type) {}
public init(_ highwayType: H.Type) {
self.highwayStart = Date()
}

public func beforeAll(handler: @escaping (H) throws -> Void) -> Autobahn<H> {
guard self.beforeAllHandler == nil else {
fatalError("beforeAll declared more than once")
}
self.beforeAllHandler = handler
precondition(beforeAllHandler == nil, "beforeAll declared more than once")
beforeAllHandler = handler
return self
}

public func highway(_ highway: H, dependsOn: [H]? = nil, taskHandler: @escaping () throws -> Void) -> Autobahn<H> {
self.highways[highway] = taskHandler
if let d = dependsOn {
self.dependings[highway] = d
}
public func highway(_ highway: H, handler: @escaping () throws -> Void) -> Autobahn<H> {
self.highways[highway] = handler
return self
}

public func highway(_ highway: H, dependsOn highways: [H]) -> Autobahn<H> {
dependings[highway] = highways
return self
}

public func highway(_ highway: H, dependsOn highways: [H], handler: @escaping () throws -> Void) -> Autobahn<H> {
self.highways[highway] = handler
dependings[highway] = highways
return self
}

public func afterAll(handler: @escaping (H) throws -> Void) -> Autobahn<H> {
guard self.afterAllHandler == nil else {
fatalError("afterAll declared more than once")
}
self.afterAllHandler = handler
precondition(afterAllHandler == nil, "afterAll declared more than once")
afterAllHandler = handler
return self
}

public func onError(handler: @escaping (String, Error) -> Void) -> Autobahn<H> {
guard self.onErrorHandler == nil else {
fatalError("onError declared more than once")
}
self.onErrorHandler = handler
precondition(onErrorHandler == nil, "onError declared more than once")
onErrorHandler = handler
return self
}

Expand All @@ -111,26 +117,35 @@ public final class Autobahn<H: Highway> {
} catch {
self.onErrorHandler?(secondArg ?? "", error)
}
highwayEnd = Date()
}

// MARK: - private functions

private func driveHighway(_ highway: H) throws {
guard let highwayHandler = self.highways[highway] else {
throw HighwayError.highwayNotDefined(highway.rawValue)
}
if let dependings = self.dependings[highway] {
for dependingHighway in dependings {
guard let d = self.highways[dependingHighway] else {
throw HighwayError.highwayNotDefined(dependingHighway.rawValue)
}
printHeader(for: dependingHighway)
try d()
}
}
try highwayHandler()
if let handler = highways[highway] {
printHeader(for: highway)
try handler()
}
}

private func printHeader(for highway: H) {
let count = highway.rawValue.count
print("-------------\(String(repeating: "-", count: count))----")
print("--- Highway: \(highway.rawValue) ---")
print("-------------\(String(repeating: "-", count: count))----")
}
}

public func sh(_ cmd: String, _ args: String...) throws {
try Shell.run(cmd, args: args)
try ShellCommand.run(cmd, args: args)
}
2 changes: 1 addition & 1 deletion Tests/AutobahnFrameworkTests/TestConsole.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class TestConsole: ConsoleProtocol {
exec(program, args: arguments)
let command = program + " " + arguments.joined(separator: " ")
guard let val = backgroundExecuteOutputBuffer[command] else {
throw AutobahnError.general("No command set for '\(command)'")
throw AutobahnError.general("No command set for '\(command)'", help: "")
}
return val
}
Expand Down

0 comments on commit a849efb

Please sign in to comment.