-
-
Notifications
You must be signed in to change notification settings - Fork 518
/
SynthesizedResourceInterfacesGenerator.swift
83 lines (76 loc) · 2.6 KB
/
SynthesizedResourceInterfacesGenerator.swift
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
81
82
83
import Path
import PathKit
import Stencil
import StencilSwiftKit
import SwiftGenKit
import TuistSupport
import XcodeGraph
protocol SynthesizedResourceInterfacesGenerating {
func render(
parser: ResourceSynthesizer.Parser,
parserOptions: [String: ResourceSynthesizer.Parser.Option],
templateString: String,
name: String,
bundleName: String?,
paths: [AbsolutePath]
) throws -> String
}
final class SynthesizedResourceInterfacesGenerator: SynthesizedResourceInterfacesGenerating {
func render(
parser: ResourceSynthesizer.Parser,
parserOptions: [String: ResourceSynthesizer.Parser.Option],
templateString: String,
name: String,
bundleName: String?,
paths: [AbsolutePath]
) throws -> String {
let template = Template(
templateString: templateString,
environment: stencilSwiftEnvironment()
)
let parser = try self.parser(for: parser, with: parserOptions)
try paths.forEach { try parser.parse(path: Path($0.pathString), relativeTo: Path("")) }
var context = parser.stencilContext()
context = try StencilContext.enrich(
context: context,
parameters: makeParams(name: name, bundleName: bundleName)
)
return try template.render(context)
}
// MARK: - Helpers
private func parser(
for parser: ResourceSynthesizer.Parser,
with parserOptions: [String: ResourceSynthesizer.Parser.Option]
) throws -> Parser {
let options = parserOptions.mapValues(\.value)
switch parser {
case .assets:
return try AssetsCatalog.Parser(options: options)
case .strings:
return try Strings.Parser(options: options)
case .plists:
return try Plist.Parser(options: options)
case .fonts:
return try Fonts.Parser(options: options)
case .coreData:
return try CoreData.Parser(options: options)
case .interfaceBuilder:
return try InterfaceBuilder.Parser(options: options)
case .json:
return try JSON.Parser(options: options)
case .yaml:
return try Yaml.Parser(options: options)
case .files:
return try Files.Parser(options: options)
}
}
private func makeParams(name: String, bundleName: String?) -> [String: Any] {
var params: [String: Any] = [:]
params["publicAccess"] = true
params["name"] = name
if let bundleName {
params["bundle"] = bundleName
}
return params
}
}