Implemented the Clean subcommand #8
@ -9,6 +9,7 @@ struct Colibri: AsyncParsableCommand {
|
||||
abstract: "The utility to manage your Hummingbird apps",
|
||||
subcommands: [
|
||||
Build.self,
|
||||
Clean.self,
|
||||
Create.self,
|
||||
Outdated.self,
|
||||
Update.self
|
||||
|
31
Executable/Sources/Commands/CleanCommand.swift
Normal file
31
Executable/Sources/Commands/CleanCommand.swift
Normal file
@ -0,0 +1,31 @@
|
||||
import ArgumentParser
|
||||
import ColibriLibrary
|
||||
|
||||
extension Colibri {
|
||||
struct Clean: AsyncParsableCommand {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let configuration = CommandConfiguration(
|
||||
commandName: "clean-project",
|
||||
abstract: "Clean a Hummingbird app",
|
||||
helpNames: .shortAndLong,
|
||||
aliases: ["clean"]
|
||||
)
|
||||
|
||||
@OptionGroup var options: Options
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
mutating func run() async throws {
|
||||
let terminalService = TerminalService()
|
||||
|
||||
let cleanProject = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
try await cleanProject(at: options.locationURL,
|
||||
shouldReset: options.reset,
|
||||
purgeCache: options.purgeCache)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
19
Executable/Sources/Options/CleanOptions.swift
Normal file
19
Executable/Sources/Options/CleanOptions.swift
Normal file
@ -0,0 +1,19 @@
|
||||
import ArgumentParser
|
||||
import ColibriLibrary
|
||||
|
||||
extension Colibri.Clean {
|
||||
struct Options: ParsableArguments, Locationable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@Flag(name: .shortAndLong)
|
||||
var reset: Bool = false
|
||||
|
||||
@Flag(name: .shortAndLong)
|
||||
var purgeCache: Bool = false
|
||||
|
||||
@Option(name: .shortAndLong)
|
||||
var location: String?
|
||||
|
||||
}
|
||||
}
|
49
Library/Sources/Public/Tasks/CleanProjectTask.swift
Normal file
49
Library/Sources/Public/Tasks/CleanProjectTask.swift
Normal file
@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
|
||||
public struct CleanProjectTask {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let terminalService: TerminalServicing
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
public init(terminalService: TerminalServicing) {
|
||||
self.terminalService = terminalService
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func callAsFunction(
|
||||
at location: URL? = nil,
|
||||
shouldReset: Bool = false,
|
||||
purgeCache: Bool = false
|
||||
) async throws (TerminalServiceError) {
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments: [String] = ["package"]
|
||||
|
||||
if let location {
|
||||
arguments.append(contentsOf: ["--package-path", location.pathString])
|
||||
}
|
||||
|
||||
arguments.insert("clean", at: 1)
|
||||
|
||||
try await terminalService.run(executableURL, arguments: arguments)
|
||||
|
||||
if shouldReset {
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("reset", at: 1)
|
||||
|
||||
try await terminalService.run(executableURL, arguments: arguments)
|
||||
}
|
||||
|
||||
if purgeCache {
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("purge-cache", at: 1)
|
||||
|
||||
try await terminalService.run(executableURL, arguments: arguments)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
128
Test/Sources/Cases/Public/Tasks/CleanProjectTaskTests.swift
Normal file
128
Test/Sources/Cases/Public/Tasks/CleanProjectTaskTests.swift
Normal file
@ -0,0 +1,128 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct CleanProjectTaskTests {
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func task(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
let arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 1)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func taskWithReset(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location, shouldReset: true)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 2)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("reset", at: 1)
|
||||
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func taskWithPurgeCache(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location, purgeCache: true)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 2)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("purge-cache", at: 1)
|
||||
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder])
|
||||
func taskWithResetAndPurgeCache(at location: URL?) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await task(at: location, shouldReset: true, purgeCache: true)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/swift")
|
||||
|
||||
var arguments = if let location {
|
||||
["package", "clean", "--package-path", location.pathString]
|
||||
} else {
|
||||
["package", "clean"]
|
||||
}
|
||||
|
||||
#expect(terminalService.actions.count == 3)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("reset", at: 1)
|
||||
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, arguments))
|
||||
|
||||
arguments.remove(at: 1)
|
||||
arguments.insert("purge-cache", at: 1)
|
||||
|
||||
#expect(terminalService.actions[2] == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [nil, URL.someCurrentFolder], [TerminalServiceError.unexpected, .output(""), .captured("")])
|
||||
func task(at location: URL?, throws error: TerminalServiceError) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceMock(action: .error(error))
|
||||
let task = CleanProjectTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await task(at: location, shouldReset: .random(), purgeCache: .random())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user