Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding MSBuild Props to BuildEnvironment file #349

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(ProjectName, Config);
var settings = Environment.GeneratedClasses;
if (settings is null || !settings.Any())
return;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -239,7 +239,7 @@ internal IDictionary<string, string> 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))
Expand Down
41 changes: 3 additions & 38 deletions src/Mobile.BuildTools.AppSettings/Generators/GeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,15 @@ 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; }

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<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 @@ -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,
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Mobile.BuildTools.Core/Mobile.BuildTools.Core.targets
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<EnvironmentSettingsTask ConfigurationPath="$(BuildToolsConfigFilePath)"
ProjectName="$(MSBuildProjectName)"
ProjectDirectory="$(MSBuildProjectDirectory)"
RootNamespace="$(RootNamespace)"
SolutionDirectory="$(SolutionDir)"
Configuration="$(Configuration)"
IntermediateOutputPath="$(IntermediateOutputPath)"
Expand Down
10 changes: 9 additions & 1 deletion src/Mobile.BuildTools.Core/Tasks/EnvironmentSettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Mobile.BuildTools.Tasks;

public class EnvironmentSettingsTask : BuildToolsTaskBase
{
public string RootNamespace { get; set; }

[Output]
public ITaskItem[] EnvironmentSettings { get; private set; } = [];
internal override void ExecuteInternal(IBuildConfiguration config)
Expand All @@ -27,6 +29,9 @@ internal override void ExecuteInternal(IBuildConfiguration config)

var environment = new BuildEnvironment
{
Debug = config.Configuration.Debug,
ProjectName = ProjectName,
RootNamespace = RootNamespace,
BuildNumber = CIBuildEnvironmentUtils.BuildNumber,
IsCI = CIBuildEnvironmentUtils.IsCI,
IsAppCenter = CIBuildEnvironmentUtils.IsAppCenter,
Expand All @@ -39,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
8 changes: 8 additions & 0 deletions src/Mobile.BuildTools.Reference/Utils/BuildEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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<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"
}
}
}
Loading