-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunch_process.rs
305 lines (270 loc) · 9.75 KB
/
launch_process.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::dotnet::project::ProjectType;
use crate::dotnet::solution::Solution;
use crate::Project;
use libcnb::data::launch::{Process, ProcessBuilder, ProcessType};
use libcnb::data::process_type;
use std::path::{Path, PathBuf};
/// Detects processes in a solution's projects
pub(crate) fn detect_solution_processes(solution: &Solution) -> Vec<Process> {
solution
.projects
.iter()
.filter_map(|project| project_launch_process(solution, project))
.collect()
}
/// Determines if a project should have a launchable process and constructs it
fn project_launch_process(solution: &Solution, project: &Project) -> Option<Process> {
if !matches!(
project.project_type,
ProjectType::ConsoleApplication | ProjectType::WebApplication | ProjectType::WorkerService
) {
return None;
}
let relative_executable_path = relative_executable_path(solution, project);
let command = build_command(&relative_executable_path, project.project_type);
let process_type = match project.project_type {
// If project is a web application, and there's only one web application in the solution,
// set the process type to `web`.
ProjectType::WebApplication
if solution
.projects
.iter()
.filter(|p| p.project_type == ProjectType::WebApplication)
.count()
== 1 =>
{
process_type!("web")
}
_ => project_process_type(project),
};
Some(ProcessBuilder::new(process_type, ["bash", "-c", &command]).build())
}
/// Constructs the shell command for launching the process
fn build_command(relative_executable_path: &Path, project_type: ProjectType) -> String {
let parent_dir = relative_executable_path
.parent()
.expect("Executable path should always have a parent directory")
.to_str()
.expect("Path should be valid UTF-8");
let file_name = relative_executable_path
.file_name()
.expect("Executable path should always have a file name")
.to_str()
.expect("Path should be valid UTF-8");
let mut command = format!(
"cd {}; ./{}",
shell_words::quote(parent_dir),
shell_words::quote(file_name)
);
if project_type == ProjectType::WebApplication {
command.push_str(" --urls http://*:$PORT");
}
command
}
/// Returns a sanitized process type name, ensuring it is always valid
fn project_process_type(project: &Project) -> ProcessType {
sanitize_process_type_name(&project.assembly_name)
.parse::<ProcessType>()
.expect("Sanitized process type name should always be valid")
}
/// Returns the (expected) relative executable path from the solution's parent directory
fn relative_executable_path(solution: &Solution, project: &Project) -> PathBuf {
project_executable_path(project)
.strip_prefix(
solution
.path
.parent()
.expect("Solution file should be in a directory"),
)
.expect("Executable path should inside the solution's directory")
.to_path_buf()
}
/// Returns the (expected) absolute path to the project's compiled executable
fn project_executable_path(project: &Project) -> PathBuf {
project
.path
.parent()
.expect("Project file should always have a parent directory")
.join("bin")
.join("publish")
.join(&project.assembly_name)
}
/// Sanitizes a process type name to only contain allowed characters
fn sanitize_process_type_name(input: &str) -> String {
input
.chars()
.filter(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_'))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use libcnb::data::launch::{Process, WorkingDirectory};
use libcnb::data::process_type;
use std::path::PathBuf;
fn create_test_project(path: &str, assembly_name: &str, project_type: ProjectType) -> Project {
Project {
path: PathBuf::from(path),
target_framework: "net9.0".to_string(),
project_type,
assembly_name: assembly_name.to_string(),
}
}
#[test]
fn test_detect_solution_processes_single_web_app() {
let solution = Solution {
path: PathBuf::from("/tmp/foo.sln"),
projects: vec![create_test_project(
"/tmp/bar/bar.csproj",
"bar",
ProjectType::WebApplication,
)],
};
let expected_processes = vec![Process {
r#type: process_type!("web"),
command: vec![
"bash".to_string(),
"-c".to_string(),
"cd bar/bin/publish; ./bar --urls http://*:$PORT".to_string(),
],
args: vec![],
default: false,
working_directory: WorkingDirectory::App,
}];
assert_eq!(detect_solution_processes(&solution), expected_processes);
}
#[test]
fn test_detect_solution_processes_multiple_web_apps() {
let solution = Solution {
path: PathBuf::from("/tmp/foo.sln"),
projects: vec![
create_test_project("/tmp/bar/bar.csproj", "bar", ProjectType::WebApplication),
create_test_project("/tmp/baz/baz.csproj", "baz", ProjectType::WebApplication),
],
};
assert_eq!(
detect_solution_processes(&solution)
.iter()
.map(|process| process.r#type.clone())
.collect::<Vec<ProcessType>>(),
vec![process_type!("bar"), process_type!("baz")]
);
}
#[test]
fn test_detect_solution_processes_single_web_app_and_console_app() {
let solution = Solution {
path: PathBuf::from("/tmp/foo.sln"),
projects: vec![
create_test_project("/tmp/qux/qux.csproj", "qux", ProjectType::Unknown),
create_test_project("/tmp/bar/bar.csproj", "bar", ProjectType::WebApplication),
create_test_project(
"/tmp/baz/baz.csproj",
"baz",
ProjectType::ConsoleApplication,
),
],
};
assert_eq!(
detect_solution_processes(&solution)
.iter()
.map(|process| process.r#type.clone())
.collect::<Vec<ProcessType>>(),
vec![process_type!("web"), process_type!("baz")]
);
}
#[test]
fn test_detect_solution_processes_with_spaces() {
let solution = Solution {
path: PathBuf::from("/tmp/My Solution With Spaces.sln"),
projects: vec![create_test_project(
"/tmp/My Project With Spaces/project.csproj",
"My App",
ProjectType::ConsoleApplication,
)],
};
let expected_processes = vec![Process {
r#type: process_type!("MyApp"),
command: vec![
"bash".to_string(),
"-c".to_string(),
"cd 'My Project With Spaces/bin/publish'; ./'My App'".to_string(),
],
args: vec![],
default: false,
working_directory: WorkingDirectory::App,
}];
assert_eq!(detect_solution_processes(&solution), expected_processes);
}
#[test]
fn test_relative_executable_path() {
let solution = Solution {
path: PathBuf::from("/tmp/solution.sln"),
projects: vec![],
};
let project = create_test_project(
"/tmp/project/project.csproj",
"TestApp",
ProjectType::ConsoleApplication,
);
assert_eq!(
relative_executable_path(&solution, &project),
PathBuf::from("project/bin/publish/TestApp")
);
}
#[test]
fn test_project_executable_path() {
let project = create_test_project(
"/tmp/project/project.csproj",
"TestApp",
ProjectType::ConsoleApplication,
);
assert_eq!(
project_executable_path(&project),
PathBuf::from("/tmp/project/bin/publish/TestApp")
);
}
#[test]
fn test_build_command_with_spaces() {
let executable_path = PathBuf::from("some/project with spaces/bin/publish/My App");
assert_eq!(
build_command(&executable_path, ProjectType::ConsoleApplication),
"cd 'some/project with spaces/bin/publish'; ./'My App'"
);
assert_eq!(
build_command(&executable_path, ProjectType::WebApplication),
"cd 'some/project with spaces/bin/publish'; ./'My App' --urls http://*:$PORT"
);
}
#[test]
fn test_build_command_with_special_chars() {
let executable_path =
PathBuf::from("some/project with #special$chars/bin/publish/My-App+v1.2_Release!");
assert_eq!(
build_command(&executable_path, ProjectType::ConsoleApplication),
"cd 'some/project with #special$chars/bin/publish'; ./My-App+v1.2_Release!"
);
}
#[test]
fn test_sanitize_process_type_name() {
assert_eq!(
sanitize_process_type_name("Hello, world! 123"),
"Helloworld123"
);
assert_eq!(
sanitize_process_type_name("This_is-a.test.123.abc"),
"This_is-a.test.123.abc"
);
assert_eq!(
sanitize_process_type_name("Special chars: !@#$%+^&*()"),
"Specialchars"
);
assert_eq!(
sanitize_process_type_name("Mixed: aBc123.xyz_-!@#"),
"MixedaBc123.xyz_-"
);
assert_eq!(
sanitize_process_type_name("Unicode: 日本語123"),
"Unicode123"
);
}
}