mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 05:51:39 +00:00
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:
parent
f38c96e12b
commit
e6944e3132
3 changed files with 62 additions and 60 deletions
|
|
@ -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!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue