Improve plugin updates to preserve version in filename

This commit is contained in:
Rbanh 2025-03-30 19:44:11 -04:00
parent 29f06b197e
commit 975258d70e

View File

@ -1,7 +1,7 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::fs; use std::fs;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use tauri::AppHandle; use regex;
use crate::models::repository::RepositorySource; use crate::models::repository::RepositorySource;
use crate::models::server::ServerInfo; use crate::models::server::ServerInfo;
@ -76,11 +76,23 @@ pub async fn replace_plugin(
// Backup the original file // Backup the original file
backup_plugin(current_file_path.clone()).await?; backup_plugin(current_file_path.clone()).await?;
// Determine new file name with version
let new_file_path = create_versioned_file_path(&current_file_path, &version)?;
// Replace the original file with the downloaded one // Replace the original file with the downloaded one
fs::rename(download_path, &current_file_path) // Use the new file path with version instead of the original path
fs::rename(download_path, &new_file_path)
.map_err(|e| format!("Failed to replace plugin: {}", e))?; .map_err(|e| format!("Failed to replace plugin: {}", e))?;
Ok(current_file_path) // Remove the old file if the path changed
if new_file_path != current_file_path {
match fs::remove_file(&current_file_path) {
Ok(_) => println!("Removed old plugin file: {}", current_file_path),
Err(e) => println!("Warning: Could not remove old plugin file {}: {}", current_file_path, e)
}
}
Ok(new_file_path)
} }
/// Create a temporary file path for plugin download /// Create a temporary file path for plugin download
@ -106,4 +118,33 @@ fn create_temp_download_path(current_file_path: &str) -> Result<PathBuf, String>
let temp_name = format!("{}.new.{}.{}", file_stem, now, file_ext); let temp_name = format!("{}.new.{}.{}", file_stem, now, file_ext);
Ok(parent.join(temp_name)) Ok(parent.join(temp_name))
}
/// Create a versioned file path for the updated plugin
fn create_versioned_file_path(current_file_path: &str, version: &str) -> Result<String, String> {
let path = Path::new(current_file_path);
let file_name = path.file_name()
.ok_or_else(|| "Invalid plugin file path".to_string())?
.to_str()
.ok_or_else(|| "Invalid file name encoding".to_string())?;
let parent = path.parent()
.unwrap_or_else(|| Path::new("."));
// Check if the file name already has a version pattern
// Common formats: PluginName-1.2.3.jar, PluginName-v1.2.3.jar, PluginName_1.2.3.jar
if let Some(captures) = regex::Regex::new(r"^([A-Za-z0-9_]+)[-_]v?[\d\.]+(\.[a-zA-Z0-9]+)$")
.unwrap()
.captures(file_name) {
// Get the plugin name without version
let base_name = captures.get(1).unwrap().as_str();
let extension = captures.get(2).unwrap().as_str();
// Create new filename with updated version
let new_file_name = format!("{}-{}{}", base_name, version, extension);
return Ok(parent.join(new_file_name).to_string_lossy().to_string());
}
// If no version pattern, just return the original path
Ok(current_file_path.to_string())
} }