Skip to content

Commit e0948db

Browse files
committed
simplify methods to import and export settings, to generate backup file contents
1 parent 3a021b0 commit e0948db

5 files changed

Lines changed: 38 additions & 59 deletions

File tree

src/UniGetUI.Core.Settings/SettingsEngine_ImportExport.cs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,37 @@ namespace UniGetUI.Core.SettingsEngine;
66

77
public partial class Settings
88
{
9-
public static void ExportToJSON(string path)
9+
public static void ExportToFile_JSON(string path)
1010
{
11-
Dictionary<string, string> settings = [];
12-
foreach (string entry in Directory.EnumerateFiles(CoreData.UniGetUIUserConfigurationDirectory))
13-
{
14-
if(new[] {"OperationHistory", "WinGetAlreadyUpgradedPackages.json", "TelemetryClientToken", "CurrentSessionToken"}.Contains(entry.Split("\\")[^1]))
15-
continue;
11+
File.WriteAllText(path, ExportToString_JSON());
12+
}
1613

17-
settings.Add(Path.GetFileName(entry), File.ReadAllText(entry));
14+
public static void ImportFromFile_JSON(string path)
15+
{
16+
if (Path.GetDirectoryName(path) == CoreData.UniGetUIUserConfigurationDirectory)
17+
{
18+
var tempLocation = Directory.CreateTempSubdirectory();
19+
var newPath = Path.Join(tempLocation.FullName, Path.GetFileName(path));
20+
File.Copy(path, newPath);
21+
path = newPath;
1822
}
19-
20-
File.WriteAllText(path, JsonSerializer.Serialize(settings, SerializationOptions));
23+
ImportFromString_JSON(path);
2124
}
2225

23-
public static async Task<string> ExportSettingsAsStringAsync()
26+
public static string ExportToString_JSON()
2427
{
2528
Dictionary<string, string> settings = [];
2629
foreach (string entry in Directory.EnumerateFiles(CoreData.UniGetUIUserConfigurationDirectory))
2730
{
2831
if (new[] { "OperationHistory", "WinGetAlreadyUpgradedPackages.json", "TelemetryClientToken", "CurrentSessionToken" }.Contains(Path.GetFileName(entry)))
2932
continue;
3033

31-
settings.Add(Path.GetFileName(entry), await File.ReadAllTextAsync(entry));
34+
settings.Add(Path.GetFileName(entry), File.ReadAllText(entry));
3235
}
3336
return JsonSerializer.Serialize(settings, SerializationOptions);
3437
}
3538

36-
public static async Task ImportSettingsFromStringAsync(string jsonContent)
39+
public static void ImportFromString_JSON(string jsonContent)
3740
{
3841
ResetSettings();
3942
Dictionary<string, string> settings = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent, SerializationOptions) ?? [];
@@ -42,30 +45,9 @@ public static async Task ImportSettingsFromStringAsync(string jsonContent)
4245
if (new[] { "OperationHistory", "WinGetAlreadyUpgradedPackages.json", "TelemetryClientToken", "CurrentSessionToken" }.Contains(entry.Key))
4346
continue;
4447

45-
await File.WriteAllTextAsync(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, entry.Key), entry.Value);
46-
}
47-
Logger.Info("Settings successfully imported from string content.");
48-
}
49-
50-
public static void ImportFromJSON(string path)
51-
{
52-
if (Path.GetDirectoryName(path) == CoreData.UniGetUIUserConfigurationDirectory)
53-
{
54-
var tempLocation = Directory.CreateTempSubdirectory();
55-
var newPath = Path.Join(tempLocation.FullName, Path.GetFileName(path));
56-
File.Copy(path, newPath);
57-
path = newPath;
58-
}
59-
60-
ResetSettings();
61-
Dictionary<string, string> settings = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(path), SerializationOptions) ?? [];
62-
foreach (KeyValuePair<string, string> entry in settings)
63-
{
64-
if(new[] {"OperationHistory", "WinGetAlreadyUpgradedPackages.json", "TelemetryClientToken", "CurrentSessionToken"}.Contains(entry.Key))
65-
continue;
66-
6748
File.WriteAllText(Path.Join(CoreData.UniGetUIUserConfigurationDirectory, entry.Key), entry.Value);
6849
}
50+
Logger.Info("Settings successfully imported from string content.");
6951
}
7052

7153
public static void ResetSettings()

src/UniGetUI/CLIHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static int ImportSettings()
6262

6363
try
6464
{
65-
Settings.ImportFromJSON(file);
65+
Settings.ImportFromFile_JSON(file);
6666
}
6767
catch (Exception ex)
6868
{
@@ -87,7 +87,7 @@ public static int ExportSettings()
8787

8888
try
8989
{
90-
Settings.ExportToJSON(file);
90+
Settings.ExportToFile_JSON(file);
9191
}
9292
catch (Exception ex)
9393
{

src/UniGetUI/Pages/SettingsPages/GeneralPages/Backup.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private async void RestoreSettingsFromGitHubButton_Click(object sender, EventArg
114114
var settingsContent = await _backupService.RestoreFileAsync("unigetui.settings.json");
115115
if (settingsContent != null)
116116
{
117-
await Settings.ImportSettingsFromStringAsync(settingsContent);
117+
await Task.Run(() => Settings.ImportFromString_JSON(settingsContent));
118118
DialogHelper.HideLoadingDialog();
119119
Logger.Info("Successfully restored settings from GitHub Gist.");
120120
DialogHelper.ShowDismissableBalloon(
@@ -180,8 +180,8 @@ private async void BackupToGitHubButton_Click(object sender, EventArgs e)
180180
BackupToGitHubButton.IsEnabled = false;
181181
DialogHelper.ShowLoadingDialog(CoreTools.Translate("Backing up settings and packages to GitHub Gist..."));
182182

183-
var settingsContent = await Settings.ExportSettingsAsStringAsync();
184-
var packagesContent = await InstalledPackagesPage.BackupPackages(true);
183+
var settingsContent = await Task.Run(Settings.ExportToString_JSON);
184+
var packagesContent = await InstalledPackagesPage.GenerateBackupContents();
185185

186186
var filesToBackup = new Dictionary<string, string>
187187
{
@@ -289,4 +289,4 @@ private async void DoBackup_Click(object sender, EventArgs e)
289289
DialogHelper.HideLoadingDialog();
290290
}
291291
}
292-
}
292+
}

src/UniGetUI/Pages/SettingsPages/GeneralPages/General.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private async void ImportSettings(object sender, EventArgs e)
6767
if (file != string.Empty)
6868
{
6969
DialogHelper.ShowLoadingDialog(CoreTools.Translate("Please wait..."));
70-
await Task.Run(() => Settings.ImportFromJSON(file));
70+
await Task.Run(() => Settings.ImportFromFile_JSON(file));
7171
DialogHelper.HideLoadingDialog();
7272
ShowRestartBanner(this, new());
7373
}
@@ -83,7 +83,7 @@ private async void ExportSettings(object sender, EventArgs e)
8383
if (file != string.Empty)
8484
{
8585
DialogHelper.ShowLoadingDialog(CoreTools.Translate("Please wait..."));
86-
await Task.Run(() => Settings.ExportToJSON(file));
86+
await Task.Run(() => Settings.ExportToFile_JSON(file));
8787
DialogHelper.HideLoadingDialog();
8888
CoreTools.ShowFileOnExplorer(file);
8989
}

src/UniGetUI/Pages/SoftwarePages/InstalledPackagesPage.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -362,25 +362,24 @@ private async void ExportSelection_Click(object sender, RoutedEventArgs e)
362362

363363
}
364364

365-
public static async Task<string?> BackupPackages(bool returnContents = false)
365+
public static Task<string> GenerateBackupContents()
366366
{
367-
368-
try
367+
Logger.Debug("Starting package backup");
368+
List<IPackage> packagesToExport = [];
369+
foreach (IPackage package in PEInterface.InstalledPackagesLoader.Packages)
369370
{
370-
Logger.Debug("Starting package backup");
371-
List<IPackage> packagesToExport = [];
372-
foreach (IPackage package in PEInterface.InstalledPackagesLoader.Packages)
373-
{
374-
packagesToExport.Add(package);
375-
}
371+
packagesToExport.Add(package);
372+
}
376373

377-
string BackupContents = await PackageBundlesPage.CreateBundle(packagesToExport.ToArray(), BundleFormatType.UBUNDLE);
374+
return PackageBundlesPage.CreateBundle(packagesToExport.ToArray(), BundleFormatType.UBUNDLE);
375+
}
378376

379-
if(returnContents)
380-
{
381-
return BackupContents;
382-
}
383377

378+
public static async Task BackupPackages()
379+
{
380+
try
381+
{
382+
string backupContents = await GenerateBackupContents();
384383
string dirName = Settings.GetValue(Settings.K.ChangeBackupOutputDirectory);
385384
if (dirName == "")
386385
{
@@ -406,16 +405,14 @@ private async void ExportSelection_Click(object sender, RoutedEventArgs e)
406405
fileName += ".ubundle";
407406

408407
string filePath = Path.Combine(dirName, fileName);
409-
await File.WriteAllTextAsync(filePath, BackupContents);
408+
await File.WriteAllTextAsync(filePath, backupContents);
410409
HasDoneBackup = true;
411410
Logger.ImportantInfo("Backup saved to " + filePath);
412-
return null;
413411
}
414412
catch (Exception ex)
415413
{
416414
Logger.Error("An error occurred while performing a backup");
417415
Logger.Error(ex);
418-
return null;
419416
}
420417
}
421418

0 commit comments

Comments
 (0)