private int Build(ILogger logger, BuildOrDecompileArguments arguments) { var diagnosticLogger = new BicepDiagnosticLogger(logger); var bicepPath = PathHelper.ResolvePath(arguments.InputFile); if (arguments.OutputToStdOut) { BuildToStdout(diagnosticLogger, bicepPath); } else if (arguments.OutputDir is not null) { var outputDir = PathHelper.ResolvePath(arguments.OutputDir); if (!Directory.Exists(outputDir)) { throw new CommandLineException($"The specified output directory \"{outputDir}\" does not exist."); } var outputPath = Path.Combine(outputDir, Path.GetFileName(bicepPath)); BuildToFile(diagnosticLogger, bicepPath, PathHelper.GetDefaultBuildOutputPath(outputPath)); } else if (arguments.OutputFile is not null) { BuildToFile(diagnosticLogger, bicepPath, arguments.OutputFile); } else { BuildToFile(diagnosticLogger, bicepPath, PathHelper.GetDefaultBuildOutputPath(bicepPath)); } // return non-zero exit code on errors return(diagnosticLogger.HasLoggedErrors ? 1 : 0); }
public int Decompile(ILogger logger, BuildOrDecompileArguments arguments) { logger.LogWarning( "WARNING: Decompilation is a best-effort process, as there is no guaranteed mapping from ARM JSON to Bicep.\n" + "You may need to fix warnings and errors in the generated bicep file(s), or decompilation may fail entirely if an accurate conversion is not possible.\n" + "If you would like to report any issues or inaccurate conversions, please see https://github.com/Azure/bicep/issues."); var diagnosticLogger = new BicepDiagnosticLogger(logger); var jsonPath = PathHelper.ResolvePath(arguments.InputFile); if (arguments.OutputToStdOut) { return(DecompileToStdout(diagnosticLogger, jsonPath)); } else if (arguments.OutputDir is not null) { var outputDir = PathHelper.ResolvePath(arguments.OutputDir); if (!Directory.Exists(outputDir)) { throw new CommandLineException($"The specified output directory \"{outputDir}\" does not exist."); } var outputPath = Path.Combine(outputDir, Path.GetFileName(jsonPath)); return(DecompileToFile(diagnosticLogger, jsonPath, PathHelper.GetDefaultDecompileOutputPath(outputPath))); } else if (arguments.OutputFile is not null) { return(DecompileToFile(diagnosticLogger, jsonPath, arguments.OutputFile)); } else { return(DecompileToFile(diagnosticLogger, jsonPath, PathHelper.GetDefaultDecompileOutputPath(jsonPath))); } }
public int Decompile(ILogger logger, DecompileArguments arguments) { logger.LogWarning( "WARNING: Decompilation is a best-effort process, as there is no guaranteed mapping from ARM JSON to Bicep.\n" + "You may need to fix warnings and errors in the generated bicep file(s), or decompilation may fail entirely if an accurate conversion is not possible.\n" + "If you would like to report any issues or inaccurate conversions, please see https://github.com/Azure/bicep/issues."); var diagnosticLogger = new BicepDiagnosticLogger(logger); var jsonPath = PathHelper.ResolvePath(arguments.InputFile); try { var(bicepUri, filesToSave) = TemplateDecompiler.DecompileFileWithModules(resourceTypeProvider, new FileResolver(), PathHelper.FilePathToFileUrl(jsonPath)); foreach (var(fileUri, bicepOutput) in filesToSave) { File.WriteAllText(fileUri.LocalPath, bicepOutput); } var syntaxTreeGrouping = SyntaxTreeGroupingBuilder.Build(new FileResolver(), new Workspace(), bicepUri); var compilation = new Compilation(resourceTypeProvider, syntaxTreeGrouping); return(LogDiagnosticsAndCheckSuccess(diagnosticLogger, compilation) ? 0 : 1); } catch (Exception exception) { this.errorWriter.WriteLine($"{jsonPath}: Decompilation failed with fatal error \"{exception.Message}\""); return(1); } }
public int Decompile(ILogger logger, BuildOrDecompileArguments arguments) { logger.LogWarning(CliResources.DecompilerDisclaimerMessage); var diagnosticLogger = new BicepDiagnosticLogger(logger); var jsonPath = PathHelper.ResolvePath(arguments.InputFile); if (arguments.OutputToStdOut) { return(DecompileToStdout(diagnosticLogger, jsonPath)); } else if (arguments.OutputDir is not null) { var outputDir = PathHelper.ResolvePath(arguments.OutputDir); if (!Directory.Exists(outputDir)) { throw new CommandLineException(string.Format(CliResources.DirectoryDoesNotExistFormat, outputDir)); } var outputPath = Path.Combine(outputDir, Path.GetFileName(jsonPath)); return(DecompileToFile(diagnosticLogger, jsonPath, PathHelper.GetDefaultDecompileOutputPath(outputPath))); } else if (arguments.OutputFile is not null) { return(DecompileToFile(diagnosticLogger, jsonPath, arguments.OutputFile)); } else { return(DecompileToFile(diagnosticLogger, jsonPath, PathHelper.GetDefaultDecompileOutputPath(jsonPath))); } }
public int Run(string[] args) { // ReSharper disable once ConvertToUsingDeclaration using (var loggerFactory = CreateLoggerFactory()) { // the only value in using the dotnet logging framework is that we can easily implement filters // and logging to multiple targets in the future (stdout AND a log file, for example) // it does not help us with formatting of the messages however, so we will have to workaround that IDiagnosticLogger logger = new BicepDiagnosticLogger(loggerFactory.CreateLogger("bicep")); try { switch (ArgumentParser.Parse(args)) { case BuildArguments buildArguments: // build Build(logger, buildArguments); break; case VersionArguments _: // --version ArgumentParser.PrintVersion(this.outputWriter); break; case HelpArguments _: // --help ArgumentParser.PrintUsage(this.outputWriter); break; case UnrecognizedArguments unrecognizedArguments: // everything else var exeName = ArgumentParser.GetExeName(); this.errorWriter.WriteLine($"Unrecognized arguments '{unrecognizedArguments.SuppliedArguments}' specified. Use '{exeName} --help' to view available options."); return(1); } // return non-zero exit code on errors return(logger.HasLoggedErrors ? 1 : 0); } catch (CommandLineException exception) { this.errorWriter.WriteLine(exception.Message); return(1); } catch (BicepException exception) { this.errorWriter.WriteLine(exception.Message); return(1); } catch (ErrorDiagnosticException exception) { this.errorWriter.WriteLine(exception.Message); return(1); } } }
public int Decompile(ILogger logger, DecompileArguments arguments) { logger.LogWarning( "WARNING: Decompilation is a best-effort process, as there is no guaranteed mapping from ARM JSON to Bicep.\n" + "You may need to fix warnings and errors in the generated bicep file(s), or decompilation may fail entirely if an accurate conversion is not possible.\n" + "If you would like to report any issues or inaccurate conversions, please see https://github.com/Azure/bicep/issues."); var diagnosticLogger = new BicepDiagnosticLogger(logger); var hadErrors = false; var jsonPaths = arguments.Files.Select(f => PathHelper.ResolvePath(f)).ToArray(); foreach (var jsonPath in jsonPaths) { hadErrors |= !DecompileSingleFile(diagnosticLogger, jsonPath); } return(hadErrors ? 1 : 0); }
private int Build(ILogger logger, BuildArguments arguments) { var diagnosticLogger = new BicepDiagnosticLogger(logger); var bicepPaths = arguments.Files.Select(f => PathHelper.ResolvePath(f)).ToArray(); if (arguments.OutputToStdOut) { BuildManyFilesToStdOut(diagnosticLogger, bicepPaths); } else { foreach (string bicepPath in bicepPaths) { string outputPath = PathHelper.GetDefaultOutputPath(bicepPath); BuildSingleFile(diagnosticLogger, bicepPath, outputPath); } } // return non-zero exit code on errors return(diagnosticLogger.HasLoggedErrors ? 1 : 0); }