CustomTool.swift 1.1 KB
Newer Older
Drew's avatar
Drew committed
1 2 3 4 5 6 7 8
#if os(Linux)
import Glibc
#else
import Darwin
#endif

import atpkg
///Create a tool out of another program someone has lying around on their system
Drew's avatar
Drew committed
9 10 11 12
///Important: this is for programs that are written for AT.  For other programs, use shell instead.
final class CustomTool: Tool {
    static func isCustomTool(name: String) -> Bool {
        return name.hasSuffix(".attool")
Drew's avatar
Drew committed
13
    }
Drew's avatar
Drew committed
14 15
    let name: String
    init(name: String) {
16
        self.name = String(name.characters[name.characters.startIndex..<name.characters.index(name.characters.startIndex, offsetBy: name.characters.count - 7)])
Drew's avatar
Drew committed
17
    }
Drew's avatar
Drew committed
18
    func run(task: Task) {
Drew's avatar
Drew committed
19
        var cmd = "\(self.name) "
Drew's avatar
Drew committed
20 21 22 23 24 25 26
        for key in task.allKeys.sorted() {
            if Task.Option.allOptions.map({$0.rawValue}).contains(key) { continue }
            guard let value = task[key]?.string else {
                fatalError("\(task.qualifiedName).\(key) is not string")
            }
            cmd += "--\(key) \"\(evaluateSubstitutions(input: value, package: task.package))\" "
        }
Drew's avatar
Drew committed
27 28
        let env = Shell.environment(task: task)
        anarchySystem(cmd,environment: env)
Drew's avatar
Drew committed
29 30
    }
}