Skip to content

Commit ca30291

Browse files
Merge branch '331-restore-generator-analytics' into 'main'
Restore Generator Analytics objectbox-swift#331 See merge request objectbox/objectbox-swift-spm!5
2 parents 58116ea + d912e8d commit ca30291

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

Package.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ let package = Package(
4747
description: "Runs the ObjectBox code generator"
4848
),
4949
permissions: [
50-
.writeToPackageDirectory(reason: "Generate files in the package directory")
50+
.writeToPackageDirectory(reason: "Generate files in the package directory"),
51+
.allowNetworkConnections(
52+
scope: .all(ports: []),
53+
reason: "Sending generator analytics to the ObjectBox team"
54+
),
5155
]
5256
),
5357
dependencies: [

Plugins/GeneratorCommand/ObjectBoxCommand.swift

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,38 @@ struct GeneratorCommand: CommandPlugin {
3737
return "generated/EntityInfo-\(targetName).generated.swift"
3838
}
3939

40+
// Removes the `--target` option and its value from the arguments array
41+
// This is needed because the `--target` option is not recognized by the Sourcery executable
42+
private func removeTargetNameFromArgs(_ args: [String]) -> [String] {
43+
var filteredArgs = args
44+
if let targetIndex = filteredArgs.firstIndex(of: "--target") {
45+
// Remove --target and the following value (target name)
46+
filteredArgs.remove(at: targetIndex)
47+
if targetIndex < filteredArgs.count {
48+
filteredArgs.remove(at: targetIndex)
49+
}
50+
}
51+
return filteredArgs
52+
}
53+
4054
private func runGenerator(
41-
generator: PluginContext.Tool, targetPath: Path, codeFilePath: String, modelFilePath: String
55+
generator: PluginContext.Tool, targetPath: Path, codeFilePath: String, modelFilePath: String,
56+
args: [String] = []
4257
) {
4358
let modelFileTargetPath = targetPath.appending(modelFilePath).string
4459
let codeFileTargetPath = targetPath.appending(codeFilePath).string
4560

61+
// The generator does not support the --target argument and would error, remove it
62+
let filteredArgs = removeTargetNameFromArgs(args)
63+
4664
// Specify --sources for Xcode project setup as well, Sourcery does not seem able to detect Xcode project
47-
let args: [String] = [
48-
"--sources", targetPath.string,
49-
"--model-json", modelFileTargetPath,
50-
"--output", codeFileTargetPath,
51-
"--disableCache",
52-
"--verbose",
53-
"--no-statistics",
54-
]
65+
let args: [String] =
66+
[
67+
"--sources", targetPath.string,
68+
"--model-json", modelFileTargetPath,
69+
"--output", codeFileTargetPath,
70+
"--disableCache",
71+
] + filteredArgs
5572

5673
runGenerator(generator: generator, args: args)
5774
}
@@ -149,7 +166,7 @@ struct GeneratorCommand: CommandPlugin {
149166
let modelFilePath = buildModelJsonFilePath(targetName: target.name)
150167
runGenerator(
151168
generator: tool, targetPath: targetPath, codeFilePath: codeFilePath,
152-
modelFilePath: modelFilePath)
169+
modelFilePath: modelFilePath, args: arguments)
153170
}
154171
}
155172
}
@@ -186,7 +203,7 @@ struct GeneratorCommand: CommandPlugin {
186203
// the generator and let it find source files.
187204
runGenerator(
188205
generator: tool, targetPath: targetPath, codeFilePath: codeFilePath,
189-
modelFilePath: modelFilePath)
206+
modelFilePath: modelFilePath, args: arguments)
190207

191208
// TODO Add the generated files to the Xcode project
192209
Diagnostics.remark(

0 commit comments

Comments
 (0)