Skip to content

Commit

Permalink
Add bin args for watch and serve commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy committed Jan 31, 2024
1 parent 0a14df0 commit b14006c
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/compile/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn dev_opts() -> Opts {
#[test]
fn test_project_dev() {
let cli = dev_opts();
let conf = Config::test_load(cli, "examples", "examples/project/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/project/Cargo.toml", true, None);

let mut command = Command::new("cargo");
let (envs, cargo) = build_cargo_server_cmd("build", &conf.projects[0], &mut command);
Expand Down Expand Up @@ -70,7 +70,7 @@ fn test_project_dev() {
#[test]
fn test_project_release() {
let cli = release_opts();
let conf = Config::test_load(cli, "examples", "examples/project/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/project/Cargo.toml", true, None);

let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_server_cmd("build", &conf.projects[0], &mut command);
Expand Down Expand Up @@ -112,7 +112,7 @@ fn test_workspace_project1() {
};

let cli = dev_opts();
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true, None);

let mut command = Command::new("cargo");
let (envs, cargo) = build_cargo_server_cmd("build", &conf.projects[0], &mut command);
Expand All @@ -134,7 +134,7 @@ fn test_workspace_project1() {
#[test]
fn test_workspace_project2() {
let cli = dev_opts();
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true, None);

let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_server_cmd("build", &conf.projects[1], &mut command);
Expand All @@ -158,7 +158,7 @@ fn test_extra_cargo_args() {
bin_cargo_args: Some(vec!["-j".into(), "16".into()]),
..dev_opts()
};
let conf = Config::test_load(cli, "examples", "examples/project/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/project/Cargo.toml", true, None);

let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_server_cmd("build", &conf.projects[0], &mut command);
Expand Down
3 changes: 3 additions & 0 deletions src/config/bin_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct BinPackage {
pub target_dir: Option<String>,
pub cargo_command: Option<String>,
pub cargo_args: Option<Vec<String>>,
pub bin_args: Option<Vec<String>>,
}

impl BinPackage {
Expand All @@ -34,6 +35,7 @@ impl BinPackage {
metadata: &Metadata,
project: &ProjectDefinition,
config: &ProjectConfig,
bin_args: Option<&[String]>,
) -> Result<Self> {
let mut features = if !cli.bin_features.is_empty() {
cli.bin_features.clone()
Expand Down Expand Up @@ -128,6 +130,7 @@ impl BinPackage {
target_dir: config.bin_target_dir.clone(),
cargo_command: config.bin_cargo_command.clone(),
cargo_args: cli.bin_cargo_args.clone(),
bin_args: bin_args.map(ToOwned::to_owned),
})
}
}
Expand Down
26 changes: 21 additions & 5 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ pub struct Opts {
pub verbose: u8,
}

#[derive(Debug, Clone, Parser, PartialEq, Default)]
pub struct BinOpts {
#[command(flatten)]
opts: Opts,

#[arg(trailing_var_arg = true)]
bin_args: Vec<String>,
}

#[derive(Debug, Parser)]
#[clap(version)]
pub struct Cli {
Expand All @@ -73,9 +82,16 @@ impl Cli {
use Commands::{Build, EndToEnd, New, Serve, Test, Watch};
match &self.command {
New(_) => None,
Build(opts) | Serve(opts) | Test(opts) | EndToEnd(opts) | Watch(opts) => {
Some(opts.clone())
}
Serve(bin_opts) | Watch(bin_opts) => Some(bin_opts.opts.clone()),
Build(opts) | Test(opts) | EndToEnd(opts) => Some(opts.clone()),
}
}

pub fn bin_args(&self) -> Option<&[String]> {
use Commands::{Serve, Watch};
match &self.command {
Serve(bin_opts) | Watch(bin_opts) => Some(bin_opts.bin_args.as_ref()),
_ => None,
}
}
}
Expand All @@ -89,9 +105,9 @@ pub enum Commands {
/// Start the server and end-2-end tests.
EndToEnd(Opts),
/// Serve. Defaults to hydrate mode.
Serve(Opts),
Serve(BinOpts),
/// Serve and automatically reload when files change.
Watch(Opts),
Watch(BinOpts),
/// Start a wizard for creating a new project (using cargo-generate).
New(NewCommand),
}
20 changes: 16 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ impl Debug for Config {
}

impl Config {
pub fn load(cli: Opts, cwd: &Utf8Path, manifest_path: &Utf8Path, watch: bool) -> Result<Self> {
pub fn load(
cli: Opts,
cwd: &Utf8Path,
manifest_path: &Utf8Path,
watch: bool,
bin_args: Option<&[String]>,
) -> Result<Self> {
let metadata = Metadata::load_cleaned(manifest_path)?;

let mut projects = Project::resolve(&cli, cwd, &metadata, watch).dot()?;
let mut projects = Project::resolve(&cli, cwd, &metadata, watch, bin_args).dot()?;

if projects.is_empty() {
bail!("Please define leptos projects in the workspace Cargo.toml sections [[workspace.metadata.leptos]]")
Expand All @@ -75,15 +81,21 @@ impl Config {
}

#[cfg(test)]
pub fn test_load(cli: Opts, cwd: &str, manifest_path: &str, watch: bool) -> Self {
pub fn test_load(
cli: Opts,
cwd: &str,
manifest_path: &str,
watch: bool,
bin_args: Option<&[String]>,
) -> Self {
use crate::ext::PathBufExt;

let manifest_path = Utf8PathBuf::from(manifest_path)
.canonicalize_utf8()
.unwrap();
let mut cwd = Utf8PathBuf::from(cwd).canonicalize_utf8().unwrap();
cwd.clean_windows_path();
Self::load(cli, &cwd, &manifest_path, watch).unwrap()
Self::load(cli, &cwd, &manifest_path, watch, bin_args).unwrap()
}

pub fn current_project(&self) -> Result<Arc<Project>> {
Expand Down
3 changes: 2 additions & 1 deletion src/config/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl Project {
cwd: &Utf8Path,
metadata: &Metadata,
watch: bool,
bin_args: Option<&[String]>,
) -> Result<Vec<Arc<Project>>> {
let projects = ProjectDefinition::parse(metadata)?;

Expand All @@ -90,7 +91,7 @@ impl Project {
working_dir: metadata.workspace_root.clone(),
name: project.name.clone(),
lib,
bin: BinPackage::resolve(cli, metadata, &project, &config)?,
bin: BinPackage::resolve(cli, metadata, &project, &config, bin_args)?,
style: StyleConfig::new(&config)?,
watch,
release: cli.release,
Expand Down
9 changes: 5 additions & 4 deletions src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn test_project() {
fn test_workspace() {
let cli = opts(None);

let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true, None);

insta::assert_debug_snapshot!(conf);
}
Expand All @@ -39,7 +39,7 @@ fn test_workspace() {
fn test_workspace_project1() {
let cli = opts(Some("project1"));

let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true, None);

insta::assert_debug_snapshot!(conf);
}
Expand All @@ -48,20 +48,21 @@ fn test_workspace_project1() {
fn test_workspace_project2() {
let cli = opts(Some("project2"));

let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true);
let conf = Config::test_load(cli, "examples", "examples/workspace/Cargo.toml", true, None);

insta::assert_debug_snapshot!(conf);
}

#[test]
fn test_workspace_in_subdir_project2() {
fn test_workspabin_argsce_in_subdir_project2() {
let cli = opts(None);

let conf = Config::test_load(
cli,
"examples/workspace/project2",
"examples/workspace/Cargo.toml",
true,
None,
);

insta::assert_debug_snapshot!(conf);
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ pub async fn run(args: Cli) -> Result<()> {
cwd.clean_windows_path();

let opts = args.opts().unwrap();
let bin_args = args.bin_args();

let watch = matches!(args.command, Commands::Watch(_));
let config = Config::load(opts, &cwd, &manifest_path, watch).dot()?;
let config = Config::load(opts, &cwd, &manifest_path, watch, bin_args).dot()?;
env::set_current_dir(&config.working_dir).dot()?;
log::debug!(
"Path working dir {}",
Expand Down
14 changes: 13 additions & 1 deletion src/service/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct ServerProcess {
process: Option<Child>,
envs: Vec<(&'static str, String)>,
binary: Utf8PathBuf,
bin_args: Option<Vec<String>>,
}

impl ServerProcess {
Expand All @@ -67,6 +68,7 @@ impl ServerProcess {
process: None,
envs: proj.to_envs(),
binary: proj.bin.exe_file.clone(),
bin_args: proj.bin.bin_args.clone(),
}
}

Expand Down Expand Up @@ -134,8 +136,18 @@ impl ServerProcess {
bin.clone()
};

let bin_args = match &self.bin_args {
Some(bin_args) => bin_args.as_slice(),
None => &[],
};

log::debug!("Serve running {}", GRAY.paint(bin_path.as_str()));
let cmd = Some(Command::new(bin_path).envs(self.envs.clone()).spawn()?);
let cmd = Some(
Command::new(bin_path)
.envs(self.envs.clone())
.args(bin_args)
.spawn()?,
);
let port = self
.envs
.iter()
Expand Down

0 comments on commit b14006c

Please sign in to comment.