Refactor file renaming logic in JobThreadContext and improve path handling

- Simplified the logic for resolving the actual file to rename, consolidating the handling of directory and file paths.
- Enhanced the script renaming process to ensure correct naming conventions based on class type, improving clarity and consistency.
- Updated logging to provide better insights during class name changes and file renaming operations, aiding in debugging.
- Made minor formatting adjustments to improve code readability in the `suppressed_paths` method and API service file handling.
This commit is contained in:
Joe 2026-02-05 18:52:10 -06:00
parent f38c96e12b
commit e6944e3132
3 changed files with 62 additions and 60 deletions

View file

@ -567,74 +567,73 @@ impl JobThreadContext {
// For directory-format scripts, the path is the directory
// (e.g., src/MyModule/), not the init file inside. We must
// find and rename the init file, not the directory.
let (actual_file, file_parent) = if path.is_dir() {
// Find the init file inside the directory
let init_file = Self::find_init_file(path);
match init_file {
Some(f) => (f.clone(), path.to_path_buf()),
None => {
log::warn!(
"Cannot change ClassName for directory {} \
— no init file found inside",
path.display()
);
continue;
}
}
// Resolve the actual file to rename. For directories,
// find the init file inside; for files, use directly.
let init_result = if path.is_dir() {
Self::find_init_file(path)
.map(|f| (f.clone(), path.to_path_buf()))
} else {
(
Some((
path.clone(),
path.parent()
.unwrap_or(path.as_path())
.to_path_buf(),
)
))
};
let name = instance.name();
let new_suffix = match new_class.as_str() {
"ModuleScript" => "",
"Script" => ".server",
"LocalScript" => ".local",
_ => "",
};
let is_init = actual_file
.file_name()
.and_then(|f| f.to_str())
.map(|f| f.starts_with("init."))
.unwrap_or(false);
if let Some((actual_file, file_parent)) = init_result {
let name = instance.name();
let new_suffix = match new_class.as_str() {
"ModuleScript" => "",
"Script" => ".server",
"LocalScript" => ".local",
_ => "",
};
let is_init = actual_file
.file_name()
.and_then(|f| f.to_str())
.map(|f| f.starts_with("init."))
.unwrap_or(false);
let new_file_name = if is_init {
if new_suffix.is_empty() {
"init.luau".to_string()
let new_file_name = if is_init {
if new_suffix.is_empty() {
"init.luau".to_string()
} else {
format!("init{}.luau", new_suffix)
}
} else if new_suffix.is_empty() {
format!("{}.luau", name)
} else {
format!("init{}.luau", new_suffix)
}
} else if new_suffix.is_empty() {
format!("{}.luau", name)
} else {
format!("{}{}.luau", name, new_suffix)
};
format!("{}{}.luau", name, new_suffix)
};
let new_path = file_parent.join(&new_file_name);
if new_path != actual_file {
log::info!(
"Two-way sync: Changing class {} -> {}, \
renaming {} -> {}",
old_class,
new_class,
actual_file.display(),
new_path.display()
);
if let Err(err) =
fs::rename(&actual_file, &new_path)
{
log::error!(
"Failed to rename {:?} to {:?}: {}",
actual_file,
new_path,
err
let new_path = file_parent.join(&new_file_name);
if new_path != actual_file {
log::info!(
"Two-way sync: Changing class {} -> {}, \
renaming {} -> {}",
old_class,
new_class,
actual_file.display(),
new_path.display()
);
if let Err(err) =
fs::rename(&actual_file, &new_path)
{
log::error!(
"Failed to rename {:?} to {:?}: {}",
actual_file,
new_path,
err
);
}
}
} else {
log::warn!(
"Cannot change ClassName for directory {} \
— no init file found inside",
path.display()
);
}
} else if old_is_script != new_is_script {
log::warn!(

View file

@ -178,7 +178,9 @@ impl ServeSession {
/// Returns a handle to the suppressed paths map, used to avoid
/// file watcher echo when the API writes files to disk.
#[allow(dead_code)]
pub fn suppressed_paths(&self) -> Arc<Mutex<std::collections::HashMap<std::path::PathBuf, usize>>> {
pub fn suppressed_paths(
&self,
) -> Arc<Mutex<std::collections::HashMap<std::path::PathBuf, usize>>> {
Arc::clone(&self.suppressed_paths)
}

View file

@ -180,7 +180,9 @@ impl ApiService {
} else if let Some(parent) = path.parent() {
if let Ok(canonical_parent) = std::fs::canonicalize(parent) {
if let Some(file_name) = path.file_name() {
*suppressed.entry(canonical_parent.join(file_name)).or_insert(0) += 1;
*suppressed
.entry(canonical_parent.join(file_name))
.or_insert(0) += 1;
}
}
}
@ -1545,8 +1547,7 @@ impl ApiService {
if !has_children && !has_metadata {
let gitkeep = dir_path.join(".gitkeep");
self.suppress_path(&gitkeep);
fs::write(gitkeep, b"")
.with_context(|| "Failed to write .gitkeep")?;
fs::write(gitkeep, b"").with_context(|| "Failed to write .gitkeep")?;
}
log::info!(