|
| 1 | +import Foundation |
| 2 | +import PackagePlugin |
| 3 | + |
| 4 | +#if swift(<6) |
| 5 | +extension PluginContext.Tool { |
| 6 | + /// Full path of the built or provided tool in the file system. |
| 7 | + var url: URL { |
| 8 | + Config.url(forFilePath: path.string) |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +extension PackagePlugin.File { |
| 13 | + /// The path of the file. |
| 14 | + var url: URL { |
| 15 | + Config.url(forFilePath: path.string) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +extension PluginContext { |
| 20 | + /// The path of a writable directory into which the plugin or the build |
| 21 | + /// commands it constructs can write anything it wants. This could include |
| 22 | + /// any generated source files that should be processed further, and it |
| 23 | + /// could include any caches used by the build tool or the plugin itself. |
| 24 | + /// The plugin is in complete control of what is written under this di- |
| 25 | + /// rectory, and the contents are preserved between builds. |
| 26 | + /// |
| 27 | + /// A plugin would usually create a separate subdirectory of this directory |
| 28 | + /// for each command it creates, and the command would be configured to |
| 29 | + /// write its outputs to that directory. The plugin may also create other |
| 30 | + /// directories for cache files and other file system content that either |
| 31 | + /// it or the command will need. |
| 32 | + var pluginWorkDirectoryURL: URL { |
| 33 | + Config.url(forFilePath: pluginWorkDirectory.string) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +extension Command { |
| 38 | + /// Returns a command that runs when any of its output files are needed by |
| 39 | + /// the build, but out-of-date. |
| 40 | + /// |
| 41 | + /// An output file is out-of-date if it doesn't exist, or if any input files |
| 42 | + /// have changed since the command was last run. |
| 43 | + /// |
| 44 | + /// - Note: the paths in the list of output files may depend on the list of |
| 45 | + /// input file paths, but **must not** depend on reading the contents of |
| 46 | + /// any input files. Such cases must be handled using a `prebuildCommand`. |
| 47 | + /// |
| 48 | + /// - parameters: |
| 49 | + /// - displayName: An optional string to show in build logs and other |
| 50 | + /// status areas. |
| 51 | + /// - executable: The absolute path to the executable to be invoked. |
| 52 | + /// - arguments: Command-line arguments to be passed to the executable. |
| 53 | + /// - environment: Environment variable assignments visible to the |
| 54 | + /// executable. |
| 55 | + /// - inputFiles: Files on which the contents of output files may depend. |
| 56 | + /// Any paths passed as `arguments` should typically be passed here as |
| 57 | + /// well. |
| 58 | + /// - outputFiles: Files to be generated or updated by the executable. |
| 59 | + /// Any files recognizable by their extension as source files |
| 60 | + /// (e.g. `.swift`) are compiled into the target for which this command |
| 61 | + /// was generated as if in its source directory; other files are treated |
| 62 | + /// as resources as if explicitly listed in `Package.swift` using |
| 63 | + /// `.process(...)`. |
| 64 | + static func buildCommand( |
| 65 | + displayName: String?, executable: URL, arguments: [String], |
| 66 | + environment: [String: String] = [:], inputFiles: [URL] = [], |
| 67 | + outputFiles: [URL] = [] |
| 68 | + ) -> Self { |
| 69 | + .buildCommand( |
| 70 | + displayName: displayName, |
| 71 | + executable: .init(Config.filePath(forURL: executable)), |
| 72 | + arguments: arguments, |
| 73 | + environment: environment, |
| 74 | + inputFiles: inputFiles.map { .init(Config.filePath(forURL: $0)) }, |
| 75 | + outputFiles: outputFiles.map { .init(Config.filePath(forURL: $0)) } |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#if canImport(XcodeProjectPlugin) |
| 81 | +extension XcodePluginContext { |
| 82 | + /// The path of a writable directory into which the plugin or the build |
| 83 | + /// commands it constructs can write anything it wants. This could include |
| 84 | + /// any generated source files that should be processed further, and it |
| 85 | + /// could include any caches used by the build tool or the plugin itself. |
| 86 | + /// The plugin is in complete control of what is written under this di- |
| 87 | + /// rectory, and the contents are preserved between builds. |
| 88 | + /// |
| 89 | + /// A plugin would usually create a separate subdirectory of this directory |
| 90 | + /// for each command it creates, and the command would be configured to |
| 91 | + /// write its outputs to that directory. The plugin may also create other |
| 92 | + /// directories for cache files and other file system content that either |
| 93 | + /// it or the command will need. |
| 94 | + var pluginWorkDirectoryURL: URL { |
| 95 | + Config.url(forFilePath: pluginWorkDirectory.string) |
| 96 | + } |
| 97 | +} |
| 98 | +#endif |
| 99 | +#endif |
| 100 | + |
| 101 | +extension SourceModuleTarget { |
| 102 | + /// The absolute path of the target directory in the local file system. |
| 103 | + var directoryURL: URL { |
| 104 | + #if swift(<6) |
| 105 | + return Config.url(forFilePath: directory.string) |
| 106 | + #else |
| 107 | + switch self { |
| 108 | + case let target as ClangSourceModuleTarget: |
| 109 | + return target.directoryURL |
| 110 | + case let target as SwiftSourceModuleTarget: |
| 111 | + return target.directoryURL |
| 112 | + default: |
| 113 | + fatalError("Unsupported target type") |
| 114 | + } |
| 115 | + #endif |
| 116 | + } |
| 117 | +} |
0 commit comments