protected virtual void GenerateNamespaceMember(HierarchyNamespace ns, HierarchyElement element, string outputDirectory) { FilesystemPath path = Context.OutputPathProvider.GetPathFor(outputDirectory, element); if (String.IsNullOrEmpty(path?.FullPath)) { Logger.Warning($"Unable to generate output for element {element.GetType ().FullName} ({element.GetManagedName (true) ?? element.FullName}) since no full path was given (at {element.GetLocation ()})"); return; } if (path.IsDirectory) { throw new InvalidOperationException($"Namespace member must be written to a file ({ns.FullName})"); } EnsureDirectory(Path.GetDirectoryName(path.FullPath)); CheckOverwrite(path.FullPath); using (Stream fs = File.Open(path.FullPath, FileMode.Create)) { using (StreamWriter writer = new StreamWriter(fs, Context.FileEncoding)) { WriteFileHeader(ns, writer); OutputNamespaceMember(element, writer, path.FullPath); WriteFileFooter(ns, writer); } } }
protected virtual void GenerateNamespace(string outputDirectoryRoot, HierarchyNamespace ns) { FilesystemPath path = Context.OutputPathProvider.GetPathFor(outputDirectoryRoot, ns); if (String.IsNullOrEmpty(path?.FullPath)) { return; } Stream nsFile = null; StreamWriter nsFileWriter = null; try { string targetKind; if (path.IsDirectory) { targetKind = "directory"; EnsureDirectory(path.FullPath); } else { targetKind = "file"; EnsureDirectory(Path.GetDirectoryName(path.FullPath)); CheckOverwrite(path.FullPath); nsFile = File.Open(path.FullPath, FileMode.Create); nsFileWriter = new StreamWriter(nsFile, Context.FileEncoding); } Logger.Debug($"Creating {targetKind} for namespace {ns.GetManagedName (true)}: {path.FullPath}"); Helpers.ForEachNotNull( ns.Members, (HierarchyElement nsm) => { if (nsFileWriter != null) { WriteFileHeader(ns, nsFileWriter); GenerateNamespaceMember(nsm, nsFileWriter, outputDirectoryRoot); WriteFileHeader(ns, nsFileWriter); } else { GenerateNamespaceMember(ns, nsm, outputDirectoryRoot); } } ); } finally { if (nsFileWriter != null) { nsFileWriter.Dispose(); nsFileWriter = null; } if (nsFile != null) { nsFile.Dispose(); nsFile = null; } } }