public AdrEntry Write() { if (this.templateType == TemplateType.Adr) { this.WriteAdr(); } else { AdrFile superSededFile = null; if (!string.IsNullOrWhiteSpace(this.SupersededFile)) { string superFilePath = Path.Combine(this.docFolder, Path.GetFileName(this.SupersededFile)); if (!File.Exists(superFilePath)) { throw new FileNotFoundException("Superseded file does not exists"); } superSededFile = new AdrFile(superFilePath); } this.WriteNew(); if (superSededFile != null) { SupersedeAdrFileStatus(superSededFile); } } new AdrIndex(this.docFolder).Generate(); return(this); }
private void SupersedeAdrFileStatus(AdrFile file) { var filePath = Path.Combine(this.docFolder, file.FileName); string[] content = File.Exists(filePath) ? File.ReadAllLines(filePath) : null; if (content?.Length > 0) { //reading file to update the status bool lastNonEmptyLineContainsStatus = false; for (int i = 0; i < content.Length; i++) { if (lastNonEmptyLineContainsStatus && !string.IsNullOrWhiteSpace(content[i]) && !Regex.IsMatch(content[i], @"##\s+Context", RegexOptions.IgnoreCase)) { content[i] = $"Superseded - [{fileNumber.ToString().PadLeft(4, '0')} - {Title}]" + $"(./{Path.GetFileName(fileName)})"; break; } if (!string.IsNullOrWhiteSpace(content[i])) { lastNonEmptyLineContainsStatus = Regex.IsMatch(content[i], @"##\s+Status", RegexOptions.IgnoreCase); } } //writing to the file with new status to `superseded` using var writer = File.CreateText(filePath); { foreach (var line in content) { writer.WriteLine(line); } } } }
public AdrIndex Generate() { IEnumerable <string> files = Directory.EnumerateFiles(_indexDocFolder, "*.md", SearchOption.TopDirectoryOnly); using var writer = File.CreateText(_indexFile); { writer.WriteLine($"# {IndexFileName}"); writer.WriteLine(); writer.WriteLine("| Number | Title | Superseded by |"); writer.WriteLine("| ------ | ----- | ------------- |"); if (files?.Count() > 0) { foreach (string file in files) { string fileName = Path.GetFileName(file); if (fileName == _indexFileName) { continue; } AdrFile adrFile = new AdrFile(file); if (string.IsNullOrWhiteSpace(adrFile.SupersededFile)) { writer.WriteLine($"| {adrFile.Number} | [{adrFile.Title}](./{adrFile.FileName})| |"); } else { AdrFile superSededFile = new AdrFile(adrFile.SupersededFile); writer.WriteLine($"| {adrFile.Number} | ~~[{adrFile.Title}](./{fileName})~~|" + $" [{superSededFile.Number} - {superSededFile.Title}](./{superSededFile.FileName}) |"); } } } } return(this); }