Skip to content

Commit

Permalink
Use WalkDir for brevity
Browse files Browse the repository at this point in the history
  • Loading branch information
jackTabsCode committed Oct 31, 2024
1 parent 711cb92 commit 555ad05
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ asphalt.lock.toml
.env
asphalt.toml
.DS_Store
.asphalt-debug/
29 changes: 29 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ serde = { version = "1.0.203", features = ["derive"] }
serde-xml-rs = "0.6.0"
tokio = { version = "1.38.0" }
toml = "0.8.14"
walkdir = "2.5.0"

# The profile that 'cargo dist' will build with
[profile.dist]
Expand Down
79 changes: 36 additions & 43 deletions src/commands/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use codegen::{generate_luau, generate_ts};
use config::SyncConfig;
use log::{debug, info, warn};
use std::{
collections::{BTreeMap, VecDeque},
collections::BTreeMap,
path::{Path, PathBuf},
};
use tokio::fs::{read, read_dir, write, DirEntry};
use tokio::fs::{read, write};
use walkdir::{DirEntry, WalkDir};

mod backend;
mod codegen;
Expand Down Expand Up @@ -109,57 +110,49 @@ pub async fn sync(args: SyncArgs, existing_lockfile: LockFile) -> anyhow::Result
info!("Syncing...");

let mut assets = BTreeMap::<String, String>::new();
let mut remaining_items = VecDeque::new();
let mut synced = 0;
remaining_items.push_back(state.asset_dir.clone());

let backend = match state.target {
SyncTarget::Cloud => TargetBackend::Cloud(CloudBackend),
SyncTarget::Studio => TargetBackend::Studio(StudioBackend::new().await?),
SyncTarget::Debug => TargetBackend::Debug(DebugBackend::new().await?),
};

while let Some(path) = remaining_items.pop_front() {
let mut dir_entries = read_dir(path.clone())
.await
.with_context(|| format!("Failed to read directory: {}", path.to_str().unwrap()))?;
for entry in WalkDir::new(&state.asset_dir)
.into_iter()
.filter_map(|e| e.ok())
{
let path = entry.path();
if !path.is_file() {
continue;
}

while let Some(entry) = dir_entries
.next_entry()
.await
.with_context(|| format!("Failed to read directory entry: {:?}", path))?
{
let entry_path = entry.path();
if entry_path.is_dir() {
remaining_items.push_back(entry_path);
} else {
let path_str = entry_path.to_str().unwrap();
let fixed_path = fix_path(path_str);

if state.exclude_assets_matcher.is_match(path_str) {
continue;
}

let result = match process_file(&entry, &mut state, &backend).await {
Ok(Some(result)) => {
synced += 1;
result
}
Ok(None) => {
synced += 1;
continue;
}
Err(e) => {
warn!("Failed to process file {fixed_path}: {e:?}");
continue;
}
};

assets.insert(fixed_path.clone(), result.asset_id);
if let Some(file_entry) = result.file_entry {
state.new_lockfile.entries.insert(fixed_path, file_entry);
}
let path_str = path.to_str().unwrap();

let fixed_path = fix_path(path_str);

if state.exclude_assets_matcher.is_match(path_str) {
continue;
}

let result = match process_file(&entry, &mut state, &backend).await {
Ok(Some(result)) => {
synced += 1;
result
}
Ok(None) => {
synced += 1;
continue;
}
Err(e) => {
warn!("Failed to process file {fixed_path}: {e:?}");
continue;
}
};

assets.insert(fixed_path.clone(), result.asset_id);
if let Some(file_entry) = result.file_entry {
state.new_lockfile.entries.insert(fixed_path, file_entry);
}
}

Expand Down

0 comments on commit 555ad05

Please sign in to comment.