Skip to content

Commit 1bc50ad

Browse files
authored
Extend LLBuildManifestWriter to let shell commands specify environment and initial working directory (#3332)
1 parent 31a2289 commit 1bc50ad

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

Sources/LLBuildManifest/BuildManifest.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -96,13 +96,17 @@ public struct BuildManifest {
9696
inputs: [Node],
9797
outputs: [Node],
9898
args: [String],
99+
environ: [String: String] = [:],
100+
workingDir: String? = nil,
99101
allowMissingInputs: Bool = false
100102
) {
101103
let tool = ShellTool(
102104
description: description,
103105
inputs: inputs,
104106
outputs: outputs,
105107
args: args,
108+
environ: environ,
109+
workingDir: workingDir,
106110
allowMissingInputs: allowMissingInputs
107111
)
108112
commands[name] = Command(name: name, tool: tool)

Sources/LLBuildManifest/ManifestWriter.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ public final class ManifestToolStream {
134134
stream <<< " \(key): " <<< Format.asJSON(newValue) <<< "\n"
135135
}
136136
}
137+
138+
public subscript(key: String) -> [String: String] {
139+
get { fatalError() }
140+
set {
141+
stream <<< " \(key):\n"
142+
for (key, value) in newValue.sorted(by: { $0.key < $1.key }) {
143+
stream <<< " " <<< Format.asJSON(key) <<< ": " <<< Format.asJSON(value) <<< "\n"
144+
}
145+
}
146+
}
137147
}
138148

139149
extension Format {

Sources/LLBuildManifest/Tools.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -97,25 +97,37 @@ public struct ShellTool: ToolProtocol {
9797
public var inputs: [Node]
9898
public var outputs: [Node]
9999
public var args: [String]
100+
public var environ: [String: String]
101+
public var workingDir: String?
100102
public var allowMissingInputs: Bool
101103

102104
init(
103105
description: String,
104106
inputs: [Node],
105107
outputs: [Node],
106108
args: [String],
109+
environ: [String: String] = [:],
110+
workingDir: String? = nil,
107111
allowMissingInputs: Bool = false
108112
) {
109113
self.description = description
110114
self.inputs = inputs
111115
self.outputs = outputs
112116
self.args = args
117+
self.environ = environ
118+
self.workingDir = workingDir
113119
self.allowMissingInputs = allowMissingInputs
114120
}
115121

116122
public func write(to stream: ManifestToolStream) {
117123
stream["description"] = description
118124
stream["args"] = args
125+
if !environ.isEmpty {
126+
stream["env"] = environ
127+
}
128+
if let workingDir = workingDir {
129+
stream["working-directory"] = workingDir
130+
}
119131
if allowMissingInputs {
120132
stream["allow-missing-inputs"] = true
121133
}

Tests/BuildTests/LLBuildManifestTests.swift

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -53,6 +53,61 @@ final class LLBuildManifestTests: XCTestCase {
5353
outputs: ["<Foo>"]
5454
5555
56+
""")
57+
}
58+
59+
func testShellCommands() throws {
60+
var manifest = BuildManifest()
61+
62+
manifest.defaultTarget = "main"
63+
manifest.addShellCmd(
64+
name: "shelley",
65+
description: "Shelley, Keats, and Byron",
66+
inputs: [
67+
.file(AbsolutePath("/file.in]"))
68+
],
69+
outputs: [
70+
.file(AbsolutePath("/file.out"))
71+
],
72+
args: [
73+
"foo", "bar", "baz"
74+
],
75+
environ: [
76+
"ABC": "DEF",
77+
"G H": "I J K",
78+
],
79+
workingDir: "/wdir",
80+
allowMissingInputs: true
81+
)
82+
83+
manifest.addNode(.file(AbsolutePath("/file.out")), toTarget: "main")
84+
85+
let fs = InMemoryFileSystem()
86+
try ManifestWriter(fs).write(manifest, at: AbsolutePath("/manifest.yaml"))
87+
88+
let contents = try fs.readFileContents(AbsolutePath("/manifest.yaml"))
89+
90+
XCTAssertEqual(contents, """
91+
client:
92+
name: basic
93+
tools: {}
94+
targets:
95+
"main": ["/file.out"]
96+
default: "main"
97+
commands:
98+
"shelley":
99+
tool: shell
100+
inputs: ["/file.in]"]
101+
outputs: ["/file.out"]
102+
description: "Shelley, Keats, and Byron"
103+
args: ["foo","bar","baz"]
104+
env:
105+
"ABC": "DEF"
106+
"G H": "I J K"
107+
working-directory: "/wdir"
108+
allow-missing-inputs: true
109+
110+
56111
""")
57112
}
58113
}

0 commit comments

Comments
 (0)