|
6 | 6 |
|
7 | 7 | See http://swift.org/LICENSE.txt for license information
|
8 | 8 | See http://swift.org/CONTRIBUTORS.txt for Swift project authors
|
9 |
| - */ |
| 9 | +*/ |
| 10 | + |
| 11 | +/// Provides information about the package for which the plugin is invoked, |
| 12 | +/// as well as contextual information based on the plugin's stated intent |
| 13 | +/// and requirements. |
| 14 | +public struct PluginContext { |
| 15 | + /// Information about the package to which the plugin is being applied. |
| 16 | + public let package: Package |
| 17 | + |
| 18 | + /// The path of a writable directory into which the plugin or the build |
| 19 | + /// commands it constructs can write anything it wants. This could include |
| 20 | + /// any generated source files that should be processed further, and it |
| 21 | + /// could include any caches used by the build tool or the plugin itself. |
| 22 | + /// The plugin is in complete control of what is written under this di- |
| 23 | + /// rectory, and the contents are preserved between builds. |
| 24 | + /// |
| 25 | + /// A plugin would usually create a separate subdirectory of this directory |
| 26 | + /// for each command it creates, and the command would be configured to |
| 27 | + /// write its outputs to that directory. The plugin may also create other |
| 28 | + /// directories for cache files and other file system content that either |
| 29 | + /// it or the command will need. |
| 30 | + public let pluginWorkDirectory: Path |
| 31 | + |
| 32 | + /// The path of the directory into which built products associated with |
| 33 | + /// targets in the graph are written. This is a private implementation |
| 34 | + /// detail. |
| 35 | + let builtProductsDirectory: Path |
| 36 | + |
| 37 | + /// Looks up and returns the path of a named command line executable tool. |
| 38 | + /// The executable must be provided by an executable target or a binary |
| 39 | + /// target on which the package plugin target depends. This function throws |
| 40 | + /// an error if the tool cannot be found. The lookup is case sensitive. |
| 41 | + public func tool(named name: String) throws -> Tool { |
| 42 | + if let path = self.toolNamesToPaths[name] { return Tool(name: name, path: path) } |
| 43 | + throw PluginContextError.toolNotFound(name: name) |
| 44 | + } |
| 45 | + |
| 46 | + /// A mapping from tool names to their definitions. Not directly available |
| 47 | + /// to the plugin, but used by the `tool(named:)` API. |
| 48 | + let toolNamesToPaths: [String: Path] |
| 49 | + |
| 50 | + /// Information about a particular tool that is available to a plugin. |
| 51 | + public struct Tool { |
| 52 | + /// Name of the tool (suitable for display purposes). |
| 53 | + public let name: String |
| 54 | + |
| 55 | + /// Full path of the built or provided tool in the file system. |
| 56 | + public let path: Path |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | + |
10 | 61 |
|
11 | 62 | /// Provides information about the target being built, as well as contextual
|
12 | 63 | /// information such as the paths of the directories to which commands should
|
13 | 64 | /// be configured to write their outputs. This information should be used as
|
14 | 65 | /// part of generating the commands to be run during the build.
|
15 |
| -public final class TargetBuildContext: Decodable { |
16 |
| - |
| 66 | +public struct TargetBuildContext { |
17 | 67 | /// The name of the target being built, as specified in the manifest.
|
18 | 68 | public let targetName: String
|
19 | 69 |
|
@@ -83,48 +133,21 @@ public final class TargetBuildContext: Decodable {
|
83 | 133 | /// The executable must be provided by an executable target or a binary
|
84 | 134 | /// target on which the package plugin target depends. This function throws
|
85 | 135 | /// an error if the tool cannot be found. The lookup is case sensitive.
|
86 |
| - public func tool(named name: String, line: UInt = #line) throws -> ToolInfo { |
87 |
| - if let tool = self.tools[name] { return tool } |
88 |
| - throw TargetBuildContextError.toolNotFound(name: name, line: line) |
| 136 | + public func tool(named name: String) throws -> ToolInfo { |
| 137 | + if let path = self.toolNamesToPaths[name] { return ToolInfo(name: name, path: path) } |
| 138 | + throw PluginContextError.toolNotFound(name: name) |
89 | 139 | }
|
90 | 140 |
|
91 | 141 | /// A mapping from tool names to their definitions. Not directly available
|
92 |
| - /// to the plugin but used by the `tool(named:)` API. |
93 |
| - private let tools: [String: ToolInfo] |
| 142 | + /// to the plugin, but used by the `tool(named:)` API. |
| 143 | + let toolNamesToPaths: [String: Path] |
94 | 144 |
|
95 | 145 | /// Information about a particular tool that is available to a plugin.
|
96 |
| - public struct ToolInfo: Codable { |
97 |
| - |
98 |
| - /// Name of the tool, suitable for display purposes. |
| 146 | + public struct ToolInfo { |
| 147 | + /// Name of the tool (suitable for display purposes). |
99 | 148 | public let name: String
|
100 | 149 |
|
101 |
| - /// Path of the built or provided tool in the file system. |
| 150 | + /// Full path of the built or provided tool in the file system. |
102 | 151 | public let path: Path
|
103 | 152 | }
|
104 |
| - |
105 |
| - /// Internal |
106 |
| - internal let pluginAction: PluginAction |
107 |
| - |
108 |
| - internal enum PluginAction: String, Decodable { |
109 |
| - case createBuildToolCommands |
110 |
| - } |
111 |
| -} |
112 |
| - |
113 |
| -public enum TargetBuildContextError: Error { |
114 |
| - |
115 |
| - /// Could not find a tool with the given name. This could be either because |
116 |
| - /// it doesn't exist, or because the plugin doesn't have a dependency on it. |
117 |
| - case toolNotFound(name: String, line: UInt) |
118 |
| -} |
119 |
| - |
120 |
| -extension TargetBuildContextError: CustomStringConvertible { |
121 |
| - |
122 |
| - public var description: String { |
123 |
| - switch self { |
124 |
| - case .toolNotFound(let name, let line): |
125 |
| - // FIXME: How to convey this line number to where it gets shown for errors at top level. |
126 |
| - // Need to customize "Fatal error: Error raised at top level: plugin doesn’t have access to any tool named ‘MySourceGenBuildTools’ [5]: file Swift/ErrorType.swift, line 200" |
127 |
| - return "plugin doesn’t have access to any tool named ‘\(name)’ [line \(line)]" |
128 |
| - } |
129 |
| - } |
130 | 153 | }
|
0 commit comments