public bool TryGetOutputBuilder(string name, out OutputBuilder outputBuilder) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } return(_outputBuilders.TryGetValue(name, out outputBuilder)); }
public OutputBuilder Create(string name) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } var outputBuilder = new OutputBuilder(name); _outputBuilders.Add(name, outputBuilder); return(outputBuilder); }
public OutputBuilder Create(string name, bool isTestOutput = false) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); } var outputBuilder = new OutputBuilder(name, isTestOutput: isTestOutput); this._outputBuilders.Add(name, outputBuilder); return(outputBuilder); }
public void Close() { Stream stream = null; OutputBuilder methodClassOutputBuilder = null; bool emitNamespaceDeclaration = true; bool leaveStreamOpen = false; if (!_config.GenerateMultipleFiles) { var outputPath = _config.OutputLocation; stream = _outputStreamFactory(outputPath); leaveStreamOpen = true; var usingDirectives = Enumerable.Empty <string>(); var staticUsingDirectives = Enumerable.Empty <string>(); foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders) { usingDirectives = usingDirectives.Concat(outputBuilder.UsingDirectives); staticUsingDirectives = staticUsingDirectives.Concat(outputBuilder.StaticUsingDirectives); } usingDirectives = usingDirectives.Distinct() .OrderBy((usingDirective) => usingDirective); staticUsingDirectives = staticUsingDirectives.Distinct() .OrderBy((staticUsingDirective) => staticUsingDirective); usingDirectives = usingDirectives.Concat(staticUsingDirectives); if (usingDirectives.Any()) { using var sw = new StreamWriter(stream, defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen); { sw.NewLine = "\n"; sw.Write(_config.HeaderText); foreach (var usingDirective in usingDirectives) { sw.Write("using"); sw.Write(' '); sw.Write(usingDirective); sw.WriteLine(';'); } sw.WriteLine(); } } } foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders) { var outputPath = _config.OutputLocation; var isMethodClass = _config.MethodClassName.Equals(outputBuilder.Name); if (_config.GenerateMultipleFiles) { outputPath = Path.Combine(outputPath, $"{outputBuilder.Name}.cs"); stream = _outputStreamFactory(outputPath); emitNamespaceDeclaration = true; } else if (isMethodClass) { methodClassOutputBuilder = outputBuilder; continue; } CloseOutputBuilder(stream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration); emitNamespaceDeclaration = false; } if (leaveStreamOpen && _outputBuilderFactory.OutputBuilders.Any()) { if (methodClassOutputBuilder != null) { CloseOutputBuilder(stream, methodClassOutputBuilder, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration); } using var sw = new StreamWriter(stream, defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen); sw.NewLine = "\n"; sw.WriteLine('}'); } _diagnostics.Clear(); _outputBuilderFactory.Clear(); _visitedCursors.Clear(); }
private void CloseOutputBuilder(Stream stream, OutputBuilder outputBuilder, bool isMethodClass, bool leaveStreamOpen, bool emitNamespaceDeclaration) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } if (outputBuilder is null) { throw new ArgumentNullException(nameof(outputBuilder)); } using var sw = new StreamWriter(stream, defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen); sw.NewLine = "\n"; if (_config.GenerateMultipleFiles) { if (_config.HeaderText != string.Empty) { sw.WriteLine(_config.HeaderText); } if (outputBuilder.UsingDirectives.Any()) { foreach (var usingDirective in outputBuilder.UsingDirectives.Concat(outputBuilder.StaticUsingDirectives)) { sw.Write("using"); sw.Write(' '); sw.Write(usingDirective); sw.WriteLine(';'); } sw.WriteLine(); } } var indentationString = outputBuilder.IndentationString; if (emitNamespaceDeclaration) { sw.Write("namespace"); sw.Write(' '); sw.WriteLine(Config.Namespace); sw.WriteLine('{'); } else { sw.WriteLine(); } if (isMethodClass) { sw.Write(indentationString); sw.Write("public static"); sw.Write(' '); if (_isMethodClassUnsafe) { sw.Write("unsafe"); sw.Write(' '); } sw.Write("partial class"); sw.Write(' '); sw.WriteLine(Config.MethodClassName); sw.Write(indentationString); sw.WriteLine('{'); indentationString += outputBuilder.IndentationString; sw.Write(indentationString); sw.Write("private const string LibraryPath ="); sw.Write(' '); sw.Write('"'); sw.Write(Config.LibraryPath); sw.Write('"'); sw.WriteLine(';'); sw.WriteLine(); } foreach (var line in outputBuilder.Contents) { if (string.IsNullOrWhiteSpace(line)) { sw.WriteLine(); } else { sw.Write(indentationString); sw.WriteLine(line); } } if (isMethodClass) { sw.Write(outputBuilder.IndentationString); sw.WriteLine('}'); } if (_config.GenerateMultipleFiles) { sw.WriteLine('}'); } }