diff --git a/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs b/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs index bb0602d0..1ba57fd4 100644 --- a/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs +++ b/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs @@ -28,7 +28,7 @@ the code is regenerated. protected override void Generate() { - var settings = ConfigHelper.GetSettingsConfig(ProjectName, Config); + var settings = Environment.GeneratedClasses; if (settings is null || !settings.Any()) return; @@ -64,7 +64,7 @@ protected override void Generate() settingsConfig.Prefix = settingsConfig.Prefix.Trim(); if (string.IsNullOrEmpty(settingsConfig.RootNamespace)) - settingsConfig.RootNamespace = RootNamespace; + settingsConfig.RootNamespace = Environment.RootNamespace; else settingsConfig.RootNamespace = settingsConfig.RootNamespace.Trim(); @@ -239,7 +239,7 @@ internal IDictionary GetMergedSecrets(SettingsConfig settingsCon key = env.Keys.FirstOrDefault(x => x.Equals(searchKey, StringComparison.InvariantCultureIgnoreCase) || - x.Equals($"{BuildConfiguration}_{searchKey}", StringComparison.InvariantCultureIgnoreCase)); + x.Equals($"{Environment.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 e294b438..91d0f65e 100644 --- a/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs +++ b/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs @@ -10,17 +10,6 @@ namespace Mobile.BuildTools.AppSettings.Generators public abstract class GeneratorBase : ISourceGenerator { protected GeneratorExecutionContext GeneratorContext { get; private set; } - private string _buildConfiguration; - private string _projectName; - private string _targetFrameworkAssembly; - private string _rootNamespace; - - protected string ProjectName => _projectName; - protected string RootNamespace => _rootNamespace; - - protected string BuildConfiguration => _buildConfiguration; - - protected BuildToolsConfig Config { get; private set; } protected BuildEnvironment Environment { get; private set; } @@ -28,21 +17,8 @@ public void Execute(GeneratorExecutionContext context) { GeneratorContext = context; - if (!TryGet(context, "MSBuildProjectName", ref _projectName) - || !TryGet(context, "RootNamespace", ref _rootNamespace) - || !TryGet(context, "Configuration", ref _buildConfiguration) - || !TryGet(context, "TargetFrameworkIdentifier", ref _targetFrameworkAssembly)) - return; - - var buildToolsConfig = context.AdditionalFiles.FirstOrDefault(x => Path.GetFileName(x.Path) == Constants.BuildToolsConfigFileName); - if (buildToolsConfig is null) - return; - - var json = buildToolsConfig.GetText().ToString(); - Config = JsonSerializer.Deserialize(json, ConfigHelper.GetSerializerSettings()); - var buildToolsEnvFile = GeneratorContext.AdditionalFiles.FirstOrDefault(x => Path.GetFileName(x.Path) == Constants.BuildToolsEnvironmentSettings); - json = buildToolsEnvFile.GetText().ToString(); + var json = buildToolsEnvFile.GetText().ToString(); Environment = JsonSerializer.Deserialize(json, new JsonSerializerOptions(JsonSerializerDefaults.General)) ?? new BuildEnvironment(); try @@ -51,14 +27,14 @@ public void Execute(GeneratorExecutionContext context) } catch (Exception ex) { - if(Config.Debug) + if (Environment.Debug) context.ReportDiagnostic (Diagnostic.Create( new DiagnosticDescriptor( "MBT500", "DEBUG - Unhandled Error", "An Unhandled Generator Error Occurred: {0} - {1}", - "DEBUG", + "DEBUG", DiagnosticSeverity.Error, true), null, @@ -68,17 +44,6 @@ public void Execute(GeneratorExecutionContext context) protected abstract void Generate(); - private bool TryGet(GeneratorExecutionContext context, string name, ref string value) - { - if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue($"build_property.{name}", out value) && !string.IsNullOrEmpty(value)) - { - return true; - } - - context.ReportDiagnostic(Diagnostic.Create(Descriptors.MissingMSBuildProperty, null, name)); - return false; - } - public void Initialize(GeneratorInitializationContext context) { // Intentionally Left Empty diff --git a/src/Mobile.BuildTools.Core/Mobile.BuildTools.Core.targets b/src/Mobile.BuildTools.Core/Mobile.BuildTools.Core.targets index 54b70749..80d21dcb 100644 --- a/src/Mobile.BuildTools.Core/Mobile.BuildTools.Core.targets +++ b/src/Mobile.BuildTools.Core/Mobile.BuildTools.Core.targets @@ -56,6 +56,7 @@ () }; if (config.Configuration.AppSettings is not null && config.Configuration.AppSettings.TryGetValue(ProjectName, out var settings) && settings.Any()) { + environment.GeneratedClasses = settings; var env = EnvironmentAnalyzer.GatherEnvironmentVariables(this); if (env.Count > 0) { diff --git a/src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs b/src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs index 6a7ae1e6..fd52a97f 100644 --- a/src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs +++ b/src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs @@ -1,9 +1,14 @@ using System.Collections.Generic; +using System.Text.Json.Serialization; +using Mobile.BuildTools.Models.Settings; namespace Mobile.BuildTools.Utils { public class BuildEnvironment { + public bool Debug { get; set; } + public string ProjectName { get; set; } + public string RootNamespace { get; set; } public bool IsCI { get; set; } public bool IsAppCenter { get; set; } public bool IsAppVeyor { get; set; } @@ -16,7 +21,10 @@ public class BuildEnvironment public bool IsBuildHost { get; set; } public string BuildNumber { get; set; } public string BuildConfiguration { get; set; } + + [JsonConverter(typeof(JsonStringEnumConverter))] public Platform TargetPlatform { get; set; } + public IEnumerable GeneratedClasses { get; set; } = []; public IDictionary Environment { get; set; } = new Dictionary(); } } diff --git a/tests/Mobile.BuildTools.AppSettings.Tests/Sources/AddsSimpleProperty/buildtools.env b/tests/Mobile.BuildTools.AppSettings.Tests/Sources/AddsSimpleProperty/buildtools.env index af10b708..8c250418 100644 --- a/tests/Mobile.BuildTools.AppSettings.Tests/Sources/AddsSimpleProperty/buildtools.env +++ b/tests/Mobile.BuildTools.AppSettings.Tests/Sources/AddsSimpleProperty/buildtools.env @@ -1,5 +1,19 @@ -{ - "Environment": { - "AProperty": "Hello World" - } +{ + "ProjectName": "TestProject", + "RootNamespace": "TestProject", + "BuildConfiguration": "Debug", + "TargetPlatform": "Unsupported", + "GeneratedClasses": [ + { + "properties": [ + { + "name": "AProperty", + "type": "String" + } + ] + } + ], + "Environment": { + "AProperty": "Hello World" + } } diff --git a/tests/Mobile.BuildTools.AppSettings.Tests/Sources/UsesSettingsOverDefault/buildtools.env b/tests/Mobile.BuildTools.AppSettings.Tests/Sources/UsesSettingsOverDefault/buildtools.env index fa2fb35b..abef8932 100644 --- a/tests/Mobile.BuildTools.AppSettings.Tests/Sources/UsesSettingsOverDefault/buildtools.env +++ b/tests/Mobile.BuildTools.AppSettings.Tests/Sources/UsesSettingsOverDefault/buildtools.env @@ -10,6 +10,21 @@ "IsTravisCI": false, "IsBuildHost": false, "BuildNumber": null, + "ProjectName": "TestProject", + "RootNamespace": "TestProject", + "BuildConfiguration": "Debug", + "TargetPlatform": "Unsupported", + "GeneratedClasses": [ + { + "properties": [ + { + "name": "ClientId", + "type": "String", + "defaultValue": "Hello World" + } + ] + } + ], "Environment": { "SESSIONNAME": "Console", "ProgramFiles(x86)": "C:\\Program Files (x86)", @@ -28,4 +43,4 @@ "MSBuildLoadMicrosoftTargetsReadOnly": "true", "ClientId": "Hello Settings" } -} +}