-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathTemplate.swift
More file actions
80 lines (70 loc) · 3.44 KB
/
Template.swift
File metadata and controls
80 lines (70 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Foundation
import JSONUtilities
struct TemplateStructure {
let baseKey: String
let templatesKey: String
let nameToReplace: String
}
extension Target {
static func resolveTargetTemplates(jsonDictionary: JSONDictionary) -> JSONDictionary {
resolveTemplates(jsonDictionary: jsonDictionary,
templateStructure: TemplateStructure(baseKey: "targets",
templatesKey: "targetTemplates",
nameToReplace: "target_name"))
}
}
extension Scheme {
static func resolveSchemeTemplates(jsonDictionary: JSONDictionary) -> JSONDictionary {
resolveTemplates(jsonDictionary: jsonDictionary,
templateStructure: TemplateStructure(baseKey: "schemes",
templatesKey: "schemeTemplates",
nameToReplace: "scheme_name"))
}
}
private func resolveTemplates(jsonDictionary: JSONDictionary, templateStructure: TemplateStructure) -> JSONDictionary {
guard var baseDictionary: [String: JSONDictionary] = jsonDictionary[templateStructure.baseKey] as? [String: JSONDictionary] else {
return jsonDictionary
}
let templatesDictionary: [String: JSONDictionary] = jsonDictionary[templateStructure.templatesKey] as? [String: JSONDictionary] ?? [:]
// Recursively collects all nested template names of a given dictionary.
func collectTemplates(of jsonDictionary: JSONDictionary,
into allTemplates: inout [String],
insertAt insertionIndex: inout Int) {
guard let templates = jsonDictionary["templates"] as? [String] else {
return
}
for template in templates where !allTemplates.contains(template) {
guard let templateDictionary = templatesDictionary[template] else {
continue
}
allTemplates.insert(template, at: insertionIndex)
collectTemplates(of: templateDictionary, into: &allTemplates, insertAt: &insertionIndex)
insertionIndex += 1
}
}
for (referenceName, var reference) in baseDictionary {
var templates: [String] = []
var index: Int = 0
collectTemplates(of: reference, into: &templates, insertAt: &index)
if !templates.isEmpty {
var mergedDictionary: JSONDictionary = [:]
for template in templates {
if let templateDictionary = templatesDictionary[template] {
mergedDictionary = templateDictionary.merged(onto: mergedDictionary)
}
}
reference = reference.merged(onto: mergedDictionary)
reference = reference.expand(variables: [templateStructure.nameToReplace: referenceName])
if let templateAttributes = reference["templateAttributes"] as? [String: String] {
reference = reference.expand(variables: templateAttributes)
}
if let globalVariables = jsonDictionary["globalTemplateAttributes"] as? [String: String] {
reference = reference.expand(variables: globalVariables)
}
}
baseDictionary[referenceName] = reference
}
var jsonDictionary = jsonDictionary
jsonDictionary[templateStructure.baseKey] = baseDictionary
return jsonDictionary
}