|
| 1 | +# AggregateConfigBuildTask |
| 2 | + |
| 3 | +**AggregateConfigBuildTask** is an MSBuild task that aggregates and transforms configuration files (such as YAML) into more consumable formats like JSON, Azure ARM template parameters, or YAML itself during the build process. |
| 4 | + |
| 5 | +## Links |
| 6 | + |
| 7 | +* NuGet.org: https://www.nuget.org/packages/AggregateConfigBuildTask |
| 8 | +* GitHub: https://github.com/richardsondev/AggregateConfigBuildTask |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Merge multiple YAML configuration files into a single output format (JSON, Azure ARM parameters, or YAML). |
| 13 | +- Support for injecting custom metadata (e.g., `ResourceGroup`, `Environment`) into the output. |
| 14 | +- Optionally include the source file name in each configuration entry. |
| 15 | +- Embed output files as resources in the assembly for easy inclusion in your project. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +To install the `AggregateConfigBuildTask` NuGet package, run the following command: |
| 20 | + |
| 21 | +```bash |
| 22 | +dotnet add package AggregateConfigBuildTask |
| 23 | +``` |
| 24 | + |
| 25 | +Alternatively, add the following line to your `.csproj` file: |
| 26 | + |
| 27 | +```xml |
| 28 | +<PackageReference Include="AggregateConfigBuildTask" Version="1.0.0" /> |
| 29 | +``` |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +### Basic Example |
| 34 | + |
| 35 | +In your `.csproj` file, use the task to aggregate YAML files and output them in a specific format. Here’s an example of aggregating YAML files and generating JSON output: |
| 36 | + |
| 37 | +```xml |
| 38 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 39 | + |
| 40 | + <ItemGroup> |
| 41 | + <PackageReference Include="AggregateConfigBuildTask" Version="1.0.0" /> |
| 42 | + </ItemGroup> |
| 43 | + |
| 44 | + <Target Name="AggregateConfigs" BeforeTargets="PrepareForBuild"> |
| 45 | + <ItemGroup> |
| 46 | + <AdditionalProperty Include="ResourceGroup=TestRG" /> |
| 47 | + <AdditionalProperty Include="Environment=Production" /> |
| 48 | + </ItemGroup> |
| 49 | + |
| 50 | + <AggregateConfigBuildTask |
| 51 | + InputDirectory="Configs" |
| 52 | + OutputFile="$(MSBuildProjectDirectory)\out\output.json" |
| 53 | + AddSourceProperty="true" |
| 54 | + OutputType="Json" |
| 55 | + AdditionalProperties="@(AdditionalProperty)" /> |
| 56 | + </Target> |
| 57 | + |
| 58 | +</Project> |
| 59 | +``` |
| 60 | + |
| 61 | +In this example: |
| 62 | +- The `Configs` directory contains the YAML files to be aggregated. |
| 63 | +- The output will be generated as `out/output.json`. |
| 64 | +- The `AddSourceProperty` flag adds the source file name to each configuration entry. |
| 65 | +- The `AdditionalProperties` are injected into the top-level of the output as custom metadata. |
| 66 | + |
| 67 | +### ARM Template Parameters Output Example |
| 68 | + |
| 69 | +You can also generate Azure ARM template parameters. Here's how to modify the configuration to output in the ARM parameter format: |
| 70 | + |
| 71 | +```xml |
| 72 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 73 | + |
| 74 | + <ItemGroup> |
| 75 | + <PackageReference Include="AggregateConfigBuildTask" Version="1.0.0" /> |
| 76 | + </ItemGroup> |
| 77 | + |
| 78 | + <Target Name="AggregateConfigsForARM" BeforeTargets="PrepareForBuild"> |
| 79 | + <ItemGroup> |
| 80 | + <AdditionalProperty Include="ResourceGroup=TestRG" /> |
| 81 | + <AdditionalProperty Include="Environment=Production" /> |
| 82 | + </ItemGroup> |
| 83 | + |
| 84 | + <AggregateConfigBuildTask |
| 85 | + InputDirectory="Configs" |
| 86 | + OutputFile="$(MSBuildProjectDirectory)\out\output.parameters.json" |
| 87 | + OutputType="ArmParameter" |
| 88 | + AdditionalProperties="@(AdditionalProperty)" /> |
| 89 | + </Target> |
| 90 | + |
| 91 | +</Project> |
| 92 | +``` |
| 93 | + |
| 94 | +### YAML Output Example |
| 95 | + |
| 96 | +You can also output the aggregated configuration back into YAML format: |
| 97 | + |
| 98 | +```xml |
| 99 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 100 | + |
| 101 | + <ItemGroup> |
| 102 | + <PackageReference Include="AggregateConfigBuildTask" Version="1.0.0" /> |
| 103 | + </ItemGroup> |
| 104 | + |
| 105 | + <Target Name="AggregateConfigsToYAML" BeforeTargets="PrepareForBuild"> |
| 106 | + <ItemGroup> |
| 107 | + <AdditionalProperty Include="ResourceGroup=TestRG" /> |
| 108 | + <AdditionalProperty Include="Environment=Production" /> |
| 109 | + </ItemGroup> |
| 110 | + |
| 111 | + <AggregateConfigBuildTask |
| 112 | + InputDirectory="Configs" |
| 113 | + OutputFile="$(MSBuildProjectDirectory)\out\output.yaml" |
| 114 | + OutputType="Yaml" |
| 115 | + AdditionalProperties="@(AdditionalProperty)" /> |
| 116 | + </Target> |
| 117 | + |
| 118 | +</Project> |
| 119 | +``` |
| 120 | + |
| 121 | +### Embedding Output Files as Resources |
| 122 | + |
| 123 | +You can embed the output files (such as the generated JSON) as resources in the assembly. This allows them to be accessed from within your code as embedded resources. |
| 124 | + |
| 125 | +```xml |
| 126 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 127 | + |
| 128 | + <ItemGroup> |
| 129 | + <PackageReference Include="AggregateConfigBuildTask" Version="1.0.0" /> |
| 130 | + </ItemGroup> |
| 131 | + |
| 132 | + <Target Name="AggregateConfigs" BeforeTargets="PrepareForBuild"> |
| 133 | + <ItemGroup> |
| 134 | + <AdditionalProperty Include="ResourceGroup=TestRG" /> |
| 135 | + <AdditionalProperty Include="Environment=Production" /> |
| 136 | + </ItemGroup> |
| 137 | + |
| 138 | + <AggregateConfigBuildTask |
| 139 | + InputDirectory="Configs" |
| 140 | + OutputFile="$(MSBuildProjectDirectory)\out\output.json" |
| 141 | + OutputType="Json" |
| 142 | + AdditionalProperties="@(AdditionalProperty)" /> |
| 143 | + </Target> |
| 144 | + |
| 145 | + <!-- Embed output.json as a resource in the assembly --> |
| 146 | + <ItemGroup> |
| 147 | + <EmbeddedResource Include="$(MSBuildProjectDirectory)\out\output.json" /> |
| 148 | + </ItemGroup> |
| 149 | + |
| 150 | +</Project> |
| 151 | +``` |
| 152 | + |
| 153 | +In this example: |
| 154 | +- The generated output file `output.json` is embedded in the resulting assembly as a resource. |
| 155 | +- You can access this resource programmatically using the `System.Reflection` API. |
| 156 | + |
| 157 | +## Parameters |
| 158 | + |
| 159 | +- **InputDirectory** *(required)*: The directory containing YAML files to be aggregated. |
| 160 | +- **OutputFile** *(required)*: The path to the output file. Can be a JSON, ARM parameter, or YAML file. |
| 161 | +- **AddSourceProperty** *(optional, default=false)*: Adds a `source` property to each object in the output, indicating the YAML file it originated from. |
| 162 | +- **OutputType** *(required)*: Determines the output format. Supported values: |
| 163 | + - `Json`: Outputs a regular JSON file. |
| 164 | + - `ArmParameter`: Outputs an Azure ARM template parameter file. |
| 165 | + - `Yaml`: Outputs a YAML file. |
| 166 | +- **AdditionalProperties** *(optional)*: A collection of custom top-level properties to inject into the final output. Use the `ItemGroup` syntax to pass key-value pairs. |
| 167 | + |
| 168 | +## Example YAML Input |
| 169 | + |
| 170 | +Assume you have the following YAML files in the `Configs` directory: |
| 171 | + |
| 172 | +```yaml |
| 173 | +resources: |
| 174 | + - id: "Resource1" |
| 175 | + type: "Compute" |
| 176 | + description: "Main compute resource" |
| 177 | +``` |
| 178 | +
|
| 179 | +```yaml |
| 180 | +resources: |
| 181 | + - id: "Resource2" |
| 182 | + type: "Storage" |
| 183 | + description: "Storage resource" |
| 184 | +``` |
| 185 | +
|
| 186 | +### Output JSON Example |
| 187 | +
|
| 188 | +```json |
| 189 | +{ |
| 190 | + "resources": [ |
| 191 | + { |
| 192 | + "id": "Resource1", |
| 193 | + "type": "Compute", |
| 194 | + "description": "Main compute resource", |
| 195 | + "source": "file1" |
| 196 | + }, |
| 197 | + { |
| 198 | + "id": "Resource2", |
| 199 | + "type": "Storage", |
| 200 | + "description": "Storage resource", |
| 201 | + "source": "file2" |
| 202 | + } |
| 203 | + ], |
| 204 | + "ResourceGroup": "TestRG", |
| 205 | + "Environment": "Production" |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +### ARM Parameter Output Example |
| 210 | + |
| 211 | +```json |
| 212 | +{ |
| 213 | + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", |
| 214 | + "contentVersion": "1.0.0.0", |
| 215 | + "parameters": { |
| 216 | + "resources": { |
| 217 | + "value": [ |
| 218 | + { |
| 219 | + "id": "Resource1", |
| 220 | + "type": "Compute", |
| 221 | + "description": "Main compute resource", |
| 222 | + "source": "file1" |
| 223 | + }, |
| 224 | + { |
| 225 | + "id": "Resource2", |
| 226 | + "type": "Storage", |
| 227 | + "description": "Storage resource", |
| 228 | + "source": "file2" |
| 229 | + } |
| 230 | + ] |
| 231 | + }, |
| 232 | + "ResourceGroup": { |
| 233 | + "value": "TestRG" |
| 234 | + }, |
| 235 | + "Environment": { |
| 236 | + "value": "Production" |
| 237 | + } |
| 238 | + } |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +## License |
| 243 | + |
| 244 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 245 | + |
| 246 | +## Contributing |
| 247 | + |
| 248 | +Contributions are welcome! Feel free to submit issues or pull requests on [GitHub](https://github.com/richardsondev/AggregateConfigBuildTask). |
0 commit comments