mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
* Update release.yml * Update ColorPickerUI.csproj * Update release.yml adding in .net6 sdk and moving stuff sooner * Update release.yml * Update release.yml * fixing test * Forcing vs17 and adding in .net 6 sdk * forcing pool * fixing issues in each pipeline * moving release .net up * fixing diff on agent version for nuget installer * Removing system.text.json.dll as included now * getting unit tests it looks like to work * updating everythign to .net 6 minus wxs for runtime * unit test still have * getting 6.0 stuff up and going. Terminal Unit tests have file max length issue .... * found i think the last .net 5 issue * looks like i wasn't aggressive enough with the 6.0 upgrade * Getting stuff .net 6 buildable again * tweaking with new stuff for installer * Update newly added merged projects to .net 6 * Fix HeatDirectory bug on VS 2022 * Settings still needs JSON dependency * Revert "getting 6.0 stuff up and going. Terminal Unit tests have file max length issue ...." This reverts commitb9cb4586dc. * Update sln version * supress obsolete warning, since this is not a new development * Partially Revert "Getting stuff .net 6 buildable again" This reverts commit42b4201c6b. * supress another obsolete warning, since this is not a new development * Reduce the unit test project name to avoid MAX PATH in CI * Upgrade project's toolset in the main solution * Some TODOs to review HttpClient usage * Upgrade project toolsets from other solutions * Install .net 6 instead of .net 5 * Fix issue when disabling PowerToys Run on .net framework 6 * Update docs for Visual Studio 2022 * PR comments: manually upgrade missing VS 2019 references * Discard no discard values to solve compiler warnings Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
140 lines
4.4 KiB
C#
140 lines
4.4 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.IO.Abstractions;
|
|
using System.Text.Json;
|
|
using Wox.Plugin.Logger;
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
{
|
|
/// <summary>
|
|
/// Serialize object using json format.
|
|
/// </summary>
|
|
public class JsonStorage<T>
|
|
{
|
|
private static readonly IFileSystem FileSystem = new FileSystem();
|
|
private static readonly IPath Path = FileSystem.Path;
|
|
private static readonly IFile File = FileSystem.File;
|
|
|
|
// use property initialization instead of DefaultValueAttribute
|
|
// easier and flexible for default value of object
|
|
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
|
|
{
|
|
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
|
|
IncludeFields = true,
|
|
PropertyNameCaseInsensitive = true,
|
|
WriteIndented = true,
|
|
};
|
|
|
|
private T _data;
|
|
|
|
// need a new directory name
|
|
public const string DirectoryName = "Settings";
|
|
public const string FileSuffix = ".json";
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
public string DirectoryPath { get; set; }
|
|
|
|
// This storage helper returns whether or not to delete the json storage items
|
|
private const int _jsonStorage = 1;
|
|
private StoragePowerToysVersionInfo _storageHelper;
|
|
|
|
public T Load()
|
|
{
|
|
_storageHelper = new StoragePowerToysVersionInfo(FilePath, _jsonStorage);
|
|
|
|
// Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible
|
|
if (_storageHelper.ClearCache)
|
|
{
|
|
if (File.Exists(FilePath))
|
|
{
|
|
File.Delete(FilePath);
|
|
Log.Info($"Deleting cached data at <{FilePath}>", GetType());
|
|
}
|
|
}
|
|
|
|
if (File.Exists(FilePath))
|
|
{
|
|
var serialized = File.ReadAllText(FilePath);
|
|
if (!string.IsNullOrWhiteSpace(serialized))
|
|
{
|
|
Deserialize(serialized);
|
|
}
|
|
else
|
|
{
|
|
LoadDefault();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LoadDefault();
|
|
}
|
|
|
|
return _data.NonNull();
|
|
}
|
|
|
|
private void Deserialize(string serialized)
|
|
{
|
|
try
|
|
{
|
|
_data = JsonSerializer.Deserialize<T>(serialized, _serializerOptions);
|
|
}
|
|
catch (JsonException e)
|
|
{
|
|
LoadDefault();
|
|
Log.Exception($"Deserialize error for json <{FilePath}>", e, GetType());
|
|
}
|
|
|
|
if (_data == null)
|
|
{
|
|
LoadDefault();
|
|
}
|
|
}
|
|
|
|
private void LoadDefault()
|
|
{
|
|
if (File.Exists(FilePath))
|
|
{
|
|
BackupOriginFile();
|
|
}
|
|
|
|
_data = JsonSerializer.Deserialize<T>("{}", _serializerOptions);
|
|
Save();
|
|
}
|
|
|
|
private void BackupOriginFile()
|
|
{
|
|
// Using InvariantCulture since this is internal
|
|
var timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff", CultureInfo.InvariantCulture);
|
|
var directory = Path.GetDirectoryName(FilePath).NonNull();
|
|
var originName = Path.GetFileNameWithoutExtension(FilePath);
|
|
var backupName = $"{originName}-{timestamp}{FileSuffix}";
|
|
var backupPath = Path.Combine(directory, backupName);
|
|
File.Copy(FilePath, backupPath, true);
|
|
|
|
// todo give user notification for the backup process
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
string serialized = JsonSerializer.Serialize(_data, _serializerOptions);
|
|
File.WriteAllText(FilePath, serialized);
|
|
_storageHelper.Close();
|
|
|
|
Log.Info($"Saving cached data at <{FilePath}>", GetType());
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
Log.Exception($"Error in saving data at <{FilePath}>", e, GetType());
|
|
}
|
|
}
|
|
}
|
|
}
|