/// <summary> /// Generates formatted Q# code for the file with the given uri based on the syntax tree in the given compilation. /// If the id of the file is consistent with the one assigned to a code snippet, /// logs the generated code using the given logger. /// Creates a file containing the generated code in the given output folder otherwise. /// Returns true if the generation succeeded, and false if an exception was thrown. /// Throws an ArgumentNullException if the given compilation, the file uri or its absolute path are null. /// </summary> private static bool GenerateFormattedQsFile(Compilation compilation, NonNullable <string> fileName, string outputFolder, ILogger logger) { if (compilation == null) { throw new ArgumentNullException(nameof(compilation)); } var code = Enumerable.Empty <string>(); try { code = code.Concat(GenerateQsCode(compilation, fileName, logger).Where(c => !String.IsNullOrWhiteSpace(c))); } catch (Exception ex) { logger?.Log(ErrorCode.QsGenerationFailed, Enumerable.Empty <string>(), fileName.Value); logger?.Log(ex); return(false); } if (Options.IsCodeSnippet(fileName)) { code = new string[] { "" }.Concat(Formatting.Indent(code.ToArray())); logger?.Log(InformationCode.GeneratedQsCode, Enumerable.Empty <string>(), messageParam: code.ToArray()); } else { var content = String.Join(Environment.NewLine, code.Select(block => $"{block}{Environment.NewLine}{Environment.NewLine}")); CompilationLoader.GeneratedFile(fileName, outputFolder ?? "FormattedFiles", ".qs", content); } return(true); }
/// <inheritdoc/> public bool Transformation(QsCompilation compilation, out QsCompilation transformed) { // random "diagnostic" to check if diagnostics loading works this.GeneratedDiagnostics = new List <IRewriteStep.Diagnostic>() { new IRewriteStep.Diagnostic { Severity = CodeAnalysis.DiagnosticSeverity.Info, Message = "Invokation of the Q# compiler extension for C# generation to demonstrate execution on the simulation framework.", } }; var success = true; var outputFolder = this.AssemblyConstants.TryGetValue(ReservedKeywords.AssemblyConstants.OutputPath, out var path) ? path : null; var allSources = GetSourceFiles.Apply(compilation.Namespaces) // also generate the code for referenced libraries... // ... except when they are one of the packages that currently still already contains the C# code (temporary workaround): .Where(s => !Path.GetFileName(s).StartsWith("Microsoft.Quantum")); foreach (var source in allSources) { var content = SimulationCode.generate(source, CodegenContext.Create(compilation.Namespaces)); try { CompilationLoader.GeneratedFile(source, outputFolder ?? this.Name, ".g.cs", content); } catch { success = false; } } transformed = compilation; return(success); }
private static void GenerateFromBinary(string outputFolder, string pathToBinary) { CompilationLoader.ReadBinary(pathToBinary, out var syntaxTree); var allSources = GetSourceFiles.Apply(syntaxTree); // also generate the code for referenced libraries foreach (var source in allSources) { var content = SimulationCode.generate(source, syntaxTree); CompilationLoader.GeneratedFile(source, outputFolder, ".g.cs", content); } }
private static void GenerateFromBinary(string outputFolder, string pathToBinary) { var syntaxTree = CompilationLoader.ReadBinary(pathToBinary).ToArray(); var allSources = GetSourceFiles.Apply(syntaxTree).Where(file => file.Value.EndsWith(".qs")); foreach (var source in allSources) { var content = SimulationCode.generate(source, syntaxTree); CompilationLoader.GeneratedFile(source, outputFolder, ".g.cs", content); } }
public bool Transformation(QsCompilation compilation, out QsCompilation transformed) { var success = true; var allSources = GetSourceFiles.Apply(compilation.Namespaces) // also generate the code for referenced libraries... // ... except when they are one of the packages that currently still already contains the C# code (temporary workaround): .Where(s => !Path.GetFileName(s.Value).StartsWith("Microsoft.Quantum")); foreach (var source in allSources) { var content = SimulationCode.generate(source, compilation.Namespaces); try { CompilationLoader.GeneratedFile(source, this.OutputFolder ?? this.Name, ".g.cs", content); } catch { success = false; } } transformed = compilation; return(success); }