diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Contracts/IFileService.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Contracts/IFileService.cs deleted file mode 100644 index b9348eb52046..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Contracts/IFileService.cs +++ /dev/null @@ -1,14 +0,0 @@ -// 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. - -namespace Microsoft.CmdPal.Common.Contracts; - -public interface IFileService -{ - T Read(string folderPath, string fileName); - - void Save(string folderPath, string fileName, T content); - - void Delete(string folderPath, string fileName); -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Contracts/ILocalSettingsService.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Contracts/ILocalSettingsService.cs deleted file mode 100644 index 2350050e3e0b..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Contracts/ILocalSettingsService.cs +++ /dev/null @@ -1,16 +0,0 @@ -// 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.Threading.Tasks; - -namespace Microsoft.CmdPal.Common.Contracts; - -public interface ILocalSettingsService -{ - Task HasSettingAsync(string key); - - Task ReadSettingAsync(string key); - - Task SaveSettingAsync(string key, T value); -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Extensions/ApplicationExtensions.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Extensions/ApplicationExtensions.cs deleted file mode 100644 index a975083c7cc8..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Extensions/ApplicationExtensions.cs +++ /dev/null @@ -1,40 +0,0 @@ -// 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 Microsoft.CmdPal.Common.Services; -using Microsoft.UI.Xaml; - -namespace Microsoft.CmdPal.Common.Extensions; - -/// -/// Extension class implementing extension methods for . -/// -public static class ApplicationExtensions -{ - /// - /// Get registered services at the application level from anywhere in the - /// application. - /// - /// Note: - /// https://learn.microsoft.com/uwp/api/windows.ui.xaml.application.current?view=winrt-22621#windows-ui-xaml-application-current - /// "Application is a singleton that implements the static Current property - /// to provide shared access to the Application instance for the current - /// application. The singleton pattern ensures that state managed by - /// Application, including shared resources and properties, is available - /// from a single, shared location." - /// - /// Example of usage: - /// - /// Application.Current.GetService() - /// - /// - /// Service type. - /// Current application. - /// Service reference. - public static T GetService(this Application application) - where T : class - { - return (application as IApp)!.GetService(); - } -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Extensions/IHostExtensions.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Extensions/IHostExtensions.cs deleted file mode 100644 index 660dcd2931a3..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Extensions/IHostExtensions.cs +++ /dev/null @@ -1,40 +0,0 @@ -// 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 Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Microsoft.CmdPal.Common.Extensions; - -public static class IHostExtensions -{ - /// - /// - /// - public static T CreateInstance(this IHost host, params object[] parameters) - { - return ActivatorUtilities.CreateInstance(host.Services, parameters); - } - - /// - /// Gets the service object for the specified type, or throws an exception - /// if type was not registered. - /// - /// Service type - /// Host object - /// Service object - /// Throw an exception if the specified - /// type is not registered - public static T GetService(this IHost host) - where T : class - { - if (host.Services.GetService(typeof(T)) is not T service) - { - throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices."); - } - - return service; - } -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/Json.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/Json.cs deleted file mode 100644 index d865e10bdb7b..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/Json.cs +++ /dev/null @@ -1,36 +0,0 @@ -// 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.IO; -using System.Text; -using System.Text.Json; -using System.Threading.Tasks; - -namespace Microsoft.CmdPal.Common.Helpers; - -public static class Json -{ - public static async Task ToObjectAsync(string value) - { - if (typeof(T) == typeof(bool)) - { - return (T)(object)bool.Parse(value); - } - - await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(value)); - return (await JsonSerializer.DeserializeAsync(stream))!; - } - - public static async Task StringifyAsync(T value) - { - if (typeof(T) == typeof(bool)) - { - return value!.ToString()!.ToLowerInvariant(); - } - - await using var stream = new MemoryStream(); - await JsonSerializer.SerializeAsync(stream, value); - return Encoding.UTF8.GetString(stream.ToArray()); - } -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/NativeEventWaiter.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/NativeEventWaiter.cs index 6ec1885a4cea..2344fbb91736 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/NativeEventWaiter.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/NativeEventWaiter.cs @@ -9,7 +9,7 @@ namespace Microsoft.CmdPal.Common.Helpers; -public static class NativeEventWaiter +public static partial class NativeEventWaiter { public static void WaitForEventLoop(string eventName, Action callback) { diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/RuntimeHelper.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/RuntimeHelper.cs index 46dce07e5e50..342667cf8341 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/RuntimeHelper.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/RuntimeHelper.cs @@ -9,7 +9,7 @@ namespace Microsoft.CmdPal.Common.Helpers; -public static class RuntimeHelper +public static partial class RuntimeHelper { public static bool IsMSIX { diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Messages/HideWindowMessage.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Messages/HideWindowMessage.cs index ed698d102456..097aefdee9f5 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Messages/HideWindowMessage.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.Common/Messages/HideWindowMessage.cs @@ -4,6 +4,6 @@ namespace Microsoft.CmdPal.Common.Messages; -public record HideWindowMessage() +public partial record HideWindowMessage() { } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Microsoft.CmdPal.Common.csproj b/src/modules/cmdpal/Microsoft.CmdPal.Common/Microsoft.CmdPal.Common.csproj index 970df0df5856..5f83ca54e155 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Microsoft.CmdPal.Common.csproj +++ b/src/modules/cmdpal/Microsoft.CmdPal.Common/Microsoft.CmdPal.Common.csproj @@ -1,5 +1,6 @@ + Microsoft.CmdPal.Common enable diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Models/LocalSettingsOptions.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Models/LocalSettingsOptions.cs deleted file mode 100644 index bae7422878e4..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Models/LocalSettingsOptions.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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. - -namespace Microsoft.CmdPal.Common.Models; - -public class LocalSettingsOptions -{ - public string? ApplicationDataFolder - { - get; set; - } - - public string? LocalSettingsFile - { - get; set; - } -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/NativeMethods.txt b/src/modules/cmdpal/Microsoft.CmdPal.Common/NativeMethods.txt index 0d456bde310b..996bbd715356 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/NativeMethods.txt +++ b/src/modules/cmdpal/Microsoft.CmdPal.Common/NativeMethods.txt @@ -1,16 +1,7 @@ -EnableWindow -CoCreateInstance -FileOpenDialog -FileSaveDialog -IFileOpenDialog -IFileSaveDialog -SHCreateItemFromParsingName GetCurrentPackageFullName SetWindowLong GetWindowLong WINDOW_EX_STYLE -SHLoadIndirectString -StrFormatByteSizeEx SFBS_FLAGS MAX_PATH GetDpiForWindow diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/FileService.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/FileService.cs deleted file mode 100644 index cc6ef960983b..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/FileService.cs +++ /dev/null @@ -1,48 +0,0 @@ -// 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.IO; -using System.Text; -using System.Text.Json; -using Microsoft.CmdPal.Common.Contracts; - -namespace Microsoft.CmdPal.Common.Services; - -public class FileService : IFileService -{ - private static readonly Encoding _encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); - -#pragma warning disable CS8603 // Possible null reference return. - public T Read(string folderPath, string fileName) - { - var path = Path.Combine(folderPath, fileName); - if (File.Exists(path)) - { - using var fileStream = File.OpenText(path); - return JsonSerializer.Deserialize(fileStream.BaseStream); - } - - return default; - } -#pragma warning restore CS8603 // Possible null reference return. - - public void Save(string folderPath, string fileName, T content) - { - if (!Directory.Exists(folderPath)) - { - Directory.CreateDirectory(folderPath); - } - - var fileContent = JsonSerializer.Serialize(content); - File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, _encoding); - } - - public void Delete(string folderPath, string fileName) - { - if (fileName != null && File.Exists(Path.Combine(folderPath, fileName))) - { - File.Delete(Path.Combine(folderPath, fileName)); - } - } -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/IApp.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/IApp.cs deleted file mode 100644 index 92980dfaff19..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/IApp.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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. - -namespace Microsoft.CmdPal.Common.Services; - -/// -/// Interface for the current application singleton object exposing the API -/// that can be accessed from anywhere in the application. -/// -public interface IApp -{ - /// - /// Gets services registered at the application level. - /// - public T GetService() - where T : class; -} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/LocalSettingsService.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/LocalSettingsService.cs deleted file mode 100644 index e4cd2a174b28..000000000000 --- a/src/modules/cmdpal/Microsoft.CmdPal.Common/Services/LocalSettingsService.cs +++ /dev/null @@ -1,120 +0,0 @@ -// 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.Collections.Generic; -using System.IO; -using System.Threading.Tasks; -using Microsoft.CmdPal.Common.Contracts; -using Microsoft.CmdPal.Common.Helpers; -using Microsoft.CmdPal.Common.Models; -using Microsoft.Extensions.Options; -using Windows.Storage; - -namespace Microsoft.CmdPal.Common.Services; - -public class LocalSettingsService : ILocalSettingsService -{ - // TODO! for now, we're hardcoding the path as effectively: - // %localappdata%\CmdPal\LocalSettings.json - private const string DefaultApplicationDataFolder = "CmdPal"; - private const string DefaultLocalSettingsFile = "LocalSettings.json"; - - private readonly IFileService _fileService; - private readonly LocalSettingsOptions _options; - - private readonly string _localApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - private readonly string _applicationDataFolder; - private readonly string _localSettingsFile; - - private readonly bool _isMsix; - - private Dictionary _settings; - private bool _isInitialized; - - public LocalSettingsService(IFileService fileService, IOptions options) - { - _isMsix = false; // RuntimeHelper.IsMSIX; - - _fileService = fileService; - _options = options.Value; - - _applicationDataFolder = Path.Combine(_localApplicationData, _options.ApplicationDataFolder ?? DefaultApplicationDataFolder); - _localSettingsFile = _options.LocalSettingsFile ?? DefaultLocalSettingsFile; - - _settings = new Dictionary(); - } - - private async Task InitializeAsync() - { - if (!_isInitialized) - { - _settings = await Task.Run(() => _fileService.Read>(_applicationDataFolder, _localSettingsFile)) ?? new Dictionary(); - - _isInitialized = true; - } - } - - public async Task HasSettingAsync(string key) - { - if (_isMsix) - { - return ApplicationData.Current.LocalSettings.Values.ContainsKey(key); - } - else - { - await InitializeAsync(); - - if (_settings != null) - { - return _settings.ContainsKey(key); - } - } - - return false; - } - - public async Task ReadSettingAsync(string key) - { - if (_isMsix) - { - if (ApplicationData.Current.LocalSettings.Values.TryGetValue(key, out var obj)) - { - return await Json.ToObjectAsync((string)obj); - } - } - else - { - await InitializeAsync(); - - if (_settings != null && _settings.TryGetValue(key, out var obj)) - { - var s = obj.ToString(); - - if (s != null) - { - return await Json.ToObjectAsync(s); - } - } - } - - return default; - } - - public async Task SaveSettingAsync(string key, T value) - { - if (_isMsix) - { - ApplicationData.Current.LocalSettings.Values[key] = await Json.StringifyAsync(value!); - } - else - { - await InitializeAsync(); - - _settings[key] = await Json.StringifyAsync(value!); - - await Task.Run(() => _fileService.Save(_applicationDataFolder, _localSettingsFile, _settings)); - } - } -}