Skip to content

Commit

Permalink
Merge pull request #784 from loco-rs/cargo-watch
Browse files Browse the repository at this point in the history
dx: loco watch reloads on changes + command aliases
  • Loading branch information
jondot authored Sep 26, 2024
2 parents c91c75e + 0509748 commit e41c886
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/demo/tests/cmd/cli.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Commands:
generate code generation creates a set of files and code templates based on a predefined set of rules
doctor Validate and diagnose configurations
version Display the app version
watch Watch and restart the app
help Print this message or the help of the given subcommand(s)

Options:
Expand Down
61 changes: 60 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cfg_if::cfg_if! {
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use duct::cmd;

use crate::{
app::{AppContext, Hooks},
Expand All @@ -36,7 +37,7 @@ use crate::{
},
environment::{resolve_from_env, Environment, DEFAULT_ENVIRONMENT},
gen::{self, Component, ScaffoldKind},
logger, task,
logger, task, Error,
};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand All @@ -62,6 +63,7 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Start an app
#[clap(alias("s"))]
Start {
/// start worker
#[arg(short, long, action)]
Expand All @@ -85,6 +87,7 @@ enum Commands {
/// Describe all application endpoints
Routes {},
/// Run a custom task
#[clap(alias("t"))]
Task {
/// Task name (identifier)
name: Option<String>,
Expand All @@ -111,6 +114,7 @@ enum Commands {
},
/// code generation creates a set of files and code templates based on a
/// predefined set of rules.
#[clap(alias("g"))]
Generate {
/// What to generate
#[command(subcommand)]
Expand All @@ -125,6 +129,17 @@ enum Commands {
},
/// Display the app version
Version {},

/// Watch and restart the app
#[clap(alias("w"))]
Watch {
/// start worker
#[arg(short, long, action)]
worker: bool,
/// start same-process server and worker
#[arg(short, long, action)]
server_and_worker: bool,
},
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -399,6 +414,7 @@ pub async fn playground<H: Hooks>() -> crate::Result<AppContext> {
/// }
/// ```
#[cfg(feature = "with-db")]
#[allow(clippy::too_many_lines)]
pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> {
let cli: Cli = Cli::parse();
let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into();
Expand Down Expand Up @@ -485,6 +501,28 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> {
Commands::Version {} => {
println!("{}", H::app_version(),);
}

Commands::Watch {
worker,
server_and_worker,
} => {
// cargo-watch -s 'cargo loco start'
let mut subcmd = vec!["cargo", "loco", "start"];
if worker {
subcmd.push("--worker");
} else if server_and_worker {
subcmd.push("--server-and-worker");
}

cmd("cargo-watch", &["-s", &subcmd.join(" ")])
.run()
.map_err(|err| {
Error::Message(format!(
"failed to start with `cargo-watch`. Did you `cargo install \
cargo-watch`?. error details: `{err}`",
))
})?;
}
}
Ok(())
}
Expand Down Expand Up @@ -552,6 +590,27 @@ pub async fn main<H: Hooks>() -> crate::Result<()> {
Commands::Version {} => {
println!("{}", H::app_version(),);
}
Commands::Watch {
worker,
server_and_worker,
} => {
// cargo-watch -s 'cargo loco start'
let mut subcmd = vec!["cargo", "loco", "start"];
if worker {
subcmd.push("--worker");
} else if server_and_worker {
subcmd.push("--server-and-worker");
}

cmd("cargo-watch", &["-s", &subcmd.join(" ")])
.run()
.map_err(|err| {
Error::Message(format!(
"failed to start with `cargo-watch`. Did you `cargo install \
cargo-watch`?. error details: `{err}`",
))
})?;
}
}
Ok(())
}
Expand Down

0 comments on commit e41c886

Please sign in to comment.