From 0761386f00928c99e62971e30acadc2d41825b41 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 18 Dec 2024 22:46:15 -0600 Subject: [PATCH] fix: fixing issue with BuildConfiguration overrides --- .../Generators/AppSettingsGenerator.cs | 19 +++-- .../Generators/GeneratorBase.cs | 2 + .../Utils/EnvironmentAnalyzer.cs | 72 +++++++++++-------- .../Fixtures/FixtureBase.cs | 42 +++++------ .../Utils/EnvironmentAnalyzerFixture.cs | 40 +++++++++-- .../Mobile.BuildTools.Tests.csproj | 14 ++-- 6 files changed, 123 insertions(+), 66 deletions(-) diff --git a/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs b/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs index ba7e4445..f63f3ec7 100644 --- a/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs +++ b/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs @@ -12,6 +12,8 @@ namespace Mobile.BuildTools.AppSettings.Generators [Generator] public sealed class AppSettingsGenerator : GeneratorBase { + private const string DefaultPrefix = "BuildTools_"; + public const string AutoGeneratedMessage = @"This code was generated by Mobile.BuildTools. For more information please visit https://mobilebuildtools.com or to file an issue please see https://github.com/dansiegel/Mobile.BuildTools @@ -57,7 +59,7 @@ protected override void Generate() settingsConfig.Delimiter = settingsConfig.Delimiter.Trim(); if (string.IsNullOrEmpty(settingsConfig.Prefix)) - settingsConfig.Prefix = "BuildTools_"; + settingsConfig.Prefix = DefaultPrefix; else settingsConfig.Prefix = settingsConfig.Prefix.Trim(); @@ -224,13 +226,19 @@ internal IDictionary GetMergedSecrets(SettingsConfig settingsCon hasErrors = false; foreach (var prop in settingsConfig.Properties) { - var searchKeys = new[] + var prefixKey = settingsConfig.Prefix.EndsWith("_") ? $"{settingsConfig.Prefix}{prop.Name}" : $"{settingsConfig.Prefix}_{prop.Name}"; + + var searchKeys = new List { - $"{settingsConfig.Prefix}{prop.Name}", - $"{settingsConfig.Prefix}_{prop.Name}", prop.Name, + prefixKey }; + if (settingsConfig.Prefix != DefaultPrefix) + { + searchKeys.Add($"{DefaultPrefix}{prefixKey}"); + } + string key = null; foreach (var searchKey in searchKeys) { @@ -238,7 +246,8 @@ internal IDictionary GetMergedSecrets(SettingsConfig settingsCon break; key = env.Keys.FirstOrDefault(x => - x.Equals(searchKey, StringComparison.InvariantCultureIgnoreCase)); + x.Equals(searchKey, StringComparison.InvariantCultureIgnoreCase) || + x.Equals($"{BuildConfiguration}_{searchKey}", StringComparison.InvariantCultureIgnoreCase)); } if (string.IsNullOrEmpty(key)) diff --git a/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs b/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs index 0264cfd6..4f8ecdca 100644 --- a/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs +++ b/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs @@ -18,6 +18,8 @@ public abstract class GeneratorBase : ISourceGenerator protected string ProjectName => _projectName; protected string RootNamespace => _rootNamespace; + protected string BuildConfiguration => _buildConfiguration; + protected BuildToolsConfig Config { get; private set; } public void Execute(GeneratorExecutionContext context) diff --git a/src/Mobile.BuildTools.Reference/Utils/EnvironmentAnalyzer.cs b/src/Mobile.BuildTools.Reference/Utils/EnvironmentAnalyzer.cs index 3261785f..97083bb4 100644 --- a/src/Mobile.BuildTools.Reference/Utils/EnvironmentAnalyzer.cs +++ b/src/Mobile.BuildTools.Reference/Utils/EnvironmentAnalyzer.cs @@ -6,7 +6,6 @@ using System.Text.RegularExpressions; using Mobile.BuildTools.Build; using Mobile.BuildTools.Handlers; -using Mobile.BuildTools.Reference.IO; namespace Mobile.BuildTools.Utils { @@ -19,14 +18,14 @@ public static class EnvironmentAnalyzer public static IDictionary GatherEnvironmentVariables(IBuildConfiguration buildConfiguration = null, bool includeManifest = false) { var env = new Dictionary(); - if(buildConfiguration is null) + if (buildConfiguration is null) { foreach (var key in Environment.GetEnvironmentVariables().Keys) { env[key.ToString()] = Environment.GetEnvironmentVariable(key.ToString()); } - return env; + return env; } env = GetEnvironmentVariables(buildConfiguration); @@ -60,7 +59,7 @@ public static IDictionary GatherEnvironmentVariables(IBuildConfi { break; } - else if(!directories.Contains(lookupDir)) + else if (!directories.Contains(lookupDir)) { directories.Add(lookupDir); } @@ -79,30 +78,45 @@ public static IDictionary GatherEnvironmentVariables(IBuildConfi .Where(x => x.Exists) .ToList(); - directories - .SelectMany(x => - x.EnumerateFiles("*.json", SearchOption.TopDirectoryOnly) - .Where(x => x.Name == Constants.SecretsJsonFileName || x.Name == string.Format(Constants.SecretsJsonConfigurationFileFormat, configuration))) - .Where(x => x.Exists) - .Select(x => x.FullName) - .Distinct() - .ForEach(x => + string[] expectedFileNames = + [ + Constants.SecretsJsonFileName, + string.Format(Constants.SecretsJsonConfigurationFileFormat, configuration) + ]; + foreach (var fileName in expectedFileNames) + { + foreach (var directory in directories) { - buildConfiguration.Logger.LogWarning($"The secrets.json has been deprecated and will no longer be supported in a future version. Please migrate '{x}' to appsettings.json"); - LoadSecrets(x, ref env); - }); - - directories - .SelectMany(x => - x.EnumerateFiles("*.json", SearchOption.TopDirectoryOnly) - .Where(x => x.Name == Constants.AppSettingsJsonFileName - || x.Name == string.Format(Constants.AppSettingsJsonConfigurationFileFormat, configuration) - || x.Name == string.Format(Constants.AppSettingsJsonConfigurationFileFormat, $"{buildConfiguration.Platform}") - || x.Name == string.Format(Constants.AppSettingsJsonConfigurationFileFormat, $"{buildConfiguration.Platform}.{configuration}"))) - .Where(x => x.Exists) - .Select(x => x.FullName) - .Distinct() - .ForEach(x => LoadSecrets(x, ref env)); + var file = new FileInfo(Path.Combine(directory.FullName, fileName)); + if (file.Exists) + { + buildConfiguration.Logger.LogWarning($"The secrets.json has been deprecated and will no longer be supported in a future version. Please migrate '{fileName}' to appsettings.json"); + LoadSecrets(file.FullName, ref env); + break; + } + } + } + + expectedFileNames = + [ + Constants.AppSettingsJsonFileName, + string.Format(Constants.AppSettingsJsonConfigurationFileFormat, configuration), + string.Format(Constants.AppSettingsJsonConfigurationFileFormat, $"{buildConfiguration.Platform}"), + string.Format(Constants.AppSettingsJsonConfigurationFileFormat, $"{buildConfiguration.Platform}.{configuration}") + ]; + + foreach(var fileName in expectedFileNames) + { + foreach(var directory in directories) + { + var file = new FileInfo(Path.Combine(directory.FullName, fileName)); + if (file.Exists) + { + LoadSecrets(file.FullName, ref env); + break; + } + } + } if (includeManifest) { @@ -134,12 +148,12 @@ public static IDictionary GatherEnvironmentVariables(IBuildConfi private static Dictionary GetEnvironmentVariables(IBuildConfiguration buildConfiguration) { var env = new Dictionary(); - foreach((var key, var value) in buildConfiguration.Configuration.Environment.Defaults) + foreach ((var key, var value) in buildConfiguration.Configuration.Environment.Defaults) { env[key] = value; } - if(buildConfiguration.Configuration.Environment.Configuration.ContainsKey(buildConfiguration.BuildConfiguration)) + if (buildConfiguration.Configuration.Environment.Configuration.ContainsKey(buildConfiguration.BuildConfiguration)) { var configEnvironment = buildConfiguration.Configuration.Environment.Configuration[buildConfiguration.BuildConfiguration]; if(configEnvironment is not null) diff --git a/tests/Mobile.BuildTools.Tests/Fixtures/FixtureBase.cs b/tests/Mobile.BuildTools.Tests/Fixtures/FixtureBase.cs index a0b88c28..b1481448 100644 --- a/tests/Mobile.BuildTools.Tests/Fixtures/FixtureBase.cs +++ b/tests/Mobile.BuildTools.Tests/Fixtures/FixtureBase.cs @@ -11,39 +11,39 @@ public abstract class FixtureBase protected ITestOutputHelper _testOutputHelper { get; } protected string ProjectDirectory { get; } - protected FixtureBase(ITestOutputHelper testOutputHelper) + protected FixtureBase(ITestOutputHelper testOutputHelper) : this(null, testOutputHelper) { } protected FixtureBase(string projectDirectory, ITestOutputHelper testOutputHelper) - { + { _testOutputHelper = testOutputHelper; ProjectDirectory = projectDirectory; } protected TestBuildConfiguration GetConfiguration(string testName = null) - { - if(string.IsNullOrEmpty(testName)) - { - var stackTrace = new StackTrace(); - testName = stackTrace.GetFrame(1).GetMethod().Name; - } - - if (testName.Length > 20) - testName = System.Guid.NewGuid().ToString(); - - var tempDir = Path.GetTempPath(); - var projectDirectory = ProjectDirectory; - var solutionDirectory = ProjectDirectory; - if (string.IsNullOrEmpty(projectDirectory)) - { - projectDirectory = Path.Combine(tempDir, "Tests", GetType().Name, testName, "SolutionDir", "src", testName); - solutionDirectory = Path.Combine(tempDir, "Tests", GetType().Name, testName, "SolutionDir"); + { + if(string.IsNullOrEmpty(testName)) + { + var stackTrace = new StackTrace(); + testName = stackTrace.GetFrame(1).GetMethod().Name; + } + + if (testName.Length > 20) + testName = System.Guid.NewGuid().ToString(); + + var tempDir = Path.GetTempPath(); + var projectDirectory = ProjectDirectory; + var solutionDirectory = ProjectDirectory; + if (string.IsNullOrEmpty(projectDirectory)) + { + projectDirectory = Path.Combine(tempDir, "Tests", GetType().Name, testName, "SolutionDir", "src", testName); + solutionDirectory = Path.Combine(tempDir, "Tests", GetType().Name, testName, "SolutionDir"); } var testOutput = Path.Combine(tempDir, "Tests", GetType().Name, testName); - ResetTestOutputDirectory(testOutput); + ResetTestOutputDirectory(testOutput); ResetTestOutputDirectory(projectDirectory); return new TestBuildConfiguration @@ -51,7 +51,7 @@ protected TestBuildConfiguration GetConfiguration(string testName = null) Logger = new XunitLog(_testOutputHelper), Platform = Platform.Unsupported, IntermediateOutputPath = testOutput, - ProjectDirectory = projectDirectory, + ProjectDirectory = projectDirectory, SolutionDirectory = solutionDirectory, BuildConfiguration = "Debug", ProjectName = "AwesomeApp", diff --git a/tests/Mobile.BuildTools.Tests/Fixtures/Utils/EnvironmentAnalyzerFixture.cs b/tests/Mobile.BuildTools.Tests/Fixtures/Utils/EnvironmentAnalyzerFixture.cs index 0e0af730..20791bb4 100644 --- a/tests/Mobile.BuildTools.Tests/Fixtures/Utils/EnvironmentAnalyzerFixture.cs +++ b/tests/Mobile.BuildTools.Tests/Fixtures/Utils/EnvironmentAnalyzerFixture.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text.Json; +using System.Text.Json; using Mobile.BuildTools.Models.Settings; using Mobile.BuildTools.Utils; using Xunit; @@ -42,6 +39,41 @@ public void GetsValuesFromJson(string filename) Assert.Equal(secrets.SampleProp, mergedSecrets["SampleProp"]); } + [Fact] + public void GetsValueFromBuildConfigurationOverrideJson() + { + var config = GetConfiguration(); + var settingsConfig = new SettingsConfig + { + Properties = + [ + new ValueConfig + { + Name = "SampleProp", + PropertyType = PropertyType.String + } + ] + }; + + config.Configuration.AppSettings[config.ProjectName] = new List([settingsConfig]); + + var secrets = new + { + SampleProp = "Hello Tests" + }; + File.WriteAllText(Path.Combine(config.ProjectDirectory, "appsettings.json"), JsonSerializer.Serialize(secrets)); + var buildConfigurationSecrets = new + { + SampleProp = $"Hello {config.BuildConfiguration}" + }; + File.WriteAllText(Path.Combine(config.ProjectDirectory, $"appsettings.{config.BuildConfiguration}.json"), JsonSerializer.Serialize(buildConfigurationSecrets)); + + var mergedSecrets = EnvironmentAnalyzer.GatherEnvironmentVariables(config); + + Assert.True(mergedSecrets.ContainsKey("SampleProp")); + Assert.Equal(buildConfigurationSecrets.SampleProp, mergedSecrets["SampleProp"]); + } + [Fact] public void GetsValuesFromConfigurationEnvironment() { diff --git a/tests/Mobile.BuildTools.Tests/Mobile.BuildTools.Tests.csproj b/tests/Mobile.BuildTools.Tests/Mobile.BuildTools.Tests.csproj index b415e6b9..daa0363d 100644 --- a/tests/Mobile.BuildTools.Tests/Mobile.BuildTools.Tests.csproj +++ b/tests/Mobile.BuildTools.Tests/Mobile.BuildTools.Tests.csproj @@ -1,4 +1,4 @@ - + net472 @@ -9,9 +9,9 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -20,9 +20,9 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive