-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMetaProtocolCodableSourceTarget.swift
More file actions
90 lines (86 loc) · 3.14 KB
/
Copy pathMetaProtocolCodableSourceTarget.swift
File metadata and controls
90 lines (86 loc) · 3.14 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
import Foundation
import PackagePlugin
/// Represents a target consisting of a source code module,
/// containing `Swift` source files.
///
/// Targets from multiple build system can support this plugin
/// by providing conformance.
protocol MetaProtocolCodableSourceTarget {
/// Type representing sequence of files.
associatedtype FileSequence: Sequence
where FileSequence.Element == FileList.Element
/// The name of the module produced
/// by the target.
///
/// This is used as additional imports in
/// plugin generated code.
var moduleName: String { get }
/// The targets on which the current target depends on.
///
/// These targets are scanned if `direct` scan mode
/// provided in config.
var dependencyTargets: [Self] { get }
/// All the targets on which current target depends on.
///
/// These targets are scanned if `recursive` scan mode
/// provided in config.
var recursiveTargets: [Self] { get }
/// A list of source files in the target that have the given
/// filename suffix.
///
/// The list can possibly be empty if no file matched.
///
/// - Parameter suffix: The name suffix.
/// - Returns: The matching files.
func sourceFiles(withSuffix suffix: String) -> FileSequence
/// The absolute path to config file if provided.
///
/// The file name comparison is case-insensitive
/// and if no match found `nil` is returned.
///
/// - Parameter name: The config file name.
/// - Returns: The config file path.
func configPath(named name: String) throws -> URL?
}
extension Config {
/// Returns targets to scan and import modules based on current
/// configuration.
///
/// Based on configuration, the targets for which source files need
/// to be checked and the modules that will be imported in final syntax
/// generated is returned.
///
/// - Parameter target: The target including plugin.
/// - Returns: The targets to scan and modules to import.
func scanInput<Context: MetaProtocolCodablePluginContext>(
for target: Context.Target, in context: Context
) -> (targets: [Context.Target], modules: [String]) {
let allTargets: [Context.Target]
let modules: [String]
switch scan {
case .target:
allTargets = [target]
modules = []
case .direct:
var targets = target.dependencyTargets
modules = targets.map(\.moduleName)
targets.append(target)
allTargets = targets
case .local:
allTargets = context.localTargets.filter { localTarget in
target.recursiveTargets.contains { target in
target.moduleName == localTarget.moduleName
}
}
modules = allTargets.lazy.map(\.moduleName).filter { module in
module != target.moduleName
}
case .recursive:
var targets = target.recursiveTargets
modules = targets.map(\.moduleName)
targets.append(target)
allTargets = targets
}
return (allTargets, modules)
}
}