Skip to content

Commit 8099cc9

Browse files
committed
Add environment variables that can refer to context of the plugin
1 parent 62bf2dc commit 8099cc9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Plugins/RunScriptCommandPlugin/plugin.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ struct RunScriptCommandPlugin: CommandPlugin {
1313
func performCommand(context: PackagePlugin.PluginContext, arguments: [String]) async throws {
1414
try performCommand(
1515
packageDirectory: context.package.directory,
16+
workingDirectory: context.pluginWorkDirectory,
1617
tool: try context.tool(named: "run-script-bin"),
1718
arguments: arguments
1819
)
1920
}
2021

2122
private func performCommand(
2223
packageDirectory: Path,
24+
workingDirectory: Path,
2325
tool: PluginContext.Tool,
2426
arguments: [String]
2527
) throws {
@@ -36,10 +38,24 @@ struct RunScriptCommandPlugin: CommandPlugin {
3638
"--timing",
3739
timing
3840
]
41+
process.environment = environments(
42+
packageDirectory: packageDirectory,
43+
workingDirectory: workingDirectory
44+
)
3945

4046
try process.run()
4147
process.waitUntilExit()
4248
}
49+
50+
private func environments(
51+
packageDirectory: Path,
52+
workingDirectory: Path
53+
) -> [String: String] {
54+
var environments = ProcessInfo.processInfo.environment
55+
environments["RUN_SCRIPT_TARGET_PACKAGE_DIR"] = packageDirectory.string
56+
environments["RUN_SCRIPT_PLUGIN_WORK_DIR"] = workingDirectory.string
57+
return environments
58+
}
4359
}
4460

4561
#if canImport(XcodeProjectPlugin)
@@ -49,6 +65,7 @@ extension RunScriptCommandPlugin: XcodeCommandPlugin {
4965
func performCommand(context: XcodeProjectPlugin.XcodePluginContext, arguments: [String]) throws {
5066
try performCommand(
5167
packageDirectory: context.xcodeProject.directory,
68+
workingDirectory: context.pluginWorkDirectory,
5269
tool: try context.tool(named: "run-script-bin"),
5370
arguments: arguments
5471
)

Plugins/RunScriptPlugin/plugin.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,37 @@ struct RunScriptPlugin: BuildToolPlugin {
3030

3131
let arguments = ["--config", configuration.string]
3232

33+
let environments = environments(
34+
packageDirectory: packageDirectory,
35+
workingDirectory: workingDirectory
36+
)
37+
3338
return [
3439
.prebuildCommand(
3540
displayName: "RunScriptPlugin(PreBuild)",
3641
executable: tool.path,
3742
arguments: arguments + ["--timing", "prebuild"],
43+
environment: environments,
3844
outputFilesDirectory: workingDirectory
3945
),
4046
.buildCommand(
4147
displayName: "RunScriptPlugin(Build)",
4248
executable: tool.path,
43-
arguments: arguments + ["--timing", "build"]
49+
arguments: arguments + ["--timing", "build"],
50+
environment: environments
4451
)
4552
]
4653
}
54+
55+
private func environments(
56+
packageDirectory: Path,
57+
workingDirectory: Path
58+
) -> [String: any CustomStringConvertible] {
59+
var environments = ProcessInfo.processInfo.environment
60+
environments["RUN_SCRIPT_TARGET_PACKAGE_DIR"] = packageDirectory.string
61+
environments["RUN_SCRIPT_PLUGIN_WORK_DIR"] = workingDirectory.string
62+
return environments
63+
}
4764
}
4865

4966
#if canImport(XcodeProjectPlugin)

0 commit comments

Comments
 (0)