Skip to content

Commit

Permalink
tests: fixing analyzer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Dec 21, 2024
1 parent 52d4155 commit cbfb3a9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
26 changes: 3 additions & 23 deletions src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuildToolsConfig>(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<BuildEnvironment>(json, new JsonSerializerOptions(JsonSerializerDefaults.General)) ?? new BuildEnvironment();

try
Expand All @@ -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,
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/Mobile.BuildTools.Core/Tasks/EnvironmentSettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } = [];
Expand All @@ -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,
Expand All @@ -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<string, string>()
};

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)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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<SettingsConfig> GeneratedClasses { get; set; } = [];
public IDictionary<string, string> Environment { get; set; } = new Dictionary<string, string>();
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -28,4 +43,4 @@
"MSBuildLoadMicrosoftTargetsReadOnly": "true",
"ClientId": "Hello Settings"
}
}
}

0 comments on commit cbfb3a9

Please sign in to comment.