Skip to content

Commit

Permalink
Attach console when CLI runs on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
InoUno committed Feb 9, 2024
1 parent dd883cf commit 8bfeee8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions client/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ clap = { version = "4.5.0", features = ["derive"] }
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]

[target."cfg(windows)".dependencies]
windows = { version = "0.52.0", features = ["Win32_System_Console", "Win32_Foundation"] }
22 changes: 17 additions & 5 deletions client/src-tauri/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,31 @@ enum Commands {
},
}

pub fn check_cli() -> Result<bool> {
fn attach_console() {
#[cfg(windows)]
{
use windows::Win32::System::Console::{AttachConsole, ATTACH_PARENT_PROCESS};
let _ = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) };
}
}

pub fn check_cli() {
if std::env::args_os().len() > 1 {
// Ensure a console is attached on Windows
attach_console();
}

let args = Args::parse();

if let Some(command) = args.command {
match command {
Commands::ExportDats { project_dir } => {
export_all_dats(project_dir)?;
export_all_dats(project_dir).unwrap();
}
}
return Ok(true);
}

return Ok(false);
std::process::exit(0);
}
}

pub fn export_all_dats(project_dir: String) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions client/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ pub const DAT_GENERATION_DIR: &'static str = "generated_dats";
pub const ZONE_MAPPING_FILE: &'static str = "zones.yml";

fn main() {
if check_cli().unwrap() {
return;
}
check_cli();

#[cfg(debug_assertions)]
ts::export(
Expand Down

0 comments on commit 8bfeee8

Please sign in to comment.