Refactor file path handling in JobThreadContext and ApiService

- Streamlined the logic for encoding new file names and paths, enhancing consistency in file operations across both JobThreadContext and ApiService.
- Improved readability by consolidating multiple lines of code into single statements, particularly in the handling of metadata file paths and script suffixes.
- Enhanced error logging for snapshot retrieval in compute_and_apply_changes, ensuring better debugging capabilities during file operations.
This commit is contained in:
Joe 2026-02-05 20:35:34 -06:00
parent e33cf7ae77
commit 9912d86896
2 changed files with 17 additions and 31 deletions

View file

@ -520,8 +520,7 @@ impl JobThreadContext {
.file_name() .file_name()
.and_then(|f| f.to_str()) .and_then(|f| f.to_str())
.unwrap_or(""); .unwrap_or("");
let encoded_new_name = let encoded_new_name = encode_path_name(new_name);
encode_path_name(new_name);
let new_dir_path = let new_dir_path =
grandparent.join(&encoded_new_name); grandparent.join(&encoded_new_name);
@ -549,12 +548,11 @@ impl JobThreadContext {
dir_name dir_name
)); ));
if old_meta.exists() { if old_meta.exists() {
let new_meta = grandparent.join( let new_meta =
format!( grandparent.join(format!(
"{}.meta.json5", "{}.meta.json5",
encoded_new_name encoded_new_name
), ));
);
let _ = let _ =
fs::rename(&old_meta, &new_meta); fs::rename(&old_meta, &new_meta);
} }
@ -589,19 +587,13 @@ impl JobThreadContext {
&stem[..stem.len() - script_suffix.len()] &stem[..stem.len() - script_suffix.len()]
}; };
let encoded_new_name = let encoded_new_name = encode_path_name(new_name);
encode_path_name(new_name);
let new_file_name = if extension.is_empty() { let new_file_name = if extension.is_empty() {
format!( format!("{}{}", encoded_new_name, script_suffix)
"{}{}",
encoded_new_name, script_suffix
)
} else { } else {
format!( format!(
"{}{}.{}", "{}{}.{}",
encoded_new_name, encoded_new_name, script_suffix, extension
script_suffix,
extension
) )
}; };
let new_path = parent.join(&new_file_name); let new_path = parent.join(&new_file_name);
@ -621,10 +613,8 @@ impl JobThreadContext {
); );
} else { } else {
overridden_source_path = Some(new_path.clone()); overridden_source_path = Some(new_path.clone());
let old_meta = parent.join(format!( let old_meta = parent
"{}.meta.json5", .join(format!("{}.meta.json5", old_base));
old_base
));
if old_meta.exists() { if old_meta.exists() {
let new_meta = parent.join(format!( let new_meta = parent.join(format!(
"{}.meta.json5", "{}.meta.json5",
@ -897,14 +887,13 @@ fn compute_and_apply_changes(tree: &mut RojoTree, vfs: &Vfs, id: Ref) -> Option<
snapshot_path.display() snapshot_path.display()
); );
let snapshot = let snapshot = match snapshot_from_vfs(&metadata.context, vfs, snapshot_path) {
match snapshot_from_vfs(&metadata.context, vfs, snapshot_path) { Ok(snapshot) => snapshot,
Ok(snapshot) => snapshot, Err(err) => {
Err(err) => { log::error!("Snapshot error: {:?}", err);
log::error!("Snapshot error: {:?}", err); return None;
return None; }
} };
};
let patch_set = compute_patch_set(snapshot, tree, id); let patch_set = compute_patch_set(snapshot, tree, id);
let applied = apply_patch_set(tree, patch_set); let applied = apply_patch_set(tree, patch_set);

View file

@ -351,10 +351,7 @@ impl ApiService {
// Strip known script suffixes (.server, .client, etc.) // Strip known script suffixes (.server, .client, etc.)
// rather than splitting on dots, so that names containing // rather than splitting on dots, so that names containing
// dots (e.g. "Config.Client.server.luau") resolve correctly. // dots (e.g. "Config.Client.server.luau") resolve correctly.
if let Some(file_stem) = path if let Some(file_stem) = path.file_stem().and_then(|s| s.to_str()) {
.file_stem()
.and_then(|s| s.to_str())
{
let base_name = file_stem let base_name = file_stem
.strip_suffix(".server") .strip_suffix(".server")
.or_else(|| file_stem.strip_suffix(".client")) .or_else(|| file_stem.strip_suffix(".client"))