Skip to content

Commit 10b14fb

Browse files
authored
Add cargo component serve subcommand (#305)
* add `serve` subcommand * change back from `cli` to `common`
1 parent d6f1496 commit 10b14fb

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/lib.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,19 @@ enum CargoCommand {
7979
Run,
8080
Test,
8181
Bench,
82+
Serve,
8283
}
8384

8485
impl CargoCommand {
8586
fn buildable(self) -> bool {
86-
matches!(self, Self::Build | Self::Run | Self::Test | Self::Bench)
87+
matches!(
88+
self,
89+
Self::Build | Self::Run | Self::Test | Self::Bench | Self::Serve
90+
)
8791
}
8892

8993
fn runnable(self) -> bool {
90-
matches!(self, Self::Run | Self::Test | Self::Bench)
94+
matches!(self, Self::Run | Self::Test | Self::Bench | Self::Serve)
9195
}
9296

9397
fn testable(self) -> bool {
@@ -103,6 +107,7 @@ impl fmt::Display for CargoCommand {
103107
Self::Run => write!(f, "run"),
104108
Self::Test => write!(f, "test"),
105109
Self::Bench => write!(f, "bench"),
110+
Self::Serve => write!(f, "serve"),
106111
Self::Other => write!(f, "<unknown>"),
107112
}
108113
}
@@ -116,6 +121,7 @@ impl From<&str> for CargoCommand {
116121
"r" | "run" => Self::Run,
117122
"t" | "test" => Self::Test,
118123
"bench" => Self::Bench,
124+
"serve" => Self::Serve,
119125
_ => Self::Other,
120126
}
121127
}
@@ -171,8 +177,8 @@ pub async fn run_cargo_command(
171177
);
172178

173179
let mut cargo = Command::new(&cargo_path);
174-
if command == CargoCommand::Run {
175-
// Treat a run as a build command as we need to componentize the output
180+
if matches!(command, CargoCommand::Run | CargoCommand::Serve) {
181+
// Treat run and serve as build commands as we need to componentize the output
176182
cargo.arg("build");
177183
if let Some(arg) = args.peek() {
178184
if Some((*arg).as_str()) == subcommand {
@@ -229,7 +235,7 @@ pub async fn run_cargo_command(
229235
}
230236

231237
let runner = if needs_runner && command.runnable() {
232-
Some(get_runner()?)
238+
Some(get_runner(command == CargoCommand::Serve)?)
233239
} else {
234240
None
235241
};
@@ -253,7 +259,7 @@ pub async fn run_cargo_command(
253259
Ok(outputs.into_iter().map(|o| o.path).collect())
254260
}
255261

256-
fn get_runner() -> Result<PathAndArgs> {
262+
fn get_runner(serve: bool) -> Result<PathAndArgs> {
257263
let cargo_config = cargo_config2::Config::load()?;
258264

259265
// We check here before we actually build that a runtime is present.
@@ -267,7 +273,11 @@ fn get_runner() -> Result<PathAndArgs> {
267273
.unwrap_or_else(|| {
268274
(
269275
PathAndArgs::new("wasmtime")
270-
.args(vec!["-S", "preview2", "-S", "common"])
276+
.args(if serve {
277+
vec!["serve"]
278+
} else {
279+
vec!["-S", "preview2", "-S", "common"]
280+
})
271281
.to_owned(),
272282
true,
273283
)
@@ -439,7 +449,8 @@ fn componentize_artifacts(
439449
};
440450

441451
if command.testable() && artifact.profile.test
442-
|| (command == CargoCommand::Run && !artifact.profile.test)
452+
|| (matches!(command, CargoCommand::Run | CargoCommand::Serve)
453+
&& !artifact.profile.test)
443454
{
444455
output.display = Some(output_display_name(
445456
cargo_metadata,
@@ -525,14 +536,14 @@ fn spawn_outputs(
525536
})
526537
.collect::<Vec<_>>();
527538

528-
if command == CargoCommand::Run && executables.len() > 1 {
539+
if matches!(command, CargoCommand::Run | CargoCommand::Serve) && executables.len() > 1 {
529540
config.terminal().error(
530-
"`cargo component run` can run at most one component, but multiple were specified",
541+
"`cargo component {command}` can run at most one component, but multiple were specified",
531542
)
532543
} else if executables.is_empty() {
533544
config.terminal().error(format!(
534545
"a component {ty} target must be available for `cargo component {command}`",
535-
ty = if command == CargoCommand::Run {
546+
ty = if matches!(command, CargoCommand::Run | CargoCommand::Serve) {
536547
"bin"
537548
} else {
538549
"test"

0 commit comments

Comments
 (0)