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.
This commit is contained in:
Jack251970 2026-01-24 16:03:26 +08:00
parent 9070ff4c7b
commit d16e43de8a
3 changed files with 64 additions and 64 deletions

View file

@ -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).
/// </summary>
private string ResolvedPluginsSettingsFilePath => Constant.ResolveAbsolutePath(PluginsSettingsFilePath);
private string ResolvedPluginsSettingsFilePath => DataLocation.ResolveAbsolutePath(PluginsSettingsFilePath);
internal IEnumerable<PluginPair> 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
{

View file

@ -56,66 +56,5 @@ namespace Flow.Launcher.Infrastructure
public const string Docs = "https://flowlauncher.com/docs";
public const string SystemLanguageCode = "system";
/// <summary>
/// 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.
/// </summary>
/// <param name="path">The path to resolve</param>
/// <returns>An absolute path</returns>
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));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="absolutePath">The absolute path to convert</param>
/// <returns>A relative path if the path is within ProgramDirectory, otherwise the original absolute path</returns>
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;
}
}
}

View file

@ -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);
/// <summary>
/// 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.
/// </summary>
/// <param name="path">The path to resolve</param>
/// <returns>An absolute path</returns>
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));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="absolutePath">The absolute path to convert</param>
/// <returns>A relative path if the path is within ProgramDirectory, otherwise the original absolute path</returns>
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;
}
}
}