Package.swift 4.33 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
final public class Task {
    public var key: String = ""
    public var dependencies: [String] = []
18
    public var tool: String = "atllbuild"
19
    
20 21
    private var kvp: [String:ParseValue]

22 23 24
    init?(value: ParseValue, name: String) {
        guard let kvp = value.map else { return nil }
        
25 26 27
        self.kvp = kvp
        self.key = name
        self.tool = kvp["tool"]?.string ?? self.tool
28 29 30
        
        if let values = kvp["dependencies"]?.vector {
            for value in values {
31
                if let dep = value.string { self.dependencies.append(dep) }
32 33 34
            }
        }
    }
35 36 37 38
    
    public subscript(key: String) -> ParseValue? {
        return kvp[key]
    }
39 40
}

41
final public class Package {
42
    // The required properties.
43
    public var name: String
44 45 46 47
    
    // The optional properties. All optional properties must have a default value.
    public var version: String = ""
    public var tasks: [String:Task] = [:]
48 49 50 51
    
    public init(name: String) {
        self.name = name
    }
52
    
Drew's avatar
Drew committed
53
    public convenience init?(filepath: String, configurations: [String: String] = [:]) {
54 55 56 57
        guard let parser = Parser(filepath: filepath) else { return nil }
        
        do {
            let result = try parser.parse()
Drew's avatar
Drew committed
58
            self.init(type: result, configurations: configurations)
59 60 61 62 63 64 65
        }
        catch {
            print("error: \(error)")
            return nil
        }
    }
    
Drew's avatar
Drew committed
66
    public init?(type: ParseType, configurations: [String: String]) {
67 68
        if type.name != "package" { return nil }
        
69
        if let value = type.properties["name"]?.string { self.name = value }
70 71 72 73
        else {
            print("ERROR: No name specified for the package.")
            return nil
        }
74
        if let value = type.properties["version"]?.string { self.version = value }
75 76 77 78 79 80 81 82

        if let parsedTasks = type.properties["tasks"]?.map {
            for (key, value) in parsedTasks {
                if let task = Task(value: value, name: key) {
                    self.tasks[key] = task
                }
            }
        }
Drew's avatar
Drew committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

        //swap in configurations
        for requestedConfiguration in configurations.keys {
            let requestedConfigurationValue = configurations[requestedConfiguration]!
            //find the overrides specific to this configuration
            guard let parsedConfigurations = type.properties["configurations"]?.map else {
                fatalError("You requested configuration --\(requestedConfiguration) but no configurations were present in the package file.")
            }
            guard let parsedConfiguration = parsedConfigurations[requestedConfiguration]?.map else {
                fatalError("You requested configuration --\(requestedConfiguration) but we only have \(Array(parsedConfigurations.keys))")
            }
            guard let overrideSpecifications = parsedConfiguration[requestedConfigurationValue]?.map else {
                fatalError("You requested configuration --\(requestedConfiguration) \(requestedConfigurationValue) but we only have \(Array(parsedConfiguration.keys))")
            }
            for taskSpec in overrideSpecifications.keys {
                guard let overrideSpecification = overrideSpecifications[taskSpec]?.map else {
                    fatalError("Cannot get override specification for --\(requestedConfiguration) \(requestedConfigurationValue)")
                }
                if let task = tasks[taskSpec] {
                    for(k,v) in overrideSpecification {
                        task.kvp[k] = v
                    }
                }
                else {
                    fatalError("Global configurations not implemented; can't configure option \(requestedConfigurationValue) for non-task spec \(taskSpec)")
                }
            }
        }
111
    }
112
}