Skip to content

Commit 8520470

Browse files
committed
Add "firefly_cli emulator"
1 parent d3a74ac commit 8520470

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

src/args.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ pub enum Commands {
2323
Import(ImportArgs),
2424

2525
/// Bootstrap a new app.
26-
///
27-
/// Requires internet connection.
2826
#[clap(alias("create"), alias("bootstrap"))]
2927
New(NewArgs),
3028

29+
/// Launch firefly-emulator.
30+
Emulator(EmulatorArgs),
31+
3132
/// List all badges (aka achievements) defined in the given app.
3233
#[clap(alias("badge"), alias("achievements"), alias("achievement"))]
3334
Badges(BadgesArgs),
@@ -232,6 +233,12 @@ pub struct NewArgs {
232233
pub lang: String,
233234
}
234235

236+
#[derive(Debug, Parser)]
237+
pub struct EmulatorArgs {
238+
/// Arguments to pass into the emulator.
239+
pub args: Vec<String>,
240+
}
241+
235242
#[derive(Debug, Parser)]
236243
pub struct MonitorArgs {}
237244

src/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn run_command(vfs: PathBuf, command: &Commands) -> anyhow::Result<()> {
99
Commands::Export(args) => cmd_export(&vfs, args),
1010
Commands::Import(args) => cmd_import(&vfs, args),
1111
Commands::New(args) => cmd_new(args),
12+
Commands::Emulator(args) => cmd_emulator(args),
1213
Commands::Badges(args) => cmd_badges(&vfs, args),
1314
Commands::Boards(args) => cmd_boards(&vfs, args),
1415
Commands::Cheat(args) => cmd_cheat(args),

src/commands/emulator.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::{args::EmulatorArgs, langs::check_output};
2+
use anyhow::{bail, Context, Result};
3+
use std::process::Command;
4+
5+
pub fn cmd_emulator(args: &EmulatorArgs) -> Result<()> {
6+
let executed_dev = run_dev(args)?;
7+
if executed_dev {
8+
return Ok(());
9+
}
10+
let bins = [
11+
"./firefly_emulator",
12+
"./firefly_emulator.exe",
13+
"firefly_emulator",
14+
"firefly_emulator.exe",
15+
"firefly-emulator",
16+
"firefly-emulator.exe",
17+
];
18+
for bin in bins {
19+
if binary_exists(bin) {
20+
println!("running {bin}...");
21+
let output = Command::new(bin).args(&args.args).output()?;
22+
check_output(&output).context("run emulator")?;
23+
}
24+
}
25+
bail!("emulator not installed");
26+
}
27+
28+
fn run_dev(args: &EmulatorArgs) -> Result<bool> {
29+
// Check common places where firefly repo might be clonned.
30+
// If found, run the dev version using cargo.
31+
let Some(base_dirs) = directories::BaseDirs::new() else {
32+
return Ok(false);
33+
};
34+
if !binary_exists("cargo") {
35+
return Ok(false);
36+
}
37+
let home = base_dirs.home_dir();
38+
let paths = [
39+
home.join("Documents").join("firefly"),
40+
home.join("ff").join("firefly"),
41+
home.join("github").join("firefly"),
42+
home.join("firefly"),
43+
];
44+
for dir_path in paths {
45+
let cargo_path = dir_path.join("Cargo.toml");
46+
if !cargo_path.is_file() {
47+
continue;
48+
}
49+
println!("running dev version from {}...", dir_path.to_str().unwrap());
50+
let output = Command::new("cargo")
51+
.arg("run")
52+
.arg("--")
53+
.args(&args.args)
54+
.current_dir(dir_path)
55+
.output()?;
56+
check_output(&output).context("run emulator")?;
57+
return Ok(true);
58+
}
59+
Ok(false)
60+
}
61+
62+
fn binary_exists(bin: &str) -> bool {
63+
let output = Command::new(bin).arg("--help").output();
64+
if let Ok(output) = output {
65+
if output.status.success() {
66+
return true;
67+
}
68+
}
69+
false
70+
}

src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod boards;
33
mod build;
44
mod catalog;
55
mod cheat;
6+
mod emulator;
67
mod export;
78
mod import;
89
mod inspect;
@@ -18,6 +19,7 @@ pub use boards::cmd_boards;
1819
pub use build::cmd_build;
1920
pub use catalog::{cmd_catalog_list, cmd_catalog_show};
2021
pub use cheat::cmd_cheat;
22+
pub use emulator::cmd_emulator;
2123
pub use export::cmd_export;
2224
pub use import::cmd_import;
2325
pub use inspect::cmd_inspect;

0 commit comments

Comments
 (0)