forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildCommand.cs
87 lines (74 loc) · 3.14 KB
/
BuildCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Threading.Tasks;
using Bicep.Cli.Arguments;
using Bicep.Cli.Logging;
using Bicep.Cli.Services;
using Bicep.Core.Configuration;
using Bicep.Core.Emit;
using Bicep.Core.Features;
using Bicep.Core.FileSystem;
using Bicep.Core.Workspaces;
using Microsoft.Extensions.Logging;
namespace Bicep.Cli.Commands
{
public class BuildCommand : ICommand
{
private readonly ILogger logger;
private readonly IDiagnosticLogger diagnosticLogger;
private readonly CompilationService compilationService;
private readonly CompilationWriter writer;
private readonly IFeatureProviderFactory featureProviderFactory;
public BuildCommand(
ILogger logger,
IDiagnosticLogger diagnosticLogger,
CompilationService compilationService,
CompilationWriter writer,
IFeatureProviderFactory featureProviderFactory)
{
this.logger = logger;
this.diagnosticLogger = diagnosticLogger;
this.compilationService = compilationService;
this.writer = writer;
this.featureProviderFactory = featureProviderFactory;
}
public async Task<int> RunAsync(BuildArguments args)
{
var inputPath = PathHelper.ResolvePath(args.InputFile);
var features = featureProviderFactory.GetFeatureProvider(PathHelper.FilePathToFileUrl(inputPath));
var emitterSettings = new EmitterSettings(features, BicepSourceFileKind.BicepFile);
if (emitterSettings.EnableSymbolicNames)
{
logger.LogWarning(CliResources.SymbolicNamesDisclaimerMessage);
}
if (features.ResourceTypedParamsAndOutputsEnabled)
{
logger.LogWarning(CliResources.ResourceTypesDisclaimerMessage);
}
if (IsBicepFile(inputPath))
{
diagnosticLogger.SetupFormat(args.DiagnosticsFormat);
var compilation = await compilationService.CompileAsync(inputPath, args.NoRestore);
if (diagnosticLogger.ErrorCount < 1)
{
if (args.OutputToStdOut)
{
writer.ToStdout(compilation);
}
else
{
static string DefaultOutputPath(string path) => PathHelper.GetDefaultBuildOutputPath(path);
var outputPath = PathHelper.ResolveDefaultOutputPath(inputPath, args.OutputDir, args.OutputFile, DefaultOutputPath);
writer.ToFile(compilation, outputPath);
}
}
diagnosticLogger.FlushLog();
// return non-zero exit code on errors
return diagnosticLogger.ErrorCount > 0 ? 1 : 0;
}
logger.LogError(CliResources.UnrecognizedBicepFileExtensionMessage, inputPath);
return 1;
}
private bool IsBicepFile(string inputPath) => PathHelper.HasBicepExtension(PathHelper.FilePathToFileUrl(inputPath));
}
}