Files
swift-hpa/Sources/hpa/BuildCommand.swift

67 lines
1.6 KiB
Swift

import ArgumentParser
import CliClient
import Dependencies
import Foundation
struct BuildCommand: AsyncParsableCommand {
static let commandName = "build"
static let configuration = CommandConfiguration.playbook(
commandName: commandName,
abstract: "Build a home performance assesment project.",
examples: (
label: "Build project when in the project directory.",
example: "\(commandName)"
),
(
label: "Build project from outside the project directory.",
example: "\(commandName) --project-directory /path/to/project"
)
)
@OptionGroup var globals: GlobalOptions
@Option(
help: "Path to the project directory.",
completion: .directory
)
var projectDirectory: String?
@Argument(
help: "Extra arguments / options passed to the playbook."
)
var extraOptions: [String] = []
mutating func run() async throws {
try await _run()
}
private func _run() async throws {
@Dependency(\.cliClient) var cliClient
let projectDir: String
if projectDirectory == nil {
guard let pwd = ProcessInfo.processInfo.environment["PWD"] else {
throw ProjectDirectoryNotSupplied()
}
projectDir = pwd
} else {
projectDir = projectDirectory!
}
try await cliClient.runPlaybookCommand(
globals.playbookOptions(
arguments: [
"--tags", "build-project",
"--extra-vars", "project_dir=\(projectDir)"
],
configuration: nil
),
logging: globals.loggingOptions(commandName: Self.commandName)
)
}
}
struct ProjectDirectoryNotSupplied: Error {}