We welcome community contributions to Template Analyzer. Please note that by participating in this project, you agree to abide by the Microsoft Open Source Code of Conduct and terms of the CLA.
- If you haven't already, you will need to install the .NET 6 SDK to build and run this project.
- Fork this repo (see this forking guide for more information).
- Checkout the repo locally with
git clone git@github.com:{your_username}/template-analyzer.git
. - The .NET solution can be built with the
dotnet build
command.
- Visual Studio Code with the C# extension is a great way to get started.
- Start VS Code and open the root directory where the code is cloned locally (File->Open Folder...).
- Run the
build
task (Terminal->Run Task...->build) in VS Code to build Template Analyzer. - Try analyzing a template: open a template file in VS Code and then run the
Launch CLI on Template
launch configuration (Run->Start Debugging).- Alternatively, modify the configuration in launch.json and specify a path to a template file, and optionally specify a path to a parameters file to use.
- Additionally, the
Launch CLI on Directory
launch configuration analyzes the directory of the open file in VS Code.
The Template Analyzer dotnet solution (src\TemplateAnalyzer.sln) is comprised of the following main components:
- CLI (src\Analyzer.Cli): The command-line tool to execute Template Analyzer. This executable will pass template files to Analyzer.Core.
- Core (src\Analyzer.Core): The main Analyzer library which executes all rule engines against provided templates.
- BuiltInRules.json (src\Analyzer.Core\Rules\BuiltInRules.json): The file with the built-in Template Analyzer rules.
- Template Processor (src\Analyzer.TemplateProcessor) and Bicep Processor (src\Analyzer.BicepProcessor): These libraries parse ARM JSON and Bicep templates and evaluate expressions found in the templates.
- JSON Rule Engine (src\Analyzer.JsonRuleEngine): The library dedicated to parse and evaluate the Template Analyzer JSON rules.
- PowerShell Rule Engine (src\Analyzer.PowerShellRuleEngine): Runs PowerShell-based rules using PSRule for Azure.
- There are two .nuspec files that define NuGet packages that can be created
- src\Analyzer.Core.NuGet\Analyzer.Core.nuspec for packing Analyzer Core into package Azure.Templates.Analyzer.Core.
- Requires building src\Analyzer.Core first.
- src\Analyzer.Cli.NuGet\Analyzer.Cli.nuspec for packing Analyzer Cli into package Azure.Templates.Analyzer.CommandLine.<platform>.
- Requires publishing the CLI first - the
Publish
task in VS Code can be used for this (Terminal->Run Task...->Publish). The platform is chosen when running the task.
- Requires publishing the CLI first - the
- src\Analyzer.Core.NuGet\Analyzer.Core.nuspec for packing Analyzer Core into package Azure.Templates.Analyzer.Core.
- These can be packed (after building/publishing) using the nuget.exe CLI
- Example:
nuget pack <nuspec-file> -Version <version> -Properties Configuration=<Debug|Release>
- Use
Debug
orRelease
depending on the Configuration the solution or projects were built with (Debug
is the default) - Analyzer.Cli also needs the runtime specified (this is selected when running the
Publish
task in VS Code). The value for the-Properties
argument should append;runtimeId=<id-for-platform>
. - This is a great way to test NuGet consumption of local changes
- Example:
- Analyzer CLI (or another calling application) identifies JSON files (template and parameter files) and invokes Analyzer Core.
- Analyzer Core calls the Template Processing Library to process the template and (if supplied) the provided parameters. The Template Processing Library processes all the template functions.
- Analyzer Core then calls the JSON and PowerShell Rule Engines, which evaluate each of their rules against the template/parameter pairs.
- JSON Rule Engine evaluates the expressions specified in the
evaluation
section of the rule and generates results to identify the rule violation in the template. - PowerShell Rule Engine invokes PSRule to run Azure rules against the template.
- Use the
dotnet test
command to run the full Template Analyzer test suite. - If using VS Code, run the tests with the
test
task (Terminal->Run Task...->test).
Please follow the below conventions when contributing to this project.
-
Using directives:
- Should be listed alphabetically by namespace
System.*
namespaces should be listed first- Namespaces can be grouped by top-level name, but should be alphabetical within the group
-
Follow the below naming conventions:
Components Casing Class names, properties, and methods PascalCase Private class variables camelCase -
All internal and public classes, methods, and properties should include a
/// <summary>
comment- Add
<param>
and<returns>
when applicable - Example:
/// <summary> /// Runs the TemplateAnalyzer logic given the template and parameters passed to it /// </summary> /// <param name="template">The ARM Template <c>JSON</c>. Must follow this schema: https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#</param> /// <param name="parameters">The parameters for the ARM Template <c>JSON</c></param> /// <returns>List of TemplateAnalyzer results</returns> public static IEnumerable<IResult> Run(string template, string parameters = null) { … }
- Add
High test code coverage is required to contribute to this project. This ensures the highest code quality. At least 80% test code coverage is required. This project uses Microsoft.VisualStudio.TestTools.UnitTesting for its tests. Please follow the below conventions when contributing to this project.
- Each .NET project should have its corresponding test project
- Each class should have its corresponding test class
- Each internal and public function in the class should be tested in the unit test class
- Follow the below naming conventions:
- Test Project: {project name}.(Unit/Functional)Tests selecting the appropriate type of tests found in the project
- Test Class: {class name}Tests.cs
- Test methods:
- (Data)TestMethod:
{method name}_{what is being tested}_{expected outcome}
- DataRow (display name): Short description that clearly differentiate between the DataRows
- (Data)TestMethod:
Review the Authoring JSON Rules section to write new built-in Template Analyzer rules. Information on the new rules would also have to be added in Built-in Rules, in alphabetical order. Thorough tests are required as well - documentation on how to use the framework for testing rules can be found here.
- The changes cannot break any existing functional/unit tests that are part of the central repository.
- This includes all tests, even those not associated with the given feature area.
- Code submitted must have basic unit test coverage, and have all the unit tests pass. Functional tests are encouraged.
- Code submitted must have all the built-in rule tests pass. If applicable, new rules should add tests for built-in rules.
- Code should be commented.
- Code should pass all relevant static checks and coding guidelines.
- All build warnings and code analysis warnings should be fixed prior to submission.
- Target the
main
branch for your Pull Request. A Pull Request Template will be applied. Fill out the details inside. - Add a short description of what the change is adding/fixing/improving.
- Add yourself as the 'assignee'.
- Add 'linked issues' if relevant.
- Two approvals are required, including at least one Owner of this repository
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.