From 56f870ff9eae0cd56f576b0601cb0bdb61ff5aa8 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:12:52 +1100 Subject: [PATCH 1/8] Use file-scoped namespace --- ...formTargetBuildPropertyPageEnumProvider.cs | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 69d475bd493..2f916ad172b 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -5,53 +5,52 @@ using Microsoft.VisualStudio.Threading; using EnumCollection = System.Collections.Generic.ICollection; -namespace Microsoft.VisualStudio.ProjectSystem.Properties +namespace Microsoft.VisualStudio.ProjectSystem.Properties; + +/// +/// Responsible for producing valid values for the TargetPlatform property from a design-time build. +/// +[ExportDynamicEnumValuesProvider("PlatformTargetEnumProvider")] +[AppliesTo(ProjectCapability.DotNet)] +internal class PlatformTargetBuildPropertyPageEnumProvider : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator { - /// - /// Responsible for producing valid values for the TargetPlatform property from a design-time build. - /// - [ExportDynamicEnumValuesProvider("PlatformTargetEnumProvider")] - [AppliesTo(ProjectCapability.DotNet)] - internal class PlatformTargetBuildPropertyPageEnumProvider : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator - { - private const string AnyCpuPlatformName = "AnyCPU"; - private const string AnyCpuDisplayName = "Any CPU"; - - private readonly ProjectProperties _properties; - - [ImportingConstructor] - public PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) - { - _properties = properties; - } + private const string AnyCpuPlatformName = "AnyCPU"; + private const string AnyCpuDisplayName = "Any CPU"; - public bool AllowCustomValues => false; + private readonly ProjectProperties _properties; - public async Task GetListedValuesAsync() - { - var result = new List(); + [ImportingConstructor] + public PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) + { + _properties = properties; + } - ConfigurationGeneral configuration = await _properties.GetConfigurationGeneralPropertiesAsync(); + public bool AllowCustomValues => false; - string availablePlatformsTargets = await configuration.AvailablePlatforms.GetDisplayValueAsync(); + public async Task GetListedValuesAsync() + { + var result = new List(); - foreach (string platformTarget in new LazyStringSplit(availablePlatformsTargets, ',')) - { - result.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = platformTarget.Equals(AnyCpuPlatformName, StringComparisons.ConfigurationDimensionValues) ? AnyCpuDisplayName : platformTarget })); - } + ConfigurationGeneral configuration = await _properties.GetConfigurationGeneralPropertiesAsync(); - return result; - } + string availablePlatformsTargets = await configuration.AvailablePlatforms.GetDisplayValueAsync(); - public Task GetProviderAsync(IList? options) + foreach (string platformTarget in new LazyStringSplit(availablePlatformsTargets, ',')) { - return Task.FromResult(this); + result.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = platformTarget.Equals(AnyCpuPlatformName, StringComparisons.ConfigurationDimensionValues) ? AnyCpuDisplayName : platformTarget })); } - public Task TryCreateEnumValueAsync(string userSuppliedValue) - { - return TaskResult.Null(); - } + return result; + } + + public Task GetProviderAsync(IList? options) + { + return Task.FromResult(this); + } + + public Task TryCreateEnumValueAsync(string userSuppliedValue) + { + return TaskResult.Null(); } } From dc26a024cd51f2dcf0daec46ba15bd9698b86772 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:13:18 +1100 Subject: [PATCH 2/8] Use primary constructor --- .../PlatformTargetBuildPropertyPageEnumProvider.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 2f916ad172b..914691590d0 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -12,26 +12,19 @@ namespace Microsoft.VisualStudio.ProjectSystem.Properties; /// [ExportDynamicEnumValuesProvider("PlatformTargetEnumProvider")] [AppliesTo(ProjectCapability.DotNet)] -internal class PlatformTargetBuildPropertyPageEnumProvider : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator +[method: ImportingConstructor] +internal class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator { private const string AnyCpuPlatformName = "AnyCPU"; private const string AnyCpuDisplayName = "Any CPU"; - private readonly ProjectProperties _properties; - - [ImportingConstructor] - public PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) - { - _properties = properties; - } - public bool AllowCustomValues => false; public async Task GetListedValuesAsync() { var result = new List(); - ConfigurationGeneral configuration = await _properties.GetConfigurationGeneralPropertiesAsync(); + ConfigurationGeneral configuration = await properties.GetConfigurationGeneralPropertiesAsync(); string availablePlatformsTargets = await configuration.AvailablePlatforms.GetDisplayValueAsync(); From 89fc46196938fcc834bce45e06eddabb4524fdfc Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:14:29 +1100 Subject: [PATCH 3/8] Add local function --- .../PlatformTargetBuildPropertyPageEnumProvider.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 914691590d0..3050c159bb6 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -15,9 +15,6 @@ namespace Microsoft.VisualStudio.ProjectSystem.Properties; [method: ImportingConstructor] internal class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator { - private const string AnyCpuPlatformName = "AnyCPU"; - private const string AnyCpuDisplayName = "Any CPU"; - public bool AllowCustomValues => false; public async Task GetListedValuesAsync() @@ -30,10 +27,18 @@ public async Task GetListedValuesAsync() foreach (string platformTarget in new LazyStringSplit(availablePlatformsTargets, ',')) { - result.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = platformTarget.Equals(AnyCpuPlatformName, StringComparisons.ConfigurationDimensionValues) ? AnyCpuDisplayName : platformTarget })); + result.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = GetDisplayName(platformTarget) })); } return result; + + static string GetDisplayName(string platformTarget) + { + const string AnyCpuPlatformName = "AnyCPU"; + const string AnyCpuDisplayName = "Any CPU"; + + return platformTarget.Equals(AnyCpuPlatformName, StringComparisons.ConfigurationDimensionValues) ? AnyCpuDisplayName : platformTarget; + } } public Task GetProviderAsync(IList? options) From 24d0daf71cbad1c29a28c865143dbf6056123415 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:15:04 +1100 Subject: [PATCH 4/8] Inline type alias --- .../Properties/PlatformTargetBuildPropertyPageEnumProvider.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 3050c159bb6..25283574825 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -3,7 +3,6 @@ using Microsoft.Build.Framework.XamlTypes; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Threading; -using EnumCollection = System.Collections.Generic.ICollection; namespace Microsoft.VisualStudio.ProjectSystem.Properties; @@ -17,7 +16,7 @@ internal class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties pro { public bool AllowCustomValues => false; - public async Task GetListedValuesAsync() + public async Task> GetListedValuesAsync() { var result = new List(); From 1b2ffea5641671d37429f1cec96bf72e3828d033 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:17:33 +1100 Subject: [PATCH 5/8] Use target-typed new and collection expression --- .../Properties/PlatformTargetBuildPropertyPageEnumProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 25283574825..d1410142b95 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -18,7 +18,7 @@ internal class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties pro public async Task> GetListedValuesAsync() { - var result = new List(); + List result = []; ConfigurationGeneral configuration = await properties.GetConfigurationGeneralPropertiesAsync(); From bf950127b870b9e99db07b6db74a64ea9ef4e435 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:17:48 +1100 Subject: [PATCH 6/8] Improve API doc --- .../PlatformTargetBuildPropertyPageEnumProvider.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index d1410142b95..7d7503b1341 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -7,8 +7,11 @@ namespace Microsoft.VisualStudio.ProjectSystem.Properties; /// -/// Responsible for producing valid values for the TargetPlatform property from a design-time build. +/// Responsible for producing valid values for the TargetPlatform MSBuild property. /// +/// +/// Candidate values from the AvailablePlatforms MSBuild property. +/// [ExportDynamicEnumValuesProvider("PlatformTargetEnumProvider")] [AppliesTo(ProjectCapability.DotNet)] [method: ImportingConstructor] From 2d77b264eb4341fb5b54963de795feabbad802c7 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:20:06 +1100 Subject: [PATCH 7/8] Prevent duplicate target platform values --- .../PlatformTargetBuildPropertyPageEnumProvider.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 7d7503b1341..09a15e6a6d1 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -21,18 +21,23 @@ internal class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties pro public async Task> GetListedValuesAsync() { - List result = []; - ConfigurationGeneral configuration = await properties.GetConfigurationGeneralPropertiesAsync(); string availablePlatformsTargets = await configuration.AvailablePlatforms.GetDisplayValueAsync(); + List enumValues = []; + HashSet targets = new(StringComparers.ConfigurationDimensionValues); + foreach (string platformTarget in new LazyStringSplit(availablePlatformsTargets, ',')) { - result.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = GetDisplayName(platformTarget) })); + // Prevent duplicates. + if (targets.Add(platformTarget)) + { + enumValues.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = GetDisplayName(platformTarget) })); + } } - return result; + return enumValues; static string GetDisplayName(string platformTarget) { From c7c72cc28ac0ab2ab9f3ad71bc468678637c3c7e Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Tue, 10 Dec 2024 15:20:31 +1100 Subject: [PATCH 8/8] Seal class --- .../Properties/PlatformTargetBuildPropertyPageEnumProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 09a15e6a6d1..3d45bb9664d 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -15,7 +15,7 @@ namespace Microsoft.VisualStudio.ProjectSystem.Properties; [ExportDynamicEnumValuesProvider("PlatformTargetEnumProvider")] [AppliesTo(ProjectCapability.DotNet)] [method: ImportingConstructor] -internal class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator +internal sealed class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator { public bool AllowCustomValues => false;