-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathConfig.swift
More file actions
175 lines (165 loc) · 6.05 KB
/
Copy pathConfig.swift
File metadata and controls
175 lines (165 loc) · 6.05 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
81
82
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Foundation
/// The configuration data for plugin.
///
/// Depending on the configuration data, source file check and
/// syntax generation is performed.
struct Config {
/// The source file scan mode.
///
/// Specifies which source files need to be parsed for syntax generation.
let scan: ScanMode
/// The source file scan mode.
///
/// Specifies which source files need to be parsed for syntax generation.
enum ScanMode: String, Codable {
/// Represents to check current target.
///
/// Files only from the target which includes plugin are checked.
case target
/// Represents to check current target and target dependencies.
///
/// Files from the target which includes plugin and target dependencies
/// present in current package manifest are checked.
case direct
/// Represents to check all local target dependencies.
///
/// Files from the target which includes plugin and all targets
/// that are in the same project/package that are dependencies
/// of this target.
case local
/// Represents to check current target and all dependencies.
///
/// Files from the target which includes plugin and all its
/// dependencies are checked.
case recursive
/// Creates a new instance by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails,
/// or if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
init(from decoder: Decoder) throws {
let rawValue = try String(from: decoder).lowercased()
guard let value = Self(rawValue: rawValue) else {
throw DecodingError.typeMismatch(
Self.self,
.init(
codingPath: decoder.codingPath,
debugDescription: "Data doesn't match any case"
)
)
}
self = value
}
}
}
extension Config: Codable {
/// Creates a new instance by decoding from the given decoder.
///
/// The scanning mode is set to only scan target unless specified
/// explicitly.
///
/// - Parameter decoder: The decoder to read data from.
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.scan =
try container.decodeIfPresent(
ScanMode.self, forCaseInsensitiveKey: .scan
) ?? .target
}
/// Returns file path as URL converting provided string.
///
/// Uses platform and version specific API to create URL file path.
///
/// - Parameter filePath: The path to file as string.
/// - Returns: The file path URL.
static func url(forFilePath filePath: String) -> URL {
#if canImport(Darwin)
guard
#available(macOS 13, iOS 16, macCatalyst 16, tvOS 16, watchOS 9, *)
else {
return URL(fileURLWithPath: filePath)
}
return URL(filePath: filePath)
#else
return URL(fileURLWithPath: filePath)
#endif
}
/// Returns file path as string converting provided URL.
///
/// Uses platform and version specific API to create string file path.
///
/// - Parameter url: The path to file as URL.
/// - Returns: The file path string.
static func filePath(forURL url: URL) -> String {
#if canImport(Darwin)
guard
#available(macOS 13, iOS 16, macCatalyst 16, tvOS 16, watchOS 9, *)
else {
return url.path
}
return url.path(percentEncoded: false)
#else
return url.path
#endif
}
/// Returns a URL by appending the specified path to the URL.
///
/// This method doesn’t percent-encode any path separators.
///
/// - Parameters:
/// - path: The path component to append to the URL.
/// - url: The base URL to which the path will be appended.
/// - Returns: A new URL with the path component appended.
static func appending(path: String, to url: URL) -> URL {
#if canImport(Darwin)
guard
#available(macOS 13, iOS 16, macCatalyst 16, tvOS 16, watchOS 9, *)
else {
return url.appendingPathComponent(path)
}
return url.appending(path: path)
#else
return url.appendingPathComponent(path)
#endif
}
}
extension KeyedDecodingContainerProtocol {
/// Decodes a value of the given type for the given key case-insensitively,
/// if present.
///
/// This method returns `nil` if the container does not have a value
/// associated with key case-insensitively, or if the value is null.
///
/// - Parameters:
/// - type: The type of value to decode.
/// - key: The key that the decoded value is associated with.
///
/// - Returns: A decoded value of the requested type, or `nil`
/// if the `Decoder` does not have an entry associated with the given
/// key, or if the value is a null value.
///
/// - Throws: `DecodingError.typeMismatch` if the encountered
/// encoded value is not convertible to the requested type or the key
/// value matches multiple value case-insensitively.
func decodeIfPresent<T: Decodable>(
_ type: T.Type,
forCaseInsensitiveKey key: Key
) throws -> T? {
let keys = self.allKeys.filter { eachKey in
eachKey.stringValue.lowercased() == key.stringValue.lowercased()
}
guard keys.count <= 1 else {
throw DecodingError.typeMismatch(
type,
.init(
codingPath: codingPath,
debugDescription: """
Duplicate keys found, keys are case-insensitive.
"""
)
)
}
return try decodeIfPresent(type, forKey: keys.first ?? key)
}
}