-
Notifications
You must be signed in to change notification settings - Fork 11
Add config file support and move previews setting from manifest #281
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/add-config-file-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95a824e
Initial plan
Copilot d5d2b6e
Add config file support with platform-specific locations
Copilot b13f5ff
Fix integration tests for config file support
Copilot 9dfc5fe
Remove automatic config file migration
Copilot 8f70456
Use ApplicationData instead of LocalApplicationData for config files
Copilot 8c42896
Refactor DnvmConfigFile to use instance methods
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Serde; | ||
| using Serde.Json; | ||
| using Zio; | ||
| using Zio.FileSystems; | ||
|
|
||
| namespace Dnvm; | ||
|
|
||
| /// <summary> | ||
| /// Represents the dnvm configuration file. | ||
| /// </summary> | ||
| [GenerateSerde] | ||
| public sealed partial record DnvmConfig | ||
| { | ||
| public static readonly DnvmConfig Default = new(); | ||
|
|
||
| /// <summary> | ||
| /// Whether to enable dnvm preview releases in the update command. | ||
| /// </summary> | ||
| public bool PreviewsEnabled { get; init; } = false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Provides access to the dnvm configuration file. | ||
| /// </summary> | ||
| public sealed class DnvmConfigFile | ||
| { | ||
| private const string ConfigFileName = "dnvmConfig.json"; | ||
|
|
||
| // Allow tests to override the config directory | ||
| public static string? TestConfigDirectory { get; set; } | ||
|
|
||
| private readonly string _configDirectory; | ||
|
|
||
| public DnvmConfigFile() : this(GetConfigDirectory()) | ||
| { | ||
| } | ||
|
|
||
| internal DnvmConfigFile(string configDirectory) | ||
| { | ||
| _configDirectory = configDirectory; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the platform-specific config directory path. | ||
| /// - Linux: ~/.config/dnvm/ (XDG_CONFIG_HOME/dnvm) | ||
| /// - macOS: ~/.config/dnvm/ | ||
| /// - Windows: %APPDATA%/dnvm/ | ||
| /// </summary> | ||
| private static string GetConfigDirectory() | ||
| { | ||
| if (TestConfigDirectory is not null) | ||
| { | ||
| return TestConfigDirectory; | ||
| } | ||
|
|
||
| // Allow tests to override config directory via environment variable | ||
| var testOverride = Environment.GetEnvironmentVariable("DNVM_TEST_CONFIG_DIR"); | ||
| if (!string.IsNullOrWhiteSpace(testOverride)) | ||
| { | ||
| return testOverride; | ||
| } | ||
|
|
||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| { | ||
| // Use XDG_CONFIG_HOME on Linux, defaulting to ~/.config | ||
| var xdgConfigHome = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME"); | ||
| var configBase = string.IsNullOrWhiteSpace(xdgConfigHome) | ||
| ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config") | ||
| : xdgConfigHome; | ||
| return Path.Combine(configBase, "dnvm"); | ||
| } | ||
| else | ||
| { | ||
| // On macOS and Windows, use ApplicationData for roaming config files | ||
| // This is ~/.config on macOS and %APPDATA% on Windows | ||
| return Path.Combine( | ||
| Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), | ||
| "dnvm"); | ||
| } | ||
| } | ||
|
|
||
| private IFileSystem GetConfigFileSystem() | ||
| { | ||
| Directory.CreateDirectory(_configDirectory); | ||
| return new SubFileSystem( | ||
| DnvmEnv.PhysicalFs, | ||
| DnvmEnv.PhysicalFs.ConvertPathFromInternal(_configDirectory)); | ||
| } | ||
|
|
||
| private static UPath ConfigPath => UPath.Root / ConfigFileName; | ||
|
|
||
| /// <summary> | ||
| /// Reads the config file from the platform-specific config directory. | ||
| /// Returns the default config if the file does not exist. | ||
| /// </summary> | ||
| public DnvmConfig Read() | ||
| { | ||
| try | ||
| { | ||
| var fs = GetConfigFileSystem(); | ||
| if (!fs.FileExists(ConfigPath)) | ||
| { | ||
| return DnvmConfig.Default; | ||
| } | ||
|
|
||
| var text = fs.ReadAllText(ConfigPath); | ||
| return JsonSerializer.Deserialize<DnvmConfig>(text); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // If there's any error reading or parsing the config, return default | ||
| return DnvmConfig.Default; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the config file to the platform-specific config directory. | ||
| /// </summary> | ||
| public void Write(DnvmConfig config) | ||
| { | ||
| var fs = GetConfigFileSystem(); | ||
| var tempFileName = $"{ConfigFileName}.{Path.GetRandomFileName()}.tmp"; | ||
| var tempPath = UPath.Root / tempFileName; | ||
|
|
||
| var text = JsonSerializer.Serialize(config); | ||
|
|
||
| // Write to temporary file first | ||
| fs.WriteAllText(tempPath, text, Encoding.UTF8); | ||
|
|
||
| // Create backup of existing config if it exists | ||
| if (fs.FileExists(ConfigPath)) | ||
| { | ||
| var backupPath = UPath.Root / $"{ConfigFileName}.backup"; | ||
| try | ||
| { | ||
| // Should not throw if the file doesn't exist | ||
| fs.DeleteFile(backupPath); | ||
| fs.MoveFile(ConfigPath, backupPath); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // Best effort cleanup - ignore if we can't delete the backup file | ||
| } | ||
| } | ||
|
|
||
| // Atomic rename operation | ||
| fs.MoveFile(tempPath, ConfigPath); | ||
|
|
||
| // Clean up temporary file | ||
| try | ||
| { | ||
| fs.DeleteFile(tempPath); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // Best effort cleanup - ignore if we can't delete the temp file | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.