Skip to content

Commit 845779a

Browse files
Add IsQuietMode setting (#19)
Addresses #12
1 parent 1eebe91 commit 845779a

File tree

6 files changed

+385
-60
lines changed

6 files changed

+385
-60
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ In this example:
155155
- `Arm`: Inputs are Azure ARM template parameter files with a `.json` extension.
156156
- `Yaml`: Inputs are YAML files with a `.yml` or `.yaml` extension.
157157
- **AdditionalProperties** *(optional)*: A collection of custom top-level properties to inject into the final output. Use the `ItemGroup` syntax to pass key-value pairs.
158+
- **IsQuietMode** *(optional, default=false)*: Only emits warning and error level logs if true.
158159

159160
## Example YAML Input
160161

src/Task/AggregateConfig.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ namespace AggregateConfigBuildTask
1414
public class AggregateConfig : Task
1515
{
1616
private readonly IFileSystem fileSystem;
17+
private ITaskLogger logger;
1718

19+
/* Start incoming properties */
1820
[Required]
1921
public string InputDirectory { get; set; }
2022

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

3133
public string[] AdditionalProperties { get; set; }
3234

35+
public bool IsQuietMode
36+
{
37+
get
38+
{
39+
return logger is QuietTaskLogger;
40+
}
41+
set
42+
{
43+
logger = value && !(logger is QuietTaskLogger) ? new QuietTaskLogger(Log) : logger;
44+
}
45+
}
46+
/* End incoming properties */
47+
3348
public AggregateConfig()
3449
{
3550
this.fileSystem = new FileSystem();
51+
this.logger = new TaskLogger(Log);
3652
}
3753

38-
internal AggregateConfig(IFileSystem fileSystem)
54+
internal AggregateConfig(IFileSystem fileSystem, ITaskLogger logger)
3955
{
4056
this.fileSystem = fileSystem;
57+
this.logger = logger;
4158
}
4259

4360
public override bool Execute()
@@ -51,47 +68,47 @@ public override bool Execute()
5168
if (!Enum.TryParse(OutputType, true, out OutputType outputType) ||
5269
!Enum.IsDefined(typeof(OutputType), outputType))
5370
{
54-
Log.LogError("Invalid OutputType: {0}. Available options: {1}", OutputType, string.Join(", ", Enum.GetNames(typeof(OutputType))));
71+
logger.LogError("Invalid OutputType: {0}. Available options: {1}", OutputType, string.Join(", ", Enum.GetNames(typeof(OutputType))));
5572
return false;
5673
}
5774

5875
InputType inputType = Contracts.InputType.Yaml;
5976
if (!string.IsNullOrEmpty(InputType) &&
6077
(!Enum.TryParse(InputType, true, out inputType) || !Enum.IsDefined(typeof(InputType), inputType)))
6178
{
62-
Log.LogError("Invalid InputType: {0}. Available options: {1}", InputType, string.Join(", ", Enum.GetNames(typeof(InputType))));
79+
logger.LogError("Invalid InputType: {0}. Available options: {1}", InputType, string.Join(", ", Enum.GetNames(typeof(InputType))));
6380
return false;
6481
}
6582

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

6885
string directoryPath = Path.GetDirectoryName(OutputFile);
6986
if (!fileSystem.DirectoryExists(directoryPath))
7087
{
7188
fileSystem.CreateDirectory(directoryPath);
7289
}
7390

74-
var finalResult = ObjectManager.MergeFileObjects(InputDirectory, inputType, AddSourceProperty, fileSystem, Log).GetAwaiter().GetResult();
91+
var finalResult = ObjectManager.MergeFileObjects(InputDirectory, inputType, AddSourceProperty, fileSystem, logger).GetAwaiter().GetResult();
7592

7693
if (finalResult == null)
7794
{
78-
Log.LogError("No input was found! Check the input directory.");
95+
logger.LogError("No input was found! Check the input directory.");
7996
return false;
8097
}
8198

8299
var additionalPropertiesDictionary = JsonHelper.ParseAdditionalProperties(AdditionalProperties);
83-
finalResult = ObjectManager.InjectAdditionalProperties(finalResult, additionalPropertiesDictionary, Log).GetAwaiter().GetResult();
100+
finalResult = ObjectManager.InjectAdditionalProperties(finalResult, additionalPropertiesDictionary, logger).GetAwaiter().GetResult();
84101

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

89106
return true;
90107
}
91108
catch (Exception ex)
92109
{
93-
Log.LogError("An unknown exception occurred: {0}", ex.Message);
94-
Log.LogErrorFromException(ex, true, true, null);
110+
logger.LogError("An unknown exception occurred: {0}", ex.Message);
111+
logger.LogErrorFromException(ex, true, true, null);
95112
return false;
96113
}
97114
}
@@ -103,7 +120,7 @@ private void EmitHeader()
103120
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
104121
.InformationalVersion;
105122

106-
Log.LogMessage(MessageImportance.High, $"AggregateConfig Version: {informationalVersion}");
123+
logger.LogMessage(MessageImportance.Normal, $"AggregateConfig Version: {informationalVersion}");
107124
}
108125
}
109126
}

src/Task/ObjectManager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using AggregateConfigBuildTask.Contracts;
22
using AggregateConfigBuildTask.FileHandlers;
33
using Microsoft.Build.Framework;
4-
using Microsoft.Build.Utilities;
54
using System;
65
using System.Collections.Concurrent;
76
using System.Collections.Generic;
@@ -14,7 +13,11 @@ namespace AggregateConfigBuildTask
1413
{
1514
internal static class ObjectManager
1615
{
17-
public static async Task<JsonElement?> MergeFileObjects(string fileObjectDirectoryPath, InputType inputType, bool addSourceProperty, IFileSystem fileSystem, TaskLoggingHelper log)
16+
public static async Task<JsonElement?> MergeFileObjects(string fileObjectDirectoryPath,
17+
InputType inputType,
18+
bool addSourceProperty,
19+
IFileSystem fileSystem,
20+
ITaskLogger log)
1821
{
1922
var finalResults = new ConcurrentBag<JsonElement>();
2023
JsonElement? finalResult = null;
@@ -177,7 +180,7 @@ public static async Task<JsonElement> MergeObjects(JsonElement? obj1, JsonElemen
177180
/// <param name="additionalPropertiesDictionary">A dictionary of additional properties to inject.</param>
178181
/// <param name="log">Logger reference.</param>
179182
/// <returns>True if the properties were successfully injected, false otherwise.</returns>
180-
public static async Task<JsonElement?> InjectAdditionalProperties(JsonElement? finalResult, Dictionary<string, string> additionalPropertiesDictionary, TaskLoggingHelper log)
183+
public static async Task<JsonElement?> InjectAdditionalProperties(JsonElement? finalResult, Dictionary<string, string> additionalPropertiesDictionary, ITaskLogger log)
181184
{
182185
if (additionalPropertiesDictionary?.Count > 0)
183186
{

0 commit comments

Comments
 (0)