/// <summary> /// Persists the artifact in a single file /// </summary> /// <param name="artifact"></param> /// <param name="productionContext"></param> private void PersistSingleFile(GeneratedArtifact artifact, ProductionContext productionContext) { string filePath = resolveTemplate(artifact.GroupFilePathTemplate, productionContext); Writer = Services.IOService.OpenIndentedTextWrite(filePath); // write begin file ArtifactBegin(); // write autogenerated infos AutogeneratedInfoBegin(); artifact.WriteAutogeneratedInfosTo(Writer); AutogeneratedInfoEnd(); // write artifact imports List<string> writtenImports = new List<string>(); artifact.WriteImportsTo(Writer, writtenImports); foreach (ArtifactDataItem artifactDataItem in artifact.ArtifactDataItems) { artifactDataItem.WriteImportsTo(Writer, writtenImports); } string ns = null; int baseIndent = 0; // namespace can be null if not required by target technology if (!string.IsNullOrEmpty(artifact.NamespaceTemplate)) { // open namespace ns = resolveTemplate(artifact.NamespaceTemplate, productionContext); baseIndent = OpenNamespace(ns); } // write artifact code lines foreach (ArtifactDataItem artifactDataItem in artifact.ArtifactDataItems) { artifactDataItem.WriteCodeLinesTo(Writer, baseIndent); } Writer.Indent = Math.Max(0, Writer.Indent - baseIndent); if (ns != null) { // close namespace CloseNamespace(ns); } // write end file ArtifactEnd(); Writer.Close(); }
/// <summary> /// Persist each <see cref="ArtifactDataItem"/> in its own file. /// </summary> /// <param name="artifact"></param> /// <param name="productionContext"></param> private void PersistMultiFile(GeneratedArtifact artifact, ProductionContext productionContext) { foreach (ArtifactDataItem artifactDataItem in artifact.ArtifactDataItems) { // create writer string filePath = resolveTemplate(artifact.ItemFilePathTemplate, productionContext); Writer = Services.IOService.OpenIndentedTextWrite(filePath); // write begin file ArtifactBegin(); // write autogenerated infos AutogeneratedInfoBegin(); artifact.WriteAutogeneratedInfosTo(Writer); AutogeneratedInfoEnd(); // write artifact imports List<string> writtenImports = new List<string>(); artifact.WriteImportsTo(Writer, writtenImports); // write artifact item imports artifactDataItem.WriteImportsTo(Writer, writtenImports); // resolve and open namespace string ns = resolveTemplate(artifact.NamespaceTemplate, productionContext); int baseIndent = OpenNamespace(ns); // write artifact item code lines artifactDataItem.WriteCodeLinesTo(Writer, baseIndent); // close namespace CloseNamespace(ns); // write end file ArtifactEnd(); Writer.Close(); } }