Skip to content

Commit 9bc512d

Browse files
add graphen-cli
1 parent a6dc446 commit 9bc512d

File tree

8 files changed

+73
-40
lines changed

8 files changed

+73
-40
lines changed

desktop/bundle/build.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
fn main() {
22
println!("cargo:rerun-if-env-changed=CARGO_PROFILE");
33
println!("cargo:rerun-if-env-changed=PROFILE");
4-
let profile = std::env::var("CARGO_PROFILE").or_else(|_| std::env::var("PROFILE")).unwrap();
4+
let mut profile = std::env::var("CARGO_PROFILE").or_else(|_| std::env::var("PROFILE")).unwrap();
5+
if profile == "release"
6+
&& let Ok(debug_str) = std::env::var("DEBUG")
7+
&& debug_str.parse().unwrap_or(false)
8+
{
9+
profile = "profiling".to_string();
10+
}
511
println!("cargo:rustc-env=CARGO_PROFILE={profile}");
612

713
println!("cargo:rerun-if-env-changed=DEP_CEF_DLL_WRAPPER_CEF_DIR");

desktop/bundle/src/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ pub(crate) fn cef_path() -> PathBuf {
2929
}
3030

3131
pub(crate) fn build_bin(package: &str, bin: Option<&str>) -> Result<PathBuf, Box<dyn Error>> {
32-
let profile = &profile_name();
33-
let mut args = vec!["build", "--package", package, "--profile", profile];
32+
let mut args = vec!["build", "--package", package, "--profile", profile_name()];
3433
if let Some(bin) = bin {
3534
args.push("--bin");
3635
args.push(bin);

desktop/bundle/src/linux.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
66
// TODO: Implement bundling for linux
77

88
// TODO: Consider adding more useful cli
9-
if std::env::args().any(|a| a == "open") {
10-
run_command(&app_bin.to_string_lossy(), &[]).expect("failed to open app");
9+
let args: Vec<String> = std::env::args().collect();
10+
if let Some(pos) = args.iter().position(|a| a == "open") {
11+
let extra_args: Vec<&str> = args[pos + 1..].iter().map(|s| s.as_str()).collect();
12+
run_command(&app_bin.to_string_lossy(), &extra_args).expect("failed to open app");
1113
} else {
1214
eprintln!("Binary built and placed at {}", app_bin.to_string_lossy());
1315
eprintln!("Bundling for Linux is not yet implemented.");

desktop/bundle/src/mac.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ pub fn main() -> Result<(), Box<dyn Error>> {
2222
let app_dir = bundle(&profile_path, &app_bin, &helper_bin);
2323

2424
// TODO: Consider adding more useful cli
25-
if std::env::args().any(|a| a == "open") {
25+
let args: Vec<String> = std::env::args().collect();
26+
if let Some(pos) = args.iter().position(|a| a == "open") {
27+
let extra_args: Vec<&str> = args[pos + 1..].iter().map(|s| s.as_str()).collect();
2628
let executable_path = app_dir.join(EXEC_PATH).join(APP_NAME);
27-
run_command(&executable_path.to_string_lossy(), &[]).expect("failed to open app");
29+
run_command(&executable_path.to_string_lossy(), &extra_args).expect("failed to open app");
2830
}
2931

3032
Ok(())

desktop/bundle/src/win.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ pub fn main() -> Result<(), Box<dyn Error>> {
1212
let executable = bundle(&profile_path(), &app_bin);
1313

1414
// TODO: Consider adding more useful cli
15-
if std::env::args().any(|a| a == "open") {
15+
let args: Vec<String> = std::env::args().collect();
16+
if let Some(pos) = args.iter().position(|a| a == "open") {
1617
let executable_path = executable.to_string_lossy();
17-
run_command(&executable_path, &[]).expect("failed to open app")
18+
let extra_args: Vec<&str> = args[pos + 1..].iter().map(|s| s.as_str()).collect();
19+
run_command(&executable_path, &extra_args).expect("failed to open app")
1820
}
1921

2022
Ok(())

tools/building/src/deps.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn dependencies(task: &Task) -> Vec<Dependency> {
2626
args: &["--version"],
2727
name: "cargo-about",
2828
install: Some("cargo install cargo-about"),
29+
skip: Some(&|task| matches!(task.target, Target::Cli)),
2930
..Default::default()
3031
},
3132
Dependency {
@@ -39,7 +40,7 @@ fn dependencies(task: &Task) -> Vec<Dependency> {
3940
Task {
4041
target: Target::Web,
4142
action: Action::Run,
42-
profile: _
43+
..
4344
}
4445
)
4546
}),
@@ -51,19 +52,21 @@ fn dependencies(task: &Task) -> Vec<Dependency> {
5152
name: "wasm-bindgen-cli",
5253
version: Some("0.2.100"),
5354
install: Some("cargo install -f wasm-bindgen-cli@0.2.100"),
54-
..Default::default()
55+
skip: Some(&|task| matches!(task.target, Target::Cli)),
5556
},
5657
Dependency {
5758
command: "wasm-pack",
5859
args: &["--version"],
5960
name: "wasm-pack",
6061
install: Some("cargo install wasm-pack"),
62+
skip: Some(&|task| matches!(task.target, Target::Cli)),
6163
..Default::default()
6264
},
6365
Dependency {
6466
command: "node",
6567
args: &["--version"],
6668
name: "Node.js",
69+
skip: Some(&|task| matches!(task.target, Target::Cli)),
6770
..Default::default()
6871
},
6972
Dependency {

tools/building/src/lib.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod deps;
66
pub enum Target {
77
Web,
88
Desktop,
9+
Cli,
910
}
1011

1112
pub enum Action {
@@ -24,31 +25,45 @@ pub struct Task {
2425
pub target: Target,
2526
pub action: Action,
2627
pub profile: Profile,
28+
pub args: Vec<String>,
2729
}
2830

2931
impl Task {
3032
pub fn parse(args: &[&str]) -> Option<Self> {
33+
let split = args.iter().position(|a| *a == "--").unwrap_or(args.len());
34+
let passthru_args = args[split..].iter().skip(1).map(|s| s.to_string()).collect();
35+
let args = &args[..split];
36+
3137
let (target, rest) = match args.first() {
3238
Some(&"desktop") => (Target::Desktop, &args[1..]),
3339
Some(&"web") => (Target::Web, &args[1..]),
34-
_ => (Target::Web, args),
40+
Some(&"cli") => (Target::Cli, &args[1..]),
41+
Some(&"help") => return None,
42+
None => (Target::Web, args),
43+
_ => return None,
3544
};
3645

3746
let (action, rest) = match rest.first() {
3847
Some(&"build") => (Action::Build, &rest[1..]),
3948
Some(&"run") => (Action::Run, &rest[1..]),
40-
_ => (Action::Run, rest),
49+
None => (Action::Run, rest),
50+
_ => return None,
4151
};
4252

43-
let profile = match rest.first().copied().unwrap_or_default() {
44-
"" => Profile::Default,
45-
"release" => Profile::Release,
46-
"debug" => Profile::Debug,
47-
"profiling" => Profile::Profiling,
53+
let profile = match rest.first() {
54+
Some(&"release") => Profile::Release,
55+
Some(&"debug") => Profile::Debug,
56+
Some(&"profiling") => Profile::Profiling,
57+
None => Profile::Default,
4858
_ => return None,
4959
};
5060

51-
Some(Task { target, action, profile })
61+
Some(Task {
62+
target,
63+
action,
64+
profile,
65+
args: passthru_args,
66+
})
5267
}
5368
}
5469

tools/building/src/main.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ use building::*;
44

55
fn usage() {
66
eprintln!();
7-
eprintln!("Usage: cargo run [<command>] [release|debug|profiling]");
7+
eprintln!("Usage: cargo run [<command>] [release|debug|profiling] -- [args...]");
88
eprintln!();
99
eprintln!("Commands:");
1010
eprintln!(" web [run] Run the web app on local dev server");
1111
eprintln!(" web build Build the web app");
1212
eprintln!(" desktop [run] Run the desktop app");
1313
eprintln!(" desktop build Build the desktop app");
14+
eprintln!(" cli [run] Run the Graphen CLI");
15+
eprintln!(" cli build Build the Graphen CLI");
16+
eprintln!(" help Show this message");
1417
eprintln!();
1518
}
1619

@@ -45,29 +48,30 @@ fn run_task(task: &Task) -> Result<(), Error> {
4548
(Target::Web, Action::Build, Profile::Release | Profile::Default) => npm_run_in_frontend_dir("build")?,
4649
(Target::Web, Action::Build, Profile::Profiling) => npm_run_in_frontend_dir("build-profiling")?,
4750

48-
(Target::Desktop, Action::Run, Profile::Debug | Profile::Default) => {
49-
npm_run_in_frontend_dir("build-native-dev")?;
51+
(Target::Desktop, action, profile) => {
52+
if matches!((action, profile), (_, Profile::Release) | (Action::Build, Profile::Default)) {
53+
npm_run_in_frontend_dir("build-native")?;
54+
} else {
55+
npm_run_in_frontend_dir("build-native-dev")?;
56+
};
5057
run("cargo run -p third-party-licenses --features desktop")?;
51-
run("cargo run -p graphite-desktop-bundle -- open")?;
52-
}
53-
(Target::Desktop, Action::Run, Profile::Release) => {
54-
npm_run_in_frontend_dir("build-native")?;
55-
run("cargo run -p third-party-licenses --features desktop")?;
56-
run("cargo run -r -p graphite-desktop-bundle -- open")?;
57-
}
58-
(Target::Desktop, Action::Run, Profile::Profiling) => todo!("profiling run for desktop"),
5958

60-
(Target::Desktop, Action::Build, Profile::Debug) => {
61-
npm_run_in_frontend_dir("build-native-dev")?;
62-
run("cargo run -p third-party-licenses --features desktop")?;
63-
run("cargo run -p graphite-desktop-bundle")?;
59+
let cargo_profile = match profile {
60+
Profile::Debug | Profile::Default => "dev",
61+
Profile::Release => "release",
62+
Profile::Profiling => "profiling",
63+
};
64+
let args = if let Action::Run = action { format!(" -- open {}", task.args.join(" ")) } else { "".to_string() };
65+
run(&format!("cargo run --profile {cargo_profile} -p graphite-desktop-bundle{args}"))?;
6466
}
65-
(Target::Desktop, Action::Build, Profile::Release | Profile::Default) => {
66-
npm_run_in_frontend_dir("build-native")?;
67-
run("cargo run -p third-party-licenses --features desktop")?;
68-
run("cargo run -r -p graphite-desktop-bundle")?;
69-
}
70-
(Target::Desktop, Action::Build, Profile::Profiling) => todo!("profiling build for desktop"),
67+
68+
(Target::Cli, Action::Run, Profile::Debug | Profile::Default) => run(&format!("cargo run -p graphene-cli -- {}", task.args.join(" ")))?,
69+
(Target::Cli, Action::Run, Profile::Release) => run(&format!("cargo run -r -p graphene-cli -- {}", task.args.join(" ")))?,
70+
(Target::Cli, Action::Run, Profile::Profiling) => run(&format!("cargo run --profile profiling -p graphene-cli -- {}", task.args.join(" ")))?,
71+
72+
(Target::Cli, Action::Build, Profile::Debug) => run("cargo build -p graphene-cli")?,
73+
(Target::Cli, Action::Build, Profile::Release | Profile::Default) => run("cargo build -r -p graphene-cli")?,
74+
(Target::Cli, Action::Build, Profile::Profiling) => run("cargo build --profile profiling -p graphene-cli")?,
7175
}
7276
Ok(())
7377
}

0 commit comments

Comments
 (0)