Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] CLI to load map and data paths #53

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Module that handles CLI to supply input files as arguments to the executable.
use bevy::prelude::{App, Entity, FileDragAndDrop};
use bevy::window::PrimaryWindow;
use std::env;
use std::io;
use std::path::PathBuf;
use thiserror::Error;

pub struct CliArgs {
pub map_path: Option<PathBuf>,
pub data_path: Option<PathBuf>,
}

#[derive(Error, Debug)]
pub enum InitCliError {
#[error("supplied path is invalid.")]
InvalidPathError(#[from] io::Error),
#[error("window not initialized.")]
UninitWindow,
}

pub fn parse_args() -> CliArgs {
let args: Vec<String> = env::args().collect();
// the last args take priority
let (map_path, data_path) = args.iter().skip(1).zip(args.iter().skip(2)).fold(
(None, None),
|(map, data), (arg, next)| match arg.as_str() {
"--map" | "-m" => (Some(PathBuf::from(next)), data),
"--data" | "-d" => (map, Some(PathBuf::from(next))),
_ => (map, data),
},
);

CliArgs {
map_path,
data_path,
}
}

/// Generate `FileDragAndDrop` such that the map and/or data
/// if supplied as CLI args are later loaded.
pub fn handle_cli_args(app: &mut App, cli_args: CliArgs) -> Result<(), InitCliError> {
let (win, _) = app
.world_mut()
.query::<(Entity, &PrimaryWindow)>()
.iter(app.world())
.next()
.ok_or(InitCliError::UninitWindow)?;
// paths are canonicalized so that they are not interpreted
// to be in the assets directory by bevy's `AssetLoader`.
if let Some(map_path) = cli_args.map_path {
app.world_mut().send_event(FileDragAndDrop::DroppedFile {
window: win,
path_buf: map_path.canonicalize()?,
});
}

if let Some(data_path) = cli_args.data_path {
app.world_mut().send_event(FileDragAndDrop::DroppedFile {
window: win,
path_buf: data_path.canonicalize()?,
});
}
Ok(())
}
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use bevy_pancam::{PanCam, PanCamPlugin};
use bevy_prototype_lyon::prelude::*;

mod aesthetics;
#[cfg(not(target_arch = "wasm32"))]
mod cli;
mod data;
mod escher;
mod funcplot;
Expand All @@ -23,7 +25,8 @@ use screenshot::{RawAsset, RawFontStorage};

#[cfg(not(target_arch = "wasm32"))]
fn main() {
App::new()
let mut app = App::new();
let app = app
.insert_resource(WinitSettings::desktop_app())
.add_plugins(
DefaultPlugins
Expand All @@ -47,8 +50,18 @@ fn main() {
.add_plugins(data::DataPlugin)
.add_systems(Startup, setup_system)
.add_plugins(aesthetics::AesPlugin)
.add_plugins(legend::LegendPlugin)
.run();
.add_plugins(legend::LegendPlugin);

let cli_args = cli::parse_args();
if let Err(e) = cli::handle_cli_args(app, cli_args) {
use cli::InitCliError::*;
match e {
InvalidPathError(error) => error!("Supplied path as arg is invalid: {error}"),
UninitWindow => error!("Window was not initialized!"),
}
}

app.run();
}

#[cfg(target_arch = "wasm32")]
Expand Down
Loading