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

.NET 7, bicep update, and partial parameters #349

Merged
merged 9 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.x
dotnet-version: 7.x

- name: Setup NuGet
uses: nuget/setup-nuget@v1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
4 changes: 2 additions & 2 deletions src/Analyzer.BicepProcessor/Analyzer.BicepProcessor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>Microsoft.Azure.Templates.Analyzer.BicepProcessor</AssemblyName>
<RootNamespace>Microsoft.Azure.Templates.Analyzer.BicepProcessor</RootNamespace>
</PropertyGroup>
Expand All @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Bicep.Core" Version="0.14.85" />
<PackageReference Include="Azure.Bicep.Core" Version="0.22.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down
93 changes: 50 additions & 43 deletions src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
using System.IO.Abstractions;
using System.Linq;
using System.Text.RegularExpressions;
using Bicep.Core;
using Bicep.Core.Analyzers.Interfaces;
using Bicep.Core.Analyzers.Linter;
using Bicep.Core.Analyzers.Linter.ApiVersions;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.Emit;
Expand All @@ -16,27 +17,47 @@
using Bicep.Core.FileSystem;
using Bicep.Core.Registry;
using Bicep.Core.Registry.Auth;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.Syntax;
using Bicep.Core.Text;
using Bicep.Core.TypeSystem.Az;
using Bicep.Core.Workspaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.WindowsAzure.ResourceStack.Common.Extensions;
using IOFileSystem = System.IO.Abstractions.FileSystem;

namespace Microsoft.Azure.Templates.Analyzer.BicepProcessor
{
/// <summary>
/// Contains functionality to process Bicep templates.
/// </summary>
public class BicepTemplateProcessor
public static class BicepTemplateProcessor
{
private static readonly Regex IsModuleRegistryPathRegex = new("^br(:[\\w.]+\\/|\\/public:)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
/// <summary>
/// DI Helper from Bicep.Cli.Helpers.ServiceCollectionExtensions
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddBicepCore(this IServiceCollection services) => services
.AddSingleton<INamespaceProvider, DefaultNamespaceProvider>()
.AddSingleton<IAzResourceTypeLoader, AzResourceTypeLoader>()
.AddSingleton<IAzResourceTypeLoaderFactory, AzResourceTypeLoaderFactory>()
.AddSingleton<IContainerRegistryClientFactory, ContainerRegistryClientFactory>()
.AddSingleton<ITemplateSpecRepositoryFactory, TemplateSpecRepositoryFactory>()
.AddSingleton<IModuleDispatcher, ModuleDispatcher>()
.AddSingleton<IArtifactRegistryProvider, DefaultArtifactRegistryProvider>()
.AddSingleton<ITokenCredentialFactory, TokenCredentialFactory>()
.AddSingleton<IFileResolver, FileResolver>()
.AddSingleton<IFileSystem, IOFileSystem>()
.AddSingleton<IConfigurationManager, ConfigurationManager>()
.AddSingleton<IBicepAnalyzer, LinterAnalyzer>()
.AddSingleton<FeatureProviderFactory>() // needed for below
.AddSingleton<IFeatureProviderFactory, SourceMapFeatureProviderFactory>() // enable source mapping
.AddSingleton<ILinterRulesProvider, LinterRulesProvider>()
.AddSingleton<BicepCompiler>();

private static readonly IConfigurationManager configurationManager = new ConfigurationManager(new FileSystem());
private static readonly IFileResolver fileResolver = new FileResolver(new FileSystem());
private static readonly INamespaceProvider namespaceProvider = new DefaultNamespaceProvider(new AzResourceTypeLoader());
private static readonly ITokenCredentialFactory tokenCredentialFactory = new TokenCredentialFactory();
private static readonly Regex IsModuleRegistryPathRegex = new("^br(:[\\w.]+\\/|\\/public:)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static BicepCompiler BicepCompiler = null;

/// <summary>
/// Converts Bicep template into JSON template and returns it as a string and its source map
Expand All @@ -45,36 +66,17 @@ public class BicepTemplateProcessor
/// <returns>The compiled template as a <c>JSON</c> string and its source map.</returns>
public static (string, BicepMetadata) ConvertBicepToJson(string bicepPath)
{
var bicepPathUri = new Uri(bicepPath);
using var stringWriter = new StringWriter();

var configuration = configurationManager.GetConfiguration(bicepPathUri);

var featureProviderFactory = IFeatureProviderFactory.WithStaticFeatureProvider(
new SourceMapFeatureProvider(new FeatureProvider(configuration)));
var apiVersionProvider = new ApiVersionProvider(
featureProviderFactory.GetFeatureProvider(bicepPathUri), namespaceProvider);
var apiVersionProviderFactory = IApiVersionProviderFactory.WithStaticApiVersionProvider(apiVersionProvider);

var moduleDispatcher = new ModuleDispatcher(
new DefaultModuleRegistryProvider(
fileResolver,
new ContainerRegistryClientFactory(tokenCredentialFactory),
new TemplateSpecRepositoryFactory(tokenCredentialFactory),
featureProviderFactory,
configurationManager),
configurationManager);
var workspace = new Workspace();
var sourceFileGrouping = SourceFileGroupingBuilder.Build(fileResolver, moduleDispatcher, workspace, PathHelper.FilePathToFileUrl(bicepPath));

// Pull modules optimistically
if (moduleDispatcher.RestoreModules(moduleDispatcher.GetValidModuleReferences(sourceFileGrouping.GetModulesToRestore())).Result)
if (BicepCompiler == null)
{
// Modules had to be restored - recompile
sourceFileGrouping = SourceFileGroupingBuilder.Rebuild(moduleDispatcher, workspace, sourceFileGrouping);
var serviceCollection = new ServiceCollection();
serviceCollection.AddBicepCore();

var services = serviceCollection.BuildServiceProvider();
BicepCompiler = services.GetRequiredService<BicepCompiler>();
}

var compilation = new Compilation(featureProviderFactory, namespaceProvider, sourceFileGrouping, configurationManager, apiVersionProviderFactory, new LinterAnalyzer());
using var stringWriter = new StringWriter();
var compilation = BicepCompiler.CreateCompilation(new Uri(bicepPath)).Result;
var emitter = new TemplateEmitter(compilation.GetEntrypointSemanticModel());
var emitResult = emitter.Emit(stringWriter);

Expand All @@ -87,27 +89,32 @@ public static (string, BicepMetadata) ConvertBicepToJson(string bicepPath)
}

string GetPathRelativeToEntryPoint(string absolutePath) => Path.GetRelativePath(
Path.GetDirectoryName(sourceFileGrouping.EntryPoint.FileUri.AbsolutePath), absolutePath);
Path.GetDirectoryName(compilation.SourceFileGrouping.EntryPoint.FileUri.AbsolutePath), absolutePath);

// Collect all needed module info from sourceFileGrouping metadata
var moduleInfo = sourceFileGrouping.UriResultByModule.Select(kvp =>
// Collect all needed module info from SourceFileGrouping metadata
var moduleInfo = compilation.SourceFileGrouping.UriResultByArtifactReference.Select(sourceFileAndMetadata =>
{
var bicepSourceFile = kvp.Key as BicepSourceFile;
var bicepSourceFile = sourceFileAndMetadata.Key as BicepSourceFile;
var pathRelativeToEntryPoint = GetPathRelativeToEntryPoint(bicepSourceFile.FileUri.AbsolutePath);
var modules = kvp.Value.Values
.Select(result =>
var modules = sourceFileAndMetadata.Value
.Select(artifactRefAndUriResult =>
{
// Do not include modules imported from public/private registries, as it is more useful for user to see line number
// of the module declaration itself instead of line number in the module as the user does not control template in registry directly
if (result.Statement is not ModuleDeclarationSyntax moduleDeclaration
if (artifactRefAndUriResult.Key is not ModuleDeclarationSyntax moduleDeclaration
|| moduleDeclaration.Path is not StringSyntax moduleDeclarationPath
|| moduleDeclarationPath.SegmentValues.Any(v => IsModuleRegistryPathRegex.IsMatch(v)))
{
return null;
}

if (!artifactRefAndUriResult.Value.IsSuccess())
{
return null;
}

var moduleLine = TextCoordinateConverter.GetPosition(bicepSourceFile.LineStarts, moduleDeclaration.Span.Position).line;
var modulePath = new FileInfo(result.FileUri.AbsolutePath).FullName; // converts path to current platform
var modulePath = new FileInfo(artifactRefAndUriResult.Value.Unwrap().AbsolutePath).FullName; // converts path to current platform

// Use relative paths for bicep to match file paths used in bicep modules and source map
if (modulePath.EndsWith(".bicep"))
Expand Down
47 changes: 41 additions & 6 deletions src/Analyzer.BicepProcessor/SourceMapFeatureProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
using Bicep.Core.Features;
using System;
using Azure.ResourceManager.Resources;
using Bicep.Core.Features;

namespace Microsoft.Azure.Templates.Analyzer.BicepProcessor
{
/// <summary>
/// Helper class that enables source mapping feature in Bicep.Core
/// </summary>
public class SourceMapFeatureProviderFactory : IFeatureProviderFactory
{
private readonly IFeatureProviderFactory factory;

/// <inheritdoc/>
public SourceMapFeatureProviderFactory(FeatureProviderFactory factory)
{
this.factory = factory;
}

/// <inheritdoc/>
public IFeatureProvider GetFeatureProvider(Uri templateUri)
=> new SourceMapFeatureProvider(this.factory.GetFeatureProvider(templateUri));
}

/// <summary>
/// Helper class that enables source mapping feature in Bicep.Core
/// </summary>
Expand All @@ -21,9 +41,6 @@ public SourceMapFeatureProvider(IFeatureProvider features)
/// <inheritdoc/>
public string CacheRootDirectory => features.CacheRootDirectory;

/// <inheritdoc/>
public bool RegistryEnabled => features.RegistryEnabled;

/// <inheritdoc/>
public bool SymbolicNameCodegenEnabled => features.SymbolicNameCodegenEnabled;

Expand All @@ -37,9 +54,27 @@ public SourceMapFeatureProvider(IFeatureProvider features)
public bool SourceMappingEnabled => true;

/// <inheritdoc/>
public bool ParamsFilesEnabled => features.ParamsFilesEnabled;
public bool UserDefinedFunctionsEnabled => features.UserDefinedFunctionsEnabled;

/// <inheritdoc/>
public bool DynamicTypeLoadingEnabled => features.DynamicTypeLoadingEnabled;

/// <inheritdoc/>
public bool PrettyPrintingEnabled => features.PrettyPrintingEnabled;

/// <inheritdoc/>
public bool TestFrameworkEnabled => features.TestFrameworkEnabled;

/// <inheritdoc/>
public bool AssertsEnabled => features.AssertsEnabled;

/// <inheritdoc/>
public bool CompileTimeImportsEnabled => features.CompileTimeImportsEnabled;

/// <inheritdoc/>
public bool MicrosoftGraphPreviewEnabled => features.MicrosoftGraphPreviewEnabled;

/// <inheritdoc/>
public bool UserDefinedTypesEnabled => features.UserDefinedTypesEnabled;
public bool PublishSourceEnabled => features.PublishSourceEnabled;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
2 changes: 1 addition & 1 deletion src/Analyzer.Cli/Analyzer.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>TemplateAnalyzer</AssemblyName>
<Description>A command line interface for Microsoft.Azure.Templates.Analyzer.Core - an ARM and Bicep template scanner for security misconfigurations and best practices</Description>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
2 changes: 1 addition & 1 deletion src/Analyzer.Core/Analyzer.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>Microsoft.Azure.Templates.Analyzer</RootNamespace>
<AssemblyName>Microsoft.Azure.Templates.Analyzer.Core</AssemblyName>
<Description>An ARM and Bicep template scanner for security misconfigurations and best practices</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzer.Core/TemplateAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private IEnumerable<IEvaluation> AnalyzeAllIncludedTemplates(string populatedTem

try
{
templatejObject = armTemplateProcessor.ProcessTemplate(parameters);
templatejObject = armTemplateProcessor.ProcessTemplate(parameters, parentContext.IsMainTemplate);
nonik0 marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>Microsoft.Azure.Templates.Analyzer.JsonRuleEngine</AssemblyName>
<RootNamespace>Microsoft.Azure.Templates.Analyzer.RuleEngines.JsonEngine</RootNamespace>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>Microsoft.Azure.Templates.Analyzer.PowerShellRuleEngine</AssemblyName>
<RootNamespace>Microsoft.Azure.Templates.Analyzer.RuleEngines.PowerShellEngine</RootNamespace>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
2 changes: 1 addition & 1 deletion src/Analyzer.Reports/Analyzer.Reports.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>Microsoft.Azure.Templates.Analyzer.Reports</AssemblyName>
<RootNamespace>Microsoft.Azure.Templates.Analyzer.Reports</RootNamespace>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand Down
Loading
Loading