Skip to content

Commit

Permalink
Add bin args for watch and serve commands (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy authored Feb 22, 2024
1 parent b0c19a8 commit 31e83de
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 20 deletions.
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
4 changes: 4 additions & 0 deletions src/config/bin_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -32,6 +33,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 @@ -130,6 +132,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 All @@ -153,6 +156,7 @@ impl std::fmt::Debug for BinPackage {
.join(", "),
)
.field("profile", &self.profile)
.field("bin_args", &self.bin_args)
.finish_non_exhaustive()
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Config {
default_features: false,
src_paths: "project1/app/src, project1/server/src",
profile: Debug,
bin_args: None,
..
},
style: StyleConfig {
Expand Down Expand Up @@ -105,6 +106,7 @@ Config {
default_features: false,
src_paths: "project2/src",
profile: Debug,
bin_args: None,
..
},
style: StyleConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
source: src/config/tests.rs
expression: conf
---
Config {
projects: [
Project {
name: "project2",
lib: LibPackage {
name: "project2",
rel_dir: "project2",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/project2.wasm",
dest: "target/site/project2/pkg/project2.wasm",
site: "pkg/project2.wasm",
},
js_file: SiteFile {
dest: "target/site/project2/pkg/project2.js",
site: "pkg/project2.js",
},
features: [
"hydrate",
],
default_features: false,
output_name: "project2",
src_paths: "project2/src",
profile: Debug,
..
},
bin: BinPackage {
name: "project2",
rel_dir: "project2",
exe_file: "target/debug/project2",
target: "project2",
features: [
"ssr",
],
default_features: false,
src_paths: "project2/src",
profile: Debug,
bin_args: Some(
[
"--",
"--foo",
],
),
..
},
style: StyleConfig {
file: Some(
SourcedSiteFile {
source: "project2/src/main.scss",
dest: "target/site/project2/pkg/project2.css",
site: "pkg/project2.css",
},
),
browserquery: "defaults",
tailwind: None,
site_file: SiteFile {
dest: "target/site/project2/pkg/project2.css",
site: "pkg/project2.css",
},
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
reload: 127.0.0.1:3001,
root_dir: "target/site/project2",
pkg_dir: "pkg",
file_reg: {},
ext_file_reg: {},
},
end2end: None,
assets: Some(
AssetsConfig {
dir: "project2/src/assets",
},
),
..
},
],
cli: Opts {
release: false,
precompress: false,
hot_reload: false,
project: Some(
"project2",
),
features: [],
lib_features: [],
lib_cargo_args: None,
bin_features: [],
bin_cargo_args: None,
verbose: 0,
},
watch: true,
..
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Config {
default_features: false,
src_paths: "project2/src",
profile: Debug,
bin_args: None,
..
},
style: StyleConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Config {
default_features: false,
src_paths: "project1/app/src, project1/server/src",
profile: Debug,
bin_args: None,
..
},
style: StyleConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Config {
default_features: false,
src_paths: "project2/src",
profile: Debug,
bin_args: None,
..
},
style: StyleConfig {
Expand Down
22 changes: 19 additions & 3 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,7 +48,7 @@ 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);
}
Expand All @@ -62,6 +62,22 @@ fn test_workspace_in_subdir_project2() {
"examples/workspace/project2",
"examples/workspace/Cargo.toml",
true,
None,
);

insta::assert_debug_snapshot!(conf);
}

#[test]
fn test_workspace_bin_args_project2() {
let cli = opts(Some("project2"));

let conf = Config::test_load(
cli,
"examples",
"examples/workspace/Cargo.toml",
true,
Some(&["--".to_string(), "--foo".to_string()]),
);

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
Loading

0 comments on commit 31e83de

Please sign in to comment.