From 7ccb82ac45d30b7d77045146fece481551f65a59 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 11:07:45 +0100 Subject: [PATCH 01/18] Adds `plugin` target to `Package.swift` --- Package.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 3e5eac380..d83322743 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.5 +// swift-tools-version:5.6 import PackageDescription import Foundation @@ -231,6 +231,12 @@ let package = Package( name: "lib_InternalSwiftSyntaxParser", url: "https://github.com/keith/StaticInternalSwiftSyntaxParser/releases/download/5.6/lib_InternalSwiftSyntaxParser.xcframework.zip", checksum: "88d748f76ec45880a8250438bd68e5d6ba716c8042f520998a438db87083ae9d" + ), + .plugin( + name: "SourceryCommandPlugin", + capability: .command( + intent: .custom(verb: "sourcery-command", description: "Sourcery command plugin for code generation"), + permissions: [.writeToPackageDirectory(reason: "Need permission to write generated files to package directory")] ) ] ) From a95e43ca06ccbb249f456c0954d7dac096f2b2e5 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 11:10:53 +0100 Subject: [PATCH 02/18] Adds empty implementation of `CommandPlugin` --- Package.swift | 10 ++++++++-- .../SourceryCommandPlugin/SourceryCommandPlugin.swift | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift diff --git a/Package.swift b/Package.swift index d83322743..21d6a8fd7 100644 --- a/Package.swift +++ b/Package.swift @@ -235,8 +235,14 @@ let package = Package( .plugin( name: "SourceryCommandPlugin", capability: .command( - intent: .custom(verb: "sourcery-command", description: "Sourcery command plugin for code generation"), - permissions: [.writeToPackageDirectory(reason: "Need permission to write generated files to package directory")] + intent: .custom( + verb: "sourcery-command", + description: "Sourcery command plugin for code generation" + ), + permissions: [ + .writeToPackageDirectory(reason: "Need permission to write generated files to package directory") + ] + ) ) ] ) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift new file mode 100644 index 000000000..d6b05e226 --- /dev/null +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -0,0 +1,8 @@ +import PackagePlugin + +@main +struct SourceryCommandPlugin: CommandPlugin { + func performCommand(context: PluginContext, arguments: [String]) async throws { + + } +} From 9529244965affeca679a18b1578ff154e3566591 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 11:14:38 +0100 Subject: [PATCH 03/18] Updates dependencies formats --- Package.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index 21d6a8fd7..f8ea05862 100644 --- a/Package.swift +++ b/Package.swift @@ -20,13 +20,13 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/jpsim/Yams.git", from: "4.0.6"), - .package(url: "https://github.com/kylef/Commander.git", .exact("0.9.1")), + .package(url: "https://github.com/kylef/Commander.git", exact: "0.9.1"), // PathKit needs to be exact to avoid a SwiftPM bug where dependency resolution takes a very long time. - .package(url: "https://github.com/kylef/PathKit.git", .exact("1.0.1")), + .package(url: "https://github.com/kylef/PathKit.git", exact: "1.0.1"), .package(url: "https://github.com/StencilProject/Stencil.git", .upToNextMajor(from: "0.14.0")), - .package(url: "https://github.com/SwiftGen/StencilSwiftKit.git", .exact("2.10.1")), - .package(url: "https://github.com/tuist/XcodeProj.git", .exact("8.3.1")), - .package(url: "https://github.com/apple/swift-syntax.git", .exact("0.50600.1")), + .package(url: "https://github.com/SwiftGen/StencilSwiftKit.git", exact: "2.10.1"), + .package(url: "https://github.com/tuist/XcodeProj.git", exact: "8.3.1"), + .package(url: "https://github.com/apple/swift-syntax.git", exact: "0.50600.1"), .package(url: "https://github.com/Quick/Quick.git", from: "3.0.0"), .package(url: "https://github.com/Quick/Nimble.git", from: "9.0.0") ], @@ -242,7 +242,8 @@ let package = Package( permissions: [ .writeToPackageDirectory(reason: "Need permission to write generated files to package directory") ] - ) + ), + dependencies: ["SourceryExecutable"] ) ] ) From 69da60deacf324ec53a562e23bfdf9453cbbca8f Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 11:18:39 +0100 Subject: [PATCH 04/18] Removes potentially unused dependency to remove warning --- Package.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Package.swift b/Package.swift index f8ea05862..816996899 100644 --- a/Package.swift +++ b/Package.swift @@ -23,7 +23,6 @@ let package = Package( .package(url: "https://github.com/kylef/Commander.git", exact: "0.9.1"), // PathKit needs to be exact to avoid a SwiftPM bug where dependency resolution takes a very long time. .package(url: "https://github.com/kylef/PathKit.git", exact: "1.0.1"), - .package(url: "https://github.com/StencilProject/Stencil.git", .upToNextMajor(from: "0.14.0")), .package(url: "https://github.com/SwiftGen/StencilSwiftKit.git", exact: "2.10.1"), .package(url: "https://github.com/tuist/XcodeProj.git", exact: "8.3.1"), .package(url: "https://github.com/apple/swift-syntax.git", exact: "0.50600.1"), From e1767de2ccbd33170b20410f82b6eba14bd318f7 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 11:25:04 +0100 Subject: [PATCH 05/18] Adds code for `SourceryCommandPlugin` --- .../SourceryCommandPlugin.swift | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index d6b05e226..8863a4b3d 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -1,8 +1,30 @@ import PackagePlugin +import Foundation @main struct SourceryCommandPlugin: CommandPlugin { func performCommand(context: PluginContext, arguments: [String]) async throws { + let configFilePath = context.package.directory.appending(subpath: ".sourcery.yml").string + guard FileManager.default.fileExists(atPath: configFilePath) else { + Diagnostics.error("🤷‍♂️ Could not find config at: \(configFilePath)") + return + } + let sourceryExecutable = try context.tool(named: "SourceryExecutable") + let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) + + let process = Process() + process.executableURL = sourceryURL + process.arguments = [ + "--disableCache" + ] + + try process.run() + process.waitUntilExit() + + let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 + if !gracefulExit { + Diagnostics.error("🛑 The plugin execution failed") + } } } From 23194badf8a9fe9aa5aa040611202589fdfb1aae Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 11:29:16 +0100 Subject: [PATCH 06/18] Makes `SourceryCommandPlugin` available as a product --- Package.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Package.swift b/Package.swift index 816996899..2be39b103 100644 --- a/Package.swift +++ b/Package.swift @@ -17,6 +17,7 @@ let package = Package( .library(name: "SourceryJS", targets: ["SourceryJS"]), .library(name: "SourcerySwift", targets: ["SourcerySwift"]), .library(name: "SourceryFramework", targets: ["SourceryFramework"]), + .plugin(name: "SourceryCommandPlugin", targets: ["SourceryCommandPlugin"]) ], dependencies: [ .package(url: "https://github.com/jpsim/Yams.git", from: "4.0.6"), From d328372cc4da84a69ab138034489a36d09ad6206 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 12:16:52 +0100 Subject: [PATCH 07/18] Makes command plugin more `focused` to the target itself and not the package --- .../SourceryCommandPlugin.swift | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 8863a4b3d..8a5e58228 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -4,27 +4,35 @@ import Foundation @main struct SourceryCommandPlugin: CommandPlugin { func performCommand(context: PluginContext, arguments: [String]) async throws { - let configFilePath = context.package.directory.appending(subpath: ".sourcery.yml").string - guard FileManager.default.fileExists(atPath: configFilePath) else { - Diagnostics.error("🤷‍♂️ Could not find config at: \(configFilePath)") - return - } - - let sourceryExecutable = try context.tool(named: "SourceryExecutable") - let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) - - let process = Process() - process.executableURL = sourceryURL - process.arguments = [ - "--disableCache" - ] - - try process.run() - process.waitUntilExit() - - let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 - if !gracefulExit { - Diagnostics.error("🛑 The plugin execution failed") + // Run one per target + for target in context.package.targets { + let configFilePath = target.directory.appending(subpath: ".sourcery.yml").string + + guard FileManager.default.fileExists(atPath: configFilePath) else { + Diagnostics.warning("🤷‍♂️ Could not find config at: \(configFilePath), skipping...") + return + } + + print(configFilePath) + + let sourceryExecutable = try context.tool(named: "SourceryExecutable") + let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) + + let process = Process() + process.executableURL = sourceryURL + process.arguments = [ + "--config", + configFilePath, + "--disableCache" + ] + + try process.run() + process.waitUntilExit() + + let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 + if !gracefulExit { + Diagnostics.error("🛑 The plugin execution failed") + } } } } From 22f9d02d0c8d9861c262bd69ec11940e873c5142 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 14:46:30 +0100 Subject: [PATCH 08/18] Updates command plugin with `Xcode` project support --- .../SourceryCommandPlugin.swift | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 8a5e58228..e427d0f9c 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -9,11 +9,10 @@ struct SourceryCommandPlugin: CommandPlugin { let configFilePath = target.directory.appending(subpath: ".sourcery.yml").string guard FileManager.default.fileExists(atPath: configFilePath) else { - Diagnostics.warning("🤷‍♂️ Could not find config at: \(configFilePath), skipping...") + Diagnostics.warning("⚠️ Could not find `.sourcery.yml` for the given target") return } - print(configFilePath) let sourceryExecutable = try context.tool(named: "SourceryExecutable") let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) @@ -36,3 +35,43 @@ struct SourceryCommandPlugin: CommandPlugin { } } } + +#if canImport(XcodeProjectPlugin) +import XcodeProjectPlugin + +extension SourceryCommandPlugin: XcodeCommandPlugin { + func performCommand(context: XcodePluginContext, arguments: [String]) throws { + for target in context.xcodeProject.targets { + guard let configFilePath = target + .inputFiles + .filter({ $0.path.lastComponent == ".sourcery.yml" }) + .first? + .path + .string else { + Diagnostics.warning("⚠️ Could not find `.sourcery.yml` in Xcode's input file list") + return + } + + let sourceryExecutable = try context.tool(named: "SourceryExecutable") + let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) + + let process = Process() + process.executableURL = sourceryURL + process.arguments = [ + "--config", + configFilePath, + "--disableCache" + ] + + try process.run() + process.waitUntilExit() + + let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 + if !gracefulExit { + Diagnostics.error("🛑 The plugin execution failed") + } + } + debugPrint(context) + } +} +#endif From 804fc4d963a62e18770465e3394e4aba97591dc8 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 15:01:56 +0100 Subject: [PATCH 09/18] Makes reusable method called `run` --- .../SourceryCommandPlugin.swift | 73 +++++++++---------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index e427d0f9c..446ecaa7e 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -2,40 +2,49 @@ import PackagePlugin import Foundation @main -struct SourceryCommandPlugin: CommandPlugin { +struct SourceryCommandPlugin { + private func run(_ sourcery: String, withConfig configFilePath: String) throws { + let sourceryURL = URL(fileURLWithPath: sourcery) + + let process = Process() + process.executableURL = sourceryURL + process.arguments = [ + "--config", + configFilePath, + "--disableCache" + ] + + try process.run() + process.waitUntilExit() + + let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 + if !gracefulExit { + throw "🛑 The plugin execution failed with reason: \(process.terminationReason.rawValue) and status: \(process.terminationStatus)" + } + } +} + +// MARK: - CommandPlugin + +extension SourceryCommandPlugin: CommandPlugin { func performCommand(context: PluginContext, arguments: [String]) async throws { // Run one per target for target in context.package.targets { let configFilePath = target.directory.appending(subpath: ".sourcery.yml").string + let sourcery = try context.tool(named: "SourceryExecutable").path.string guard FileManager.default.fileExists(atPath: configFilePath) else { Diagnostics.warning("⚠️ Could not find `.sourcery.yml` for the given target") return } - - let sourceryExecutable = try context.tool(named: "SourceryExecutable") - let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) - - let process = Process() - process.executableURL = sourceryURL - process.arguments = [ - "--config", - configFilePath, - "--disableCache" - ] - - try process.run() - process.waitUntilExit() - - let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 - if !gracefulExit { - Diagnostics.error("🛑 The plugin execution failed") - } + try run(sourcery, withConfig: configFilePath) } } } +// MARK: - XcodeProjectPlugin + #if canImport(XcodeProjectPlugin) import XcodeProjectPlugin @@ -51,27 +60,15 @@ extension SourceryCommandPlugin: XcodeCommandPlugin { Diagnostics.warning("⚠️ Could not find `.sourcery.yml` in Xcode's input file list") return } - - let sourceryExecutable = try context.tool(named: "SourceryExecutable") - let sourceryURL = URL(fileURLWithPath: sourceryExecutable.path.string) - let process = Process() - process.executableURL = sourceryURL - process.arguments = [ - "--config", - configFilePath, - "--disableCache" - ] + let sourcery = try context.tool(named: "SourceryExecutable").path.string - try process.run() - process.waitUntilExit() - - let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 - if !gracefulExit { - Diagnostics.error("🛑 The plugin execution failed") - } + try run(sourcery, withConfig: configFilePath) } - debugPrint(context) } } #endif + +extension String: LocalizedError { + public var errorDescription: String? { return self } +} From 7c521159159fdc130d206fcc12e76c9c1cf0044f Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 15:12:54 +0100 Subject: [PATCH 10/18] Handles throwing errors from `run` function --- Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 446ecaa7e..0ac74cd39 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -19,7 +19,7 @@ struct SourceryCommandPlugin { let gracefulExit = process.terminationReason == .exit && process.terminationStatus == 0 if !gracefulExit { - throw "🛑 The plugin execution failed with reason: \(process.terminationReason.rawValue) and status: \(process.terminationStatus)" + throw "🛑 The plugin execution failed with reason: \(process.terminationReason.rawValue) and status: \(process.terminationStatus) " } } } @@ -60,7 +60,6 @@ extension SourceryCommandPlugin: XcodeCommandPlugin { Diagnostics.warning("⚠️ Could not find `.sourcery.yml` in Xcode's input file list") return } - let sourcery = try context.tool(named: "SourceryExecutable").path.string try run(sourcery, withConfig: configFilePath) From 150b077afb69794c1996ebe86c151d773b35d36f Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 15:26:40 +0100 Subject: [PATCH 11/18] Updates cli parameter to be `cacheBasePath` --- .../SourceryCommandPlugin/SourceryCommandPlugin.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 0ac74cd39..7db8671e2 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -3,7 +3,7 @@ import Foundation @main struct SourceryCommandPlugin { - private func run(_ sourcery: String, withConfig configFilePath: String) throws { + private func run(_ sourcery: String, withConfig configFilePath: String, cachePath: String) throws { let sourceryURL = URL(fileURLWithPath: sourcery) let process = Process() @@ -11,7 +11,8 @@ struct SourceryCommandPlugin { process.arguments = [ "--config", configFilePath, - "--disableCache" + "--cachePath", + cachePath ] try process.run() @@ -38,7 +39,7 @@ extension SourceryCommandPlugin: CommandPlugin { return } - try run(sourcery, withConfig: configFilePath) + try run(sourcery, withConfig: configFilePath, cachePath: context.pluginWorkDirectory.string) } } } @@ -62,7 +63,7 @@ extension SourceryCommandPlugin: XcodeCommandPlugin { } let sourcery = try context.tool(named: "SourceryExecutable").path.string - try run(sourcery, withConfig: configFilePath) + try run(sourcery, withConfig: configFilePath, cachePath: context.pluginWorkDirectory.string) } } } From 38d47efcbceb477081c372da60c495b6394032df Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 15:26:48 +0100 Subject: [PATCH 12/18] Updates cli parameter to be `cacheBasePath` --- Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 7db8671e2..18260843f 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -11,7 +11,7 @@ struct SourceryCommandPlugin { process.arguments = [ "--config", configFilePath, - "--cachePath", + "--cacheBasePath", cachePath ] From 7bc089e1cbf98563ffb8e124f4229820e93d5959 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 15:39:20 +0100 Subject: [PATCH 13/18] Updates `README.md` --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 5c876ffba..23133b3a4 100644 --- a/README.md +++ b/README.md @@ -120,12 +120,21 @@ You can also watch this quick getting started and intro to mocking video by Insi Run `xcodebuild -scheme sourcery -destination generic/platform=macOS -archivePath sourcery.xcarchive archive` and export the binary from the archive. +- _SPM (for plugin use only)_ +Add the package dependency to your `Package.swift` manifest from version `1.8.3`. + +``` +.package(url: "https://github.com/krzysztofzablocki/Sourcery.git", from: "1.8.3") +``` + ## Documentation Full documentation for the latest release is available [here](http://merowing.info/Sourcery/). ## Usage +### Running the executable + Sourcery is a command line tool; you can either run it manually or in a custom build phase using the following command: ``` @@ -134,6 +143,32 @@ $ ./bin/sourcery --sources --templates --output > Note: this command differs depending on how you installed Sourcery (see [Installing](#installing)) +### Swift Package command + +Sourcery can now be used as a Swift package command plugin. In order to do this, the package must be added as a dependency to your Swift package or Xcode project (see [Installing](#installing) above). + +To provide a configuration for the plugin to use, place a `.sourcery.yml` file at the root of the target's directory (in the sources folder rather than the root of the package). + +#### Running from the command line + +To verify the plugin can be found by SwiftPM, use: + +``` +$ swift package --list +``` + +To run the code generator, you need to allow changes to the project with the `--allow-writing-to-package-directory` flag: + +``` +$ swift package --allow-writing-to-package-directory sourcery-code-generation +``` + +#### Running in Xcode + +Inside a project/package that uses this command plugin, right-click the project and select "SourceryCommand" from the "SourceryPlugins" menu group. + +> ⚠️ Note that this is only available from Xcode 14 onwards. + ### Command line options - `--sources` - Path to a source swift files or directories. You can provide multiple paths using multiple `--sources` option. From 08ac7753e7a1453c25f53701a6f030b767138372 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 17:41:21 +0100 Subject: [PATCH 14/18] Temporarily removes `cachePath` parameter --- .../SourceryCommandPlugin/SourceryCommandPlugin.swift | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 18260843f..0ac74cd39 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -3,7 +3,7 @@ import Foundation @main struct SourceryCommandPlugin { - private func run(_ sourcery: String, withConfig configFilePath: String, cachePath: String) throws { + private func run(_ sourcery: String, withConfig configFilePath: String) throws { let sourceryURL = URL(fileURLWithPath: sourcery) let process = Process() @@ -11,8 +11,7 @@ struct SourceryCommandPlugin { process.arguments = [ "--config", configFilePath, - "--cacheBasePath", - cachePath + "--disableCache" ] try process.run() @@ -39,7 +38,7 @@ extension SourceryCommandPlugin: CommandPlugin { return } - try run(sourcery, withConfig: configFilePath, cachePath: context.pluginWorkDirectory.string) + try run(sourcery, withConfig: configFilePath) } } } @@ -63,7 +62,7 @@ extension SourceryCommandPlugin: XcodeCommandPlugin { } let sourcery = try context.tool(named: "SourceryExecutable").path.string - try run(sourcery, withConfig: configFilePath, cachePath: context.pluginWorkDirectory.string) + try run(sourcery, withConfig: configFilePath) } } } From 050157f70145c90a5f578b6bd3df65cca2017f80 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Fri, 9 Sep 2022 17:59:39 +0100 Subject: [PATCH 15/18] Fixes typo in `README.md` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 23133b3a4..870f54560 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ $ swift package --list To run the code generator, you need to allow changes to the project with the `--allow-writing-to-package-directory` flag: ``` -$ swift package --allow-writing-to-package-directory sourcery-code-generation +$ swift package --allow-writing-to-package-directory sourcery-command ``` #### Running in Xcode From eb5c23a5726343a8ba9cec9b2cedb6e918d6fb68 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Sun, 11 Sep 2022 20:17:37 +0100 Subject: [PATCH 16/18] Fixes mistake in `Readme.md` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 870f54560..65b1572d4 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ To provide a configuration for the plugin to use, place a `.sourcery.yml` file a To verify the plugin can be found by SwiftPM, use: ``` -$ swift package --list +$ swift package plugin --list ``` To run the code generator, you need to allow changes to the project with the `--allow-writing-to-package-directory` flag: From 368ae703b43cc5e456c26cf92ac536981dfe06b2 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Tue, 13 Sep 2022 20:08:21 +0100 Subject: [PATCH 17/18] Adds `cacheBasePath` option to executable --- .../SourceryCommandPlugin/SourceryCommandPlugin.swift | 9 +++++---- README.md | 1 + SourceryExecutable/main.swift | 11 +++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift index 0ac74cd39..a4d4405eb 100644 --- a/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift +++ b/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift @@ -3,7 +3,7 @@ import Foundation @main struct SourceryCommandPlugin { - private func run(_ sourcery: String, withConfig configFilePath: String) throws { + private func run(_ sourcery: String, withConfig configFilePath: String, cacheBasePath: String) throws { let sourceryURL = URL(fileURLWithPath: sourcery) let process = Process() @@ -11,7 +11,8 @@ struct SourceryCommandPlugin { process.arguments = [ "--config", configFilePath, - "--disableCache" + "--cacheBasePath", + cacheBasePath ] try process.run() @@ -38,7 +39,7 @@ extension SourceryCommandPlugin: CommandPlugin { return } - try run(sourcery, withConfig: configFilePath) + try run(sourcery, withConfig: configFilePath, cacheBasePath: context.pluginWorkDirectory.string) } } } @@ -62,7 +63,7 @@ extension SourceryCommandPlugin: XcodeCommandPlugin { } let sourcery = try context.tool(named: "SourceryExecutable").path.string - try run(sourcery, withConfig: configFilePath) + try run(sourcery, withConfig: configFilePath, cacheBasePath: context.pluginWorkDirectory.string) } } } diff --git a/README.md b/README.md index 65b1572d4..ddbd6d4f5 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ Inside a project/package that uses this command plugin, right-click the project - `--prune` [default: false] - Prune empty generated files - `--version` - Display the current version of Sourcery - `--help` - Display help information +- `--cacheBasePath` - Base path to the cache directory. Can be overriden by the config file. ### Configuration file diff --git a/SourceryExecutable/main.swift b/SourceryExecutable/main.swift index 0b0286dbe..ae2a3a27c 100644 --- a/SourceryExecutable/main.swift +++ b/SourceryExecutable/main.swift @@ -111,8 +111,9 @@ func runCLI() { or should be passed one by one (e.g. --args arg1=value --args arg2). Arguments are accessible in templates \ via `argument.`. To pass in string you should use escaped quotes (\\"). """), - Option("ejsPath", default: "", description: "Path to EJS file for JavaScript templates.") - ) { watcherEnabled, disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, configPaths, forceParse, args, ejsPath in + Option("ejsPath", default: "", description: "Path to EJS file for JavaScript templates."), + Option("cacheBasePath", default: "", description: "Base path to Sourcery's cache directory") + ) { watcherEnabled, disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, configPaths, forceParse, args, ejsPath, cacheBasePath in do { switch (quiet, verboseLogging) { case (true, _): @@ -140,7 +141,7 @@ func runCLI() { sources: Paths(include: sources, exclude: excludeSources) , templates: Paths(include: templates, exclude: excludeTemplates), output: output.string.isEmpty ? "." : output, - cacheBasePath: Path.defaultBaseCachePath, + cacheBasePath: cacheBasePath.string.isEmpty ? Path.defaultBaseCachePath : cacheBasePath, forceParse: forceParse, parseDocumentation: parseDocumentation, args: arguments @@ -187,11 +188,13 @@ func runCLI() { let keepAlive = try configurations.flatMap { configuration -> [FolderWatcher.Local] in configuration.validate() + + let shouldUseCacheBasePathArg = configuration.cacheBasePath == Path.defaultBaseCachePath && !cacheBasePath.string.isEmpty let sourcery = Sourcery(verbose: verboseLogging, watcherEnabled: watcherEnabled, cacheDisabled: disableCache, - cacheBasePath: configuration.cacheBasePath, + cacheBasePath: shouldUseCacheBasePathArg ? cacheBasePath : configuration.cacheBasePath, prune: prune, serialParse: serialParse, arguments: configuration.args) From a6846cc27a962a4dabaf831b5717fb8166836728 Mon Sep 17 00:00:00 2001 From: Pol Piella Abadia Date: Tue, 13 Sep 2022 20:54:41 +0100 Subject: [PATCH 18/18] Adds `CHANGELOG.md` entry for changes to library code --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6b9404d..332d0fdc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 1.8.3 - Update StencilSwiftKit to fix SPM resolving issue when building as a Command Plugin [#1023](https://github.com/krzysztofzablocki/Sourcery/issues/1023) +- Adds new `--cacheBasePath` option to `SourceryExecutable` to allow for plugins setting a default cache [#1093](https://github.com/krzysztofzablocki/Sourcery/pull/1093) ## 1.8.2 ## New Features