From d16e43de8a0717429085015008542e53f7e4307d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 24 Jan 2026 16:03:26 +0800 Subject: [PATCH] Refactor path utilities to DataLocation from Constant Move ResolveAbsolutePath and ConvertToRelativePathIfPossible from Constant to DataLocation for better organization. Update all references accordingly; implementations remain unchanged. This improves code clarity around file path management. --- .../Environments/AbstractPluginEnvironment.cs | 6 +- Flow.Launcher.Infrastructure/Constant.cs | 61 ------------------- .../UserSettings/DataLocation.cs | 61 +++++++++++++++++++ 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/Flow.Launcher.Core/ExternalPlugins/Environments/AbstractPluginEnvironment.cs b/Flow.Launcher.Core/ExternalPlugins/Environments/AbstractPluginEnvironment.cs index 3a9732aa3..e08a5815e 100644 --- a/Flow.Launcher.Core/ExternalPlugins/Environments/AbstractPluginEnvironment.cs +++ b/Flow.Launcher.Core/ExternalPlugins/Environments/AbstractPluginEnvironment.cs @@ -45,7 +45,7 @@ namespace Flow.Launcher.Core.ExternalPlugins.Environments /// Resolves the configured plugin settings file path to an absolute path. /// Supports both absolute paths and relative paths (relative to ProgramDirectory). /// - private string ResolvedPluginsSettingsFilePath => Constant.ResolveAbsolutePath(PluginsSettingsFilePath); + private string ResolvedPluginsSettingsFilePath => DataLocation.ResolveAbsolutePath(PluginsSettingsFilePath); internal IEnumerable Setup() { @@ -75,7 +75,7 @@ namespace Flow.Launcher.Core.ExternalPlugins.Environments if (!string.IsNullOrEmpty(selectedFile)) { // Convert to relative path if within ProgramDirectory for portability - PluginsSettingsFilePath = Constant.ConvertToRelativePathIfPossible(selectedFile); + PluginsSettingsFilePath = DataLocation.ConvertToRelativePathIfPossible(selectedFile); } // Nothing selected because user pressed cancel from the file dialog window else @@ -100,7 +100,7 @@ namespace Flow.Launcher.Core.ExternalPlugins.Environments if (!string.IsNullOrEmpty(selectedFile)) { // Convert to relative path if within ProgramDirectory for portability - PluginsSettingsFilePath = Constant.ConvertToRelativePathIfPossible(selectedFile); + PluginsSettingsFilePath = DataLocation.ConvertToRelativePathIfPossible(selectedFile); } else { diff --git a/Flow.Launcher.Infrastructure/Constant.cs b/Flow.Launcher.Infrastructure/Constant.cs index fd1538646..13da9f79f 100644 --- a/Flow.Launcher.Infrastructure/Constant.cs +++ b/Flow.Launcher.Infrastructure/Constant.cs @@ -56,66 +56,5 @@ namespace Flow.Launcher.Infrastructure public const string Docs = "https://flowlauncher.com/docs"; public const string SystemLanguageCode = "system"; - - /// - /// Resolves a path that may be relative to an absolute path. - /// If the path is already absolute, returns it as-is. - /// If the path is relative (starts with . or doesn't contain a drive), resolves it relative to ProgramDirectory. - /// - /// The path to resolve - /// An absolute path - public static string ResolveAbsolutePath(string path) - { - if (string.IsNullOrEmpty(path)) - return path; - - // If already absolute, return as-is - if (Path.IsPathRooted(path)) - return path; - - // Resolve relative to ProgramDirectory - return Path.GetFullPath(Path.Combine(ProgramDirectory, path)); - } - - /// - /// Converts an absolute path to a relative path if it's within ProgramDirectory. - /// This enables portability by storing paths relative to the program directory when possible. - /// - /// The absolute path to convert - /// A relative path if the path is within ProgramDirectory, otherwise the original absolute path - public static string ConvertToRelativePathIfPossible(string absolutePath) - { - if (string.IsNullOrEmpty(absolutePath)) - return absolutePath; - - if (!Path.IsPathRooted(absolutePath)) - return absolutePath; - - try - { - // Get the full absolute paths for comparison - var fullAbsolutePath = Path.GetFullPath(absolutePath); - var fullProgramDir = Path.GetFullPath(ProgramDirectory); - - // Check if the absolute path is within ProgramDirectory - if (fullAbsolutePath.StartsWith(fullProgramDir, StringComparison.OrdinalIgnoreCase)) - { - // Convert to relative path - var relativePath = Path.GetRelativePath(fullProgramDir, fullAbsolutePath); - - // Prefix with .\ for clarity - if (!relativePath.StartsWith(".")) - relativePath = ".\\" + relativePath; - - return relativePath; - } - } - catch - { - // If conversion fails, return the original path - } - - return absolutePath; - } } } diff --git a/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs b/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs index 82f83b11a..4588a9e6a 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs @@ -42,5 +42,66 @@ namespace Flow.Launcher.Infrastructure.UserSettings public const string PluginEnvironments = "Environments"; public const string PluginDeleteFile = "NeedDelete.txt"; public static readonly string PluginEnvironmentsPath = Path.Combine(DataDirectory(), PluginEnvironments); + + /// + /// Resolves a path that may be relative to an absolute path. + /// If the path is already absolute, returns it as-is. + /// If the path is relative (starts with . or doesn't contain a drive), resolves it relative to ProgramDirectory. + /// + /// The path to resolve + /// An absolute path + public static string ResolveAbsolutePath(string path) + { + if (string.IsNullOrEmpty(path)) + return path; + + // If already absolute, return as-is + if (Path.IsPathRooted(path)) + return path; + + // Resolve relative to ProgramDirectory + return Path.GetFullPath(Path.Combine(Constant.ProgramDirectory, path)); + } + + /// + /// Converts an absolute path to a relative path if it's within ProgramDirectory. + /// This enables portability by storing paths relative to the program directory when possible. + /// + /// The absolute path to convert + /// A relative path if the path is within ProgramDirectory, otherwise the original absolute path + public static string ConvertToRelativePathIfPossible(string absolutePath) + { + if (string.IsNullOrEmpty(absolutePath)) + return absolutePath; + + if (!Path.IsPathRooted(absolutePath)) + return absolutePath; + + try + { + // Get the full absolute paths for comparison + var fullAbsolutePath = Path.GetFullPath(absolutePath); + var fullProgramDir = Path.GetFullPath(Constant.ProgramDirectory); + + // Check if the absolute path is within ProgramDirectory + if (fullAbsolutePath.StartsWith(fullProgramDir, StringComparison.OrdinalIgnoreCase)) + { + // Convert to relative path + var relativePath = Path.GetRelativePath(fullProgramDir, fullAbsolutePath); + + // Prefix with .\ for clarity + if (!relativePath.StartsWith('.')) + relativePath = ".\\" + relativePath; + + return relativePath; + } + } + catch + { + // If conversion fails, return the original path + } + + return absolutePath; + } } }