-
Notifications
You must be signed in to change notification settings - Fork 2
WIP:EIM-277 rename activation script in case there is already one present #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
WIP:EIM-277 rename activation script in case there is already one present #225
Conversation
Summary of ChangesHello @Hahihula, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a potential issue where installing the same IDF version multiple times could lead to the overwriting of previously generated activation scripts and PowerShell profiles. It implements a safeguard that checks for the existence of these files and, if found, renames them by appending a Universally Unique Identifier (UUID) to their names. This ensures that all generated scripts are preserved, providing a more stable and predictable installation experience without data loss. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request modifies the creation of activation scripts for both shell and PowerShell to avoid overwriting existing files. It introduces a check to see if a script already exists, and if so, generates a new unique filename using a UUID. My review focuses on improving code maintainability by addressing code duplication and enhancing the error handling to prevent potential file overwrites in edge cases. I've suggested extracting the filename generation logic into a shared helper function and propagating errors instead of falling back to a default that could lead to data loss.
| filename = match filename.try_exists() { | ||
| Ok(true) => { | ||
|
|
||
| let mut new_filename = filename.clone(); | ||
| new_filename.set_file_name(format!("activate_idf_{}_{}.sh", idf_version, uuid::Uuid::new_v4())); | ||
| info!( | ||
| "Activation script already exists at {}, changing name to {}.", | ||
| filename.display(), | ||
| new_filename.display() | ||
| ); | ||
| new_filename | ||
| } | ||
| Ok(false) => { filename} | ||
| Err(e) => { | ||
| error!("Failed to check if file exists: {}", e); | ||
| filename | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block has two issues:
- Code Duplication: The logic for handling existing files is duplicated in
create_powershell_profileon lines 356-373. This should be extracted into a helper function to improve maintainability. - Incorrect Error Handling: In case of an error when checking if the file exists, the code falls back to using the original
filename. This could lead to overwriting an existing file, which contradicts the goal of the PR ("we will now not rewrite it"). It would be safer to propagate the error.
I suggest creating a helper function that addresses both points. You could add this function within src-tauri/src/lib/mod.rs:
fn ensure_unique_filename(path: PathBuf, file_type: &str) -> Result<PathBuf, std::io::Error> {
match path.try_exists() {
Ok(true) => {
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or_default();
let extension = path.extension().and_then(|s| s.to_str()).unwrap_or_default();
let mut new_path = path.clone();
let new_name = if extension.is_empty() {
format!("{}_{}", stem, uuid::Uuid::new_v4())
} else {
format!("{}_{}.{}", stem, uuid::Uuid::new_v4(), extension)
};
new_path.set_file_name(new_name);
info!(
"{} already exists at {}, changing name to {}.",
file_type,
path.display(),
new_path.display()
);
Ok(new_path)
}
Ok(false) => Ok(path),
Err(e) => {
error!("Failed to check if file exists: {}. Aborting.", e);
Err(e)
}
}
}With this helper function, you can replace this entire match block with:
filename = ensure_unique_filename(filename, "Activation script").map_err(|e| e.to_string())?;
| filename = match filename.try_exists() { | ||
| Ok(true) => { | ||
|
|
||
| let mut new_filename = filename.clone(); | ||
| new_filename.set_file_name(format!("Microsoft.{}.PowerShell_profile_{}.ps1", idf_version, uuid::Uuid::new_v4())); | ||
| info!( | ||
| "Profile already exists at {}, changing name to {}.", | ||
| filename.display(), | ||
| new_filename.display() | ||
| ); | ||
| new_filename | ||
| } | ||
| Ok(false) => { filename} | ||
| Err(e) => { | ||
| error!("Failed to check if file exists: {}", e); | ||
| filename | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block has the same issues as the one in create_activation_shell_script (lines 142-159): code duplication and incorrect error handling.
As suggested in the other comment, this logic should be extracted to a shared helper function. You can then replace this block with a call to the suggested ensure_unique_filename function:
filename = ensure_unique_filename(filename, "Profile")?;
In case there is already activation script in place (user installs same IDF version to different location) we will now not rewrite it.
Do not merge. This needs to be reworked as this way it will broke the named workflow.