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
|
// For directory-format scripts, the path is the directory
|
||||||
// (e.g., src/MyModule/), not the init file inside. We must
|
// (e.g., src/MyModule/), not the init file inside. We must
|
||||||
// find and rename the init file, not the directory.
|
// find and rename the init file, not the directory.
|
||||||
let (actual_file, file_parent) = if path.is_dir() {
|
// Resolve the actual file to rename. For directories,
|
||||||
// Find the init file inside the directory
|
// find the init file inside; for files, use directly.
|
||||||
let init_file = Self::find_init_file(path);
|
let init_result = if path.is_dir() {
|
||||||
match init_file {
|
Self::find_init_file(path)
|
||||||
Some(f) => (f.clone(), path.to_path_buf()),
|
.map(|f| (f.clone(), path.to_path_buf()))
|
||||||
None => {
|
|
||||||
log::warn!(
|
|
||||||
"Cannot change ClassName for directory {} \
|
|
||||||
— no init file found inside",
|
|
||||||
path.display()
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
(
|
Some((
|
||||||
path.clone(),
|
path.clone(),
|
||||||
path.parent()
|
path.parent()
|
||||||
.unwrap_or(path.as_path())
|
.unwrap_or(path.as_path())
|
||||||
.to_path_buf(),
|
.to_path_buf(),
|
||||||
)
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
let name = instance.name();
|
if let Some((actual_file, file_parent)) = init_result {
|
||||||
let new_suffix = match new_class.as_str() {
|
let name = instance.name();
|
||||||
"ModuleScript" => "",
|
let new_suffix = match new_class.as_str() {
|
||||||
"Script" => ".server",
|
"ModuleScript" => "",
|
||||||
"LocalScript" => ".local",
|
"Script" => ".server",
|
||||||
_ => "",
|
"LocalScript" => ".local",
|
||||||
};
|
_ => "",
|
||||||
let is_init = actual_file
|
};
|
||||||
.file_name()
|
let is_init = actual_file
|
||||||
.and_then(|f| f.to_str())
|
.file_name()
|
||||||
.map(|f| f.starts_with("init."))
|
.and_then(|f| f.to_str())
|
||||||
.unwrap_or(false);
|
.map(|f| f.starts_with("init."))
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
let new_file_name = if is_init {
|
let new_file_name = if is_init {
|
||||||
if new_suffix.is_empty() {
|
if new_suffix.is_empty() {
|
||||||
"init.luau".to_string()
|
"init.luau".to_string()
|
||||||
|
} else {
|
||||||
|
format!("init{}.luau", new_suffix)
|
||||||
|
}
|
||||||
|
} else if new_suffix.is_empty() {
|
||||||
|
format!("{}.luau", name)
|
||||||
} else {
|
} else {
|
||||||
format!("init{}.luau", new_suffix)
|
format!("{}{}.luau", name, new_suffix)
|
||||||
}
|
};
|
||||||
} else if new_suffix.is_empty() {
|
|
||||||
format!("{}.luau", name)
|
|
||||||
} else {
|
|
||||||
format!("{}{}.luau", name, new_suffix)
|
|
||||||
};
|
|
||||||
|
|
||||||
let new_path = file_parent.join(&new_file_name);
|
let new_path = file_parent.join(&new_file_name);
|
||||||
if new_path != actual_file {
|
if new_path != actual_file {
|
||||||
log::info!(
|
log::info!(
|
||||||
"Two-way sync: Changing class {} -> {}, \
|
"Two-way sync: Changing class {} -> {}, \
|
||||||
renaming {} -> {}",
|
renaming {} -> {}",
|
||||||
old_class,
|
old_class,
|
||||||
new_class,
|
new_class,
|
||||||
actual_file.display(),
|
actual_file.display(),
|
||||||
new_path.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
|
|
||||||
);
|
);
|
||||||
|
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 {
|
} else if old_is_script != new_is_script {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,9 @@ impl ServeSession {
|
||||||
/// Returns a handle to the suppressed paths map, used to avoid
|
/// Returns a handle to the suppressed paths map, used to avoid
|
||||||
/// file watcher echo when the API writes files to disk.
|
/// file watcher echo when the API writes files to disk.
|
||||||
#[allow(dead_code)]
|
#[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)
|
Arc::clone(&self.suppressed_paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,9 @@ impl ApiService {
|
||||||
} else if let Some(parent) = path.parent() {
|
} else if let Some(parent) = path.parent() {
|
||||||
if let Ok(canonical_parent) = std::fs::canonicalize(parent) {
|
if let Ok(canonical_parent) = std::fs::canonicalize(parent) {
|
||||||
if let Some(file_name) = path.file_name() {
|
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 {
|
if !has_children && !has_metadata {
|
||||||
let gitkeep = dir_path.join(".gitkeep");
|
let gitkeep = dir_path.join(".gitkeep");
|
||||||
self.suppress_path(&gitkeep);
|
self.suppress_path(&gitkeep);
|
||||||
fs::write(gitkeep, b"")
|
fs::write(gitkeep, b"").with_context(|| "Failed to write .gitkeep")?;
|
||||||
.with_context(|| "Failed to write .gitkeep")?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info!(
|
log::info!(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue