From 2aea65b39024b8d3c44b57310f98fd61e381c599 Mon Sep 17 00:00:00 2001 From: IT&feel CA Date: Tue, 8 Mar 2022 17:12:57 -0400 Subject: [PATCH] fixed dependencies && forcing renerate default file --- Helpers/ProjectExtensions.cs | 43 +- InternalSecureStorage/SecureLocalStorage.cs | 53 +- ShieldVSExtensionPackage.cs | 7 +- ToolWindows/ConfigurationWindow.cs | 2 +- ToolWindows/ConfigurationWindowControl.xaml | 535 +++++++++++------- .../ConfigurationWindowControl.xaml.cs | 51 +- 6 files changed, 416 insertions(+), 275 deletions(-) diff --git a/Helpers/ProjectExtensions.cs b/Helpers/ProjectExtensions.cs index 9a5a7e8..4af77be 100644 --- a/Helpers/ProjectExtensions.cs +++ b/Helpers/ProjectExtensions.cs @@ -357,25 +357,30 @@ public static async Task> GetEvaluatedPropertiesAsync( } } - var unConfiguredProject = context.UnconfiguredProject; - var configuredProject = await unConfiguredProject.GetSuggestedConfiguredProjectAsync(); - var properties = configuredProject.Services.ProjectPropertiesProvider.GetCommonProperties(); - - return new Dictionary - { - { "ProjectPath", await properties.GetEvaluatedPropertyValueAsync("ProjectPath").ConfigureAwait(false)}, - { "TargetPath", await properties.GetEvaluatedPropertyValueAsync("TargetPath").ConfigureAwait(false)}, - { "TargetName", await properties.GetEvaluatedPropertyValueAsync("TargetName").ConfigureAwait(false)}, - { "TargetExt", await properties.GetEvaluatedPropertyValueAsync("TargetExt").ConfigureAwait(false)}, - { "TargetFileName", await properties.GetEvaluatedPropertyValueAsync("TargetFileName").ConfigureAwait(false)}, - { "TargetDir", await properties.GetEvaluatedPropertyValueAsync("TargetDir").ConfigureAwait(false)}, - { "ProjectDir", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)}, - { "ProjectName", await properties.GetEvaluatedPropertyValueAsync("ProjectName").ConfigureAwait(false)}, - { "SolutionName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)}, - { "SolutionDir", await properties.GetEvaluatedPropertyValueAsync("SolutionDir").ConfigureAwait(false)}, - { "PlatformName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)}, - { "ConfigurationName", await properties.GetEvaluatedPropertyValueAsync("ConfigurationName").ConfigureAwait(false)}, - }; + var unConfiguredProject = context?.UnconfiguredProject; + if (unConfiguredProject != null) + { + var configuredProject = await unConfiguredProject.GetSuggestedConfiguredProjectAsync(); + var properties = configuredProject.Services.ProjectPropertiesProvider.GetCommonProperties(); + + return new Dictionary + { + { "ProjectPath", await properties.GetEvaluatedPropertyValueAsync("ProjectPath").ConfigureAwait(false)}, + { "TargetPath", await properties.GetEvaluatedPropertyValueAsync("TargetPath").ConfigureAwait(false)}, + { "TargetName", await properties.GetEvaluatedPropertyValueAsync("TargetName").ConfigureAwait(false)}, + { "TargetExt", await properties.GetEvaluatedPropertyValueAsync("TargetExt").ConfigureAwait(false)}, + { "TargetFileName", await properties.GetEvaluatedPropertyValueAsync("TargetFileName").ConfigureAwait(false)}, + { "TargetDir", await properties.GetEvaluatedPropertyValueAsync("TargetDir").ConfigureAwait(false)}, + { "ProjectDir", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)}, + { "ProjectName", await properties.GetEvaluatedPropertyValueAsync("ProjectName").ConfigureAwait(false)}, + { "SolutionName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)}, + { "SolutionDir", await properties.GetEvaluatedPropertyValueAsync("SolutionDir").ConfigureAwait(false)}, + { "PlatformName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)}, + { "ConfigurationName", await properties.GetEvaluatedPropertyValueAsync("ConfigurationName").ConfigureAwait(false)}, + }; + } + + return new Dictionary(); } catch { diff --git a/InternalSecureStorage/SecureLocalStorage.cs b/InternalSecureStorage/SecureLocalStorage.cs index 15716b9..b7aa65b 100644 --- a/InternalSecureStorage/SecureLocalStorage.cs +++ b/InternalSecureStorage/SecureLocalStorage.cs @@ -15,8 +15,7 @@ public class SecureLocalStorage : ISecureLocalStorage internal void CreateIfNotExists(string path) { - if (!Directory.Exists(path)) - Directory.CreateDirectory(path); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); } public SecureLocalStorage(ISecureLocalStorageConfig configuration) @@ -27,45 +26,41 @@ public SecureLocalStorage(ISecureLocalStorageConfig configuration) Read(); } - internal byte[] EncryptData(string data, byte[] key, DataProtectionScope scope) { - if (data == null) - throw new ArgumentNullException(nameof(data)); - if (data.Length <= 0) - throw new ArgumentException("data"); - if (key == null) - throw new ArgumentNullException(nameof(key)); - if (key.Length <= 0) - throw new ArgumentException("key"); + if (data == null) throw new ArgumentNullException(nameof(data)); + if (data.Length <= 0) throw new ArgumentException("data"); + if (key == null) throw new ArgumentNullException(nameof(key)); + if (key.Length <= 0) throw new ArgumentException("key"); return ProtectedData.Protect(Encoding.UTF8.GetBytes(data), key, scope); } internal string DecryptData(byte[] data, byte[] key, DataProtectionScope scope) { - if (data == null) - throw new ArgumentNullException(nameof(data)); - if (data.Length <= 0) - throw new ArgumentException("data"); - if (key == null) - throw new ArgumentNullException(nameof(key)); - if (key.Length <= 0) - throw new ArgumentException("key"); + if (null == data) throw new ArgumentNullException(nameof(data)); + if (data.Length <= 0) throw new ArgumentException("data"); + if (null == key) throw new ArgumentNullException(nameof(key)); + if (key.Length <= 0) throw new ArgumentException("key"); return Encoding.UTF8.GetString(ProtectedData.Unprotect(data, key, scope)); } internal void Read() - => StoredData = - File.Exists(Path.Combine(Config.StoragePath, "default")) - ? JsonSerializer.Deserialize>(DecryptData(File.ReadAllBytes(Path.Combine(Config.StoragePath, "default")), Key, DataProtectionScope.LocalMachine)) - : new Dictionary(); - + { + Write(); // TODO remove it, forcing regenerate default file + StoredData = File.Exists(Path.Combine(Config.StoragePath, "default")) + ? JsonSerializer.Deserialize>(DecryptData(File.ReadAllBytes(Path.Combine(Config.StoragePath, "default")), Key, DataProtectionScope.LocalMachine)) + : new Dictionary(); + } internal void Write() - => File.WriteAllBytes(Path.Combine(Config.StoragePath, "default"), - EncryptData(JsonSerializer.Serialize(StoredData), Key, DataProtectionScope.LocalMachine)); + { + if (StoredData is null) return; + + File.WriteAllBytes(Path.Combine(Config.StoragePath, "default"), + EncryptData(JsonSerializer.Serialize(StoredData), Key, DataProtectionScope.LocalMachine)); + } public int Count => StoredData.Count; @@ -76,8 +71,7 @@ public void Clear() public bool Exists() => File.Exists(Path.Combine(Config.StoragePath, "default")); - public bool Exists(string key) - => StoredData.ContainsKey(key); + public bool Exists(string key) => StoredData?.ContainsKey(key) ?? false; public string Get(string key) => !StoredData.TryGetValue(key, out var value) ? default : JsonSerializer.Deserialize(value ?? string.Empty); @@ -93,7 +87,8 @@ public IReadOnlyCollection Keys() public void Remove(string key) { - StoredData.Remove(key); + if (Exists(key)) StoredData?.Remove(key); + Write(); } diff --git a/ShieldVSExtensionPackage.cs b/ShieldVSExtensionPackage.cs index 1b1938a..93f985a 100644 --- a/ShieldVSExtensionPackage.cs +++ b/ShieldVSExtensionPackage.cs @@ -135,8 +135,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke solutionPersistenceService.LoadPackageUserOpts(this, ShieldConfiguration); - if (isSolutionLoaded) - SolutionEventsOnOpened(); + if (isSolutionLoaded) SolutionEventsOnOpened(); solutionEvents.Opened += SolutionEventsOnOpened; @@ -149,9 +148,7 @@ private bool TryReloadStorage() try { - LocalStorage = new SecureLocalStorage( - new CustomLocalStorageConfig(null, "DotnetsaferShieldForVisualStudio").WithDefaultKeyBuilder() - ); + LocalStorage = new SecureLocalStorage( new CustomLocalStorageConfig(null, "DotnetsaferShieldForVisualStudio").WithDefaultKeyBuilder()); ExtensionConfiguration = LocalStorage.Exists(ExtensionConfigurationFile) ? LocalStorage.Get(ExtensionConfigurationFile) diff --git a/ToolWindows/ConfigurationWindow.cs b/ToolWindows/ConfigurationWindow.cs index 5127b28..9840fbe 100644 --- a/ToolWindows/ConfigurationWindow.cs +++ b/ToolWindows/ConfigurationWindow.cs @@ -4,7 +4,7 @@ namespace ShieldVSExtension.ToolWindows { [Guid("23b0bdd9-76b7-4917-a75e-e29a99bcd863")] - public class ConfigurationWindow : ToolWindowPane + public sealed class ConfigurationWindow : ToolWindowPane { public ConfigurationWindow() : base(null) { diff --git a/ToolWindows/ConfigurationWindowControl.xaml b/ToolWindows/ConfigurationWindowControl.xaml index 89d713f..06a5e8e 100644 --- a/ToolWindows/ConfigurationWindowControl.xaml +++ b/ToolWindows/ConfigurationWindowControl.xaml @@ -1,84 +1,107 @@  + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0" + xmlns:local="clr-namespace:ShieldVSExtension.ToolWindows" + xmlns:PlatformUI="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.15.0" + xmlns:Imaging="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.Imaging" + xmlns:UI_Extensions="clr-namespace:ShieldVSExtension.UI_Extensions" + x:Class="ShieldVSExtension.ToolWindows.ConfigurationWindowControl" + Background="#FF1E1E1E" + Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}" + mc:Ignorable="d" + x:Name="ShieldConfiguration" Title="Dotnetsafer Shield Configuration" Width="800" + Icon="{DynamicResource ShieldIcon}" ResizeMode="NoResize" Height="606"> - + - - + - + - + - - - - - - - - - - - + + + + + + + + + + + - - - - - - + + + + + + - - + + - - + + - + - - + + - + - - - - + + + + @@ -88,7 +111,8 @@ - + @@ -119,32 +143,35 @@ + Background="Transparent" /> - + - + + Binding="{Binding FileToProtect, Converter={StaticResource StringToBoolConverter}}" + Value="True"> + Binding="{Binding FileToProtect, Converter={StaticResource StringToBoolConverter}}" + Value="True" /> + Binding="{Binding InheritFromProject, Converter={StaticResource InvertCondition}}" + Value="True" /> @@ -152,20 +179,25 @@ - + - - - + + - - + + - - - -