Shell.swift 3.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2016 Anarchy Tools Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16 17 18 19
#if os(Linux)
import Glibc
#else
import Darwin
#endif
20

21
import atfoundation
22 23 24 25 26 27 28 29
import atpkg

/**
 * The shell tool forks a new process with `/bin/sh -c`. Any arguments specified
 * within the task will also be sent across.
 * If the tool returns with an error code of non-zero, the tool will fail.
 */
final class Shell : Tool {
Drew's avatar
Drew committed
30

Drew's avatar
Drew committed
31 32 33 34 35
    ///Builds the environment for the specified task.
    static func environment(task: Task) -> [String:String] {
        var environment: [String:String] = [:]
        environment["ATBUILD_PLATFORM"] = Platform.targetPlatform.description
        environment["ATBUILD_USER_PATH"] = userPath().description
36
        if let version = task.package.version {
Drew's avatar
Drew committed
37
            environment["ATBUILD_PACKAGE_VERSION"] = version
38
        }
39

Drew's avatar
Drew committed
40
        environment["ATBUILD_CONFIGURATION"] = "\(currentConfiguration)"
Drew's avatar
Drew committed
41
        if let o = currentConfiguration.optimize {
Drew's avatar
Drew committed
42
            environment["ATBUILD_CONFIGURATION_OPTIMIZE"] = o ? "1":"0"
Drew's avatar
Drew committed
43 44
        }
        if let o = currentConfiguration.fastCompile {
Drew's avatar
Drew committed
45
            environment["ATBUILD_CONFIGURATION_FAST_COMPILE"] = o ? "1":"0"
Drew's avatar
Drew committed
46 47
        }
        if let o = currentConfiguration.testingEnabled {
Drew's avatar
Drew committed
48
            environment["ATBUILD_CONFIGURATION_TESTING_ENABLED"] = o ? "1":"0"
Drew's avatar
Drew committed
49 50
        }
        if let o = currentConfiguration.noMagic {
Drew's avatar
Drew committed
51
            environment["ATBUILD_CONFIGURATION_NO_MAGIC"] = o ? "1":"0"
Drew's avatar
Drew committed
52 53
        }

54 55 56 57 58 59 60 61 62 63
        //expose debug configuration info
        let conf: String
        switch (currentConfiguration.debugInstrumentation) {
            case .Included:
            conf = "included"
            case .Omitted:
            conf = "omitted"
            case .Stripped:
            conf = "stripped"
        }
Drew's avatar
Drew committed
64
        environment["ATBUILD_CONFIGURATION_DEBUG_INSTRUMENTATION"] = conf
65

Drew's avatar
Drew committed
66 67 68 69 70 71
        //does bin path not exist?
        //let's create it!
        let binPath = try! FS.getWorkingDirectory().appending("bin")
        if !FS.fileExists(path: binPath) {
            try! FS.createDirectory(path: binPath)
        }
Drew's avatar
Drew committed
72
        environment["ATBUILD_BIN_PATH"] = binPath.description
73

Drew's avatar
Drew committed
74
        environment["PWD"]=String(validatingUTF8: realpath(task.importedPath.description,nil))!
Drew's avatar
Drew committed
75
        print("pwd set to",environment["PWD"]!)
Drew's avatar
Drew committed
76
        return environment
Drew's avatar
Drew committed
77
    }
Drew's avatar
Drew committed
78
    func run(task: Task) {
Drew's avatar
Drew committed
79 80
        guard var script = task["script"]?.string else { fatalError("Invalid 'script' argument to shell tool.") }
        script = evaluateSubstitutions(input: script, package: task.package)
Drew's avatar
Drew committed
81 82
        let env = Shell.environment(task: task)
        anarchySystem(script, environment: env)
83 84
    }
}