diff --git a/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs b/src/Mobile.BuildTools.AppSettings/Generators/AppSettingsGenerator.cs index 767a40a9..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(Environment.ProjectName, Config); + var settings = Environment.GeneratedClasses; if (settings is null || !settings.Any()) return; diff --git a/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs b/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs index 38557210..91d0f65e 100644 --- a/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs +++ b/src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs @@ -11,23 +11,14 @@ public abstract class GeneratorBase : ISourceGenerator { protected GeneratorExecutionContext GeneratorContext { get; private set; } - protected BuildToolsConfig Config { get; private set; } - protected BuildEnvironment Environment { get; private set; } public void Execute(GeneratorExecutionContext context) { GeneratorContext = context; - 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 @@ -36,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, @@ -53,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/Tasks/EnvironmentSettingsTask.cs b/src/Mobile.BuildTools.Core/Tasks/EnvironmentSettingsTask.cs index 4a7e5ca8..8becfd8d 100644 --- a/src/Mobile.BuildTools.Core/Tasks/EnvironmentSettingsTask.cs +++ b/src/Mobile.BuildTools.Core/Tasks/EnvironmentSettingsTask.cs @@ -7,8 +7,8 @@ namespace Mobile.BuildTools.Tasks; public class EnvironmentSettingsTask : BuildToolsTaskBase -{ - public string RootNamespace { get; set; } +{ + public string RootNamespace { get; set; } [Output] public ITaskItem[] EnvironmentSettings { get; private set; } = []; @@ -28,8 +28,9 @@ internal override void ExecuteInternal(IBuildConfiguration config) } var environment = new BuildEnvironment - { - ProjectName = ProjectName, + { + Debug = config.Configuration.Debug, + ProjectName = ProjectName, RootNamespace = RootNamespace, BuildNumber = CIBuildEnvironmentUtils.BuildNumber, IsCI = CIBuildEnvironmentUtils.IsCI, @@ -43,13 +44,16 @@ internal override void ExecuteInternal(IBuildConfiguration config) IsTeamCity = CIBuildEnvironmentUtils.IsTeamCity, IsTravisCI = CIBuildEnvironmentUtils.IsTravisCI, BuildConfiguration = config.BuildConfiguration, - TargetPlatform = config.Platform + TargetPlatform = config.Platform, + GeneratedClasses = [], + Environment = new Dictionary() }; 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 a99f61fa..fd52a97f 100644 --- a/src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs +++ b/src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs @@ -1,9 +1,12 @@ 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; } @@ -18,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" } -} +}