Skip to content

Commit

Permalink
feat(cli): Add feature to search deploy path automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
nazo6 committed Dec 18, 2024
1 parent f2d3338 commit 6ef6a85
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 43 deletions.
88 changes: 88 additions & 0 deletions tools/cli/src/commands/build/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::path::{Path, PathBuf};

use colored::Colorize as _;

use crate::utils::xprintln;

pub fn deploy(
deploy_path_args: String,
uf2_path: PathBuf,
deploy_retry_count: u32,
) -> anyhow::Result<()> {
let bar = indicatif::ProgressBar::new(0)
.with_prefix(format!("{} ", " rktk ".on_blue()))
.with_style(
indicatif::ProgressStyle::with_template(
"{prefix} [{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
)
.unwrap(),
)
.with_position(0);

'abandoned: {
for i in 0..deploy_retry_count {
if i > 0 {
std::thread::sleep(std::time::Duration::from_millis(500));
}

bar.set_message(format!(
"Deploying (attempt {}/{})",
i + 1,
deploy_retry_count
));

let Ok(deploy_path) = get_deploy_path(deploy_path_args.clone(), &uf2_path) else {
continue;
};

match fs_extra::file::copy_with_progress(
&uf2_path,
&deploy_path,
&fs_extra::file::CopyOptions::new(),
|p| {
bar.set_length(p.total_bytes);
bar.set_position(p.copied_bytes);
},
) {
Ok(_) => {
bar.finish();

xprintln!("Success");
break 'abandoned;
}
Err(_e) => {}
}
}
bar.abandon_with_message("Abandoned");
anyhow::bail!("Failed to copy the uf2 file to the deploy directory");
}

Ok(())
}

fn get_deploy_path(deploy_path: String, uf2_path: &Path) -> anyhow::Result<PathBuf> {
let deploy_dir = if deploy_path == "auto" {
// search mount that have "INFO_UF2.txt" file
let mount_path = PathBuf::from("/mnt");
if !mount_path.exists() {
anyhow::bail!("No /mnt directory found");
}
let mut deploy_dir = None;
for entry in std::fs::read_dir(&mount_path)? {
let entry = entry?;
let path = entry.path();
if path.join("INFO_UF2.TXT").exists() {
deploy_dir = Some(path);
break;
}
}
if let Some(deploy_dir) = deploy_dir {
deploy_dir
} else {
anyhow::bail!("No mount found that have INFO_UF2.TXT file");
}
} else {
PathBuf::from(deploy_path)
};
Ok(deploy_dir.join(uf2_path.file_name().unwrap()))
}
45 changes: 2 additions & 43 deletions tools/cli/src/commands/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod config;
mod deploy;
mod mcu;
mod profile;
mod uf2;

use anyhow::Context as _;
use colored::Colorize as _;
use config::BuildConfig;
use mcu::BuildMcuList;
use profile::{BuildProfileList, PROFILE_CONFIG_TOML};
Expand Down Expand Up @@ -211,48 +211,7 @@ pub fn start(args: BuildCommand) -> anyhow::Result<()> {
)?;

if let Some(deploy_path) = args.deploy_dir {
let deploy_path = PathBuf::from(deploy_path).join(uf2_path.file_name().unwrap());

let bar = indicatif::ProgressBar::new(0)
.with_prefix(format!("{} ", " rktk ".on_blue()))
.with_style(
indicatif::ProgressStyle::with_template(
"{prefix} [{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
)
.unwrap(),
)
.with_position(0);

'abandoned: {
for i in 0..args.deploy_retry_count {
bar.set_message(format!(
"Copying... (attempt {}/{})",
i + 1,
args.deploy_retry_count
));
match fs_extra::file::copy_with_progress(
&uf2_path,
&deploy_path,
&fs_extra::file::CopyOptions::new(),
|p| {
bar.set_length(p.total_bytes);
bar.set_position(p.copied_bytes);
},
) {
Ok(_) => {
bar.finish();
break 'abandoned;
}
Err(_e) => {
std::thread::sleep(std::time::Duration::from_millis(500));
}
}
}
bar.abandon_with_message("Abandoned");
anyhow::bail!("Failed to copy the uf2 file to the deploy directory");
}

xprintln!("Uf2 file copied to: {}", deploy_path.display());
deploy::deploy(deploy_path, uf2_path, args.deploy_retry_count)?;
}
}

Expand Down

0 comments on commit 6ef6a85

Please sign in to comment.