Skip to content

Commit ed24fc9

Browse files
committed
Release 1.9.2
1 parent 997ad8e commit ed24fc9

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ fastlane/test_output
8888
# https://github.com/johnno1962/injectionforxcode
8989

9090
iOSInjectionProject/
91+
.DS_Store
92+
.swiftpm

Package.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// swift-tools-version: 5.6
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "SourceryGenPlugin",
6+
products: [
7+
.plugin(name: "SourceryGenPlugin", targets: ["SourceryGenPlugin"])
8+
],
9+
dependencies: [],
10+
targets: [
11+
.plugin(
12+
name: "SourceryGenPlugin",
13+
capability: .buildTool(),
14+
dependencies: ["sourcery"]
15+
),
16+
.binaryTarget(
17+
name: "sourcery",
18+
url: "https://github.com/mgolovko/SourceryGenPlugin/releases/download/1.9.2/sourcery-1.9.2.artifactbundle.zip",
19+
checksum: "f176e608354386fc8622f343ffd4ebed09cf8f0ba175052904f86c2c988c6fab"
20+
)
21+
]
22+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Foundation
2+
import PackagePlugin
3+
4+
@main
5+
struct SourceryGenPlugin: BuildToolPlugin {
6+
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
7+
let fileManager = FileManager.default
8+
9+
let configurations: [Path] = [context.package.directory, target.directory]
10+
.map { $0.appending("sourcery.yml") }
11+
.filter { fileManager.fileExists(atPath: $0.string) }
12+
13+
guard validate(configurations: configurations, target: target) else {
14+
return []
15+
}
16+
17+
fileManager.forceClean(directory: context.pluginWorkDirectory)
18+
19+
return try configurations.map { configuration in
20+
try .swiftgen(using: configuration, context: context, target: target)
21+
}
22+
}
23+
}
24+
25+
// MARK: - Helpers
26+
27+
private extension SourceryGenPlugin {
28+
/// Validate the given list of configurations
29+
func validate(configurations: [Path], target: Target) -> Bool {
30+
guard !configurations.isEmpty else {
31+
Diagnostics.error("""
32+
No SourceryGen configurations found for target \(target.name). If you would like to generate sources for this \
33+
target include a `sourcery.yml` in the target's source directory, or include a shared `sourcery.yml` at the \
34+
package's root.
35+
""")
36+
return false
37+
}
38+
39+
return true
40+
}
41+
}
42+
43+
private extension Command {
44+
static func swiftgen(using configuration: Path, context: PluginContext, target: Target) throws -> Command {
45+
.prebuildCommand(
46+
displayName: "SourceryGen BuildTool Plugin",
47+
executable: try context.tool(named: "sourcery").path,
48+
arguments: [
49+
"--config",
50+
configuration,
51+
"--cacheBasePath",
52+
context.pluginWorkDirectory.string
53+
],
54+
outputFilesDirectory: context.pluginWorkDirectory
55+
)
56+
}
57+
}
58+
59+
private extension FileManager {
60+
/// Re-create the given directory
61+
func forceClean(directory: Path) {
62+
try? removeItem(atPath: directory.string)
63+
try? createDirectory(atPath: directory.string, withIntermediateDirectories: false)
64+
}
65+
}

0 commit comments

Comments
 (0)