Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ In this example:
- `Arm`: Inputs are Azure ARM template parameter files with a `.json` extension.
- `Yaml`: Inputs are YAML files with a `.yml` or `.yaml` extension.
- **AdditionalProperties** *(optional)*: A collection of custom top-level properties to inject into the final output. Use the `ItemGroup` syntax to pass key-value pairs.
- **IsQuietMode** *(optional, default=false)*: Only emits warning and error level logs if true.

## Example YAML Input

Expand Down
39 changes: 28 additions & 11 deletions src/Task/AggregateConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace AggregateConfigBuildTask
public class AggregateConfig : Task
{
private readonly IFileSystem fileSystem;
private ITaskLogger logger;

/* Start incoming properties */
[Required]
public string InputDirectory { get; set; }

Expand All @@ -30,14 +32,29 @@ public class AggregateConfig : Task

public string[] AdditionalProperties { get; set; }

public bool IsQuietMode
{
get
{
return logger is QuietTaskLogger;
}
set
{
logger = value && !(logger is QuietTaskLogger) ? new QuietTaskLogger(Log) : logger;
}
}
/* End incoming properties */

public AggregateConfig()
{
this.fileSystem = new FileSystem();
this.logger = new TaskLogger(Log);
}

internal AggregateConfig(IFileSystem fileSystem)
internal AggregateConfig(IFileSystem fileSystem, ITaskLogger logger)
{
this.fileSystem = fileSystem;
this.logger = logger;
}

public override bool Execute()
Expand All @@ -51,47 +68,47 @@ public override bool Execute()
if (!Enum.TryParse(OutputType, true, out OutputType outputType) ||
!Enum.IsDefined(typeof(OutputType), outputType))
{
Log.LogError("Invalid OutputType: {0}. Available options: {1}", OutputType, string.Join(", ", Enum.GetNames(typeof(OutputType))));
logger.LogError("Invalid OutputType: {0}. Available options: {1}", OutputType, string.Join(", ", Enum.GetNames(typeof(OutputType))));
return false;
}

InputType inputType = Contracts.InputType.Yaml;
if (!string.IsNullOrEmpty(InputType) &&
(!Enum.TryParse(InputType, true, out inputType) || !Enum.IsDefined(typeof(InputType), inputType)))
{
Log.LogError("Invalid InputType: {0}. Available options: {1}", InputType, string.Join(", ", Enum.GetNames(typeof(InputType))));
logger.LogError("Invalid InputType: {0}. Available options: {1}", InputType, string.Join(", ", Enum.GetNames(typeof(InputType))));
return false;
}

Log.LogMessage(MessageImportance.High, "Aggregating {0} to {1} in folder {2}", inputType, outputType, InputDirectory);
logger.LogMessage(MessageImportance.High, "Aggregating {0} to {1} in folder {2}", inputType, outputType, InputDirectory);

string directoryPath = Path.GetDirectoryName(OutputFile);
if (!fileSystem.DirectoryExists(directoryPath))
{
fileSystem.CreateDirectory(directoryPath);
}

var finalResult = ObjectManager.MergeFileObjects(InputDirectory, inputType, AddSourceProperty, fileSystem, Log).GetAwaiter().GetResult();
var finalResult = ObjectManager.MergeFileObjects(InputDirectory, inputType, AddSourceProperty, fileSystem, logger).GetAwaiter().GetResult();

if (finalResult == null)
{
Log.LogError("No input was found! Check the input directory.");
logger.LogError("No input was found! Check the input directory.");
return false;
}

var additionalPropertiesDictionary = JsonHelper.ParseAdditionalProperties(AdditionalProperties);
finalResult = ObjectManager.InjectAdditionalProperties(finalResult, additionalPropertiesDictionary, Log).GetAwaiter().GetResult();
finalResult = ObjectManager.InjectAdditionalProperties(finalResult, additionalPropertiesDictionary, logger).GetAwaiter().GetResult();

var writer = FileHandlerFactory.GetOutputWriter(fileSystem, outputType);
writer.WriteOutput(finalResult, OutputFile);
Log.LogMessage(MessageImportance.High, "Wrote aggregated configuration file to {0}", OutputFile);
logger.LogMessage(MessageImportance.High, "Wrote aggregated configuration file to {0}", OutputFile);

return true;
}
catch (Exception ex)
{
Log.LogError("An unknown exception occurred: {0}", ex.Message);
Log.LogErrorFromException(ex, true, true, null);
logger.LogError("An unknown exception occurred: {0}", ex.Message);
logger.LogErrorFromException(ex, true, true, null);
return false;
}
}
Expand All @@ -103,7 +120,7 @@ private void EmitHeader()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion;

Log.LogMessage(MessageImportance.High, $"AggregateConfig Version: {informationalVersion}");
logger.LogMessage(MessageImportance.Normal, $"AggregateConfig Version: {informationalVersion}");
}
}
}
9 changes: 6 additions & 3 deletions src/Task/ObjectManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AggregateConfigBuildTask.Contracts;
using AggregateConfigBuildTask.FileHandlers;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -14,7 +13,11 @@ namespace AggregateConfigBuildTask
{
internal static class ObjectManager
{
public static async Task<JsonElement?> MergeFileObjects(string fileObjectDirectoryPath, InputType inputType, bool addSourceProperty, IFileSystem fileSystem, TaskLoggingHelper log)
public static async Task<JsonElement?> MergeFileObjects(string fileObjectDirectoryPath,
InputType inputType,
bool addSourceProperty,
IFileSystem fileSystem,
ITaskLogger log)
{
var finalResults = new ConcurrentBag<JsonElement>();
JsonElement? finalResult = null;
Expand Down Expand Up @@ -177,7 +180,7 @@ public static async Task<JsonElement> MergeObjects(JsonElement? obj1, JsonElemen
/// <param name="additionalPropertiesDictionary">A dictionary of additional properties to inject.</param>
/// <param name="log">Logger reference.</param>
/// <returns>True if the properties were successfully injected, false otherwise.</returns>
public static async Task<JsonElement?> InjectAdditionalProperties(JsonElement? finalResult, Dictionary<string, string> additionalPropertiesDictionary, TaskLoggingHelper log)
public static async Task<JsonElement?> InjectAdditionalProperties(JsonElement? finalResult, Dictionary<string, string> additionalPropertiesDictionary, ITaskLogger log)
{
if (additionalPropertiesDictionary?.Count > 0)
{
Expand Down
Loading