public static void WritePage(ModelIndex page) { string intendedFilePath = page.Links.Self; (new FileInfo(intendedFilePath)).Directory.Create(); ConvertLinks(page); string indexJsonString = JsonSerializer.Serialize(page, ParsingUtils.DefaultJsonSerializerOptions); Outputs.WriteToFile(intendedFilePath, indexJsonString); Outputs.WriteOut($"Created index page: {intendedFilePath}"); }
private static void ConvertLinks(ModelIndex page) { if (!string.IsNullOrEmpty(page.Links.Self)) { page.Links.Self = new FileInfo(page.Links.Self).Name; } if (!string.IsNullOrEmpty(page.Links.Next)) { page.Links.Next = new FileInfo(page.Links.Next).Name; } if (!string.IsNullOrEmpty(page.Links.Prev)) { page.Links.Prev = new FileInfo(page.Links.Prev).Name; } }
public static int RepoIndex(DirectoryInfo localRepo, FileInfo outputFile, int pageLimit) { if (localRepo == null) { localRepo = new DirectoryInfo(Path.GetFullPath(".")); } if (!localRepo.Exists) { Outputs.WriteError($"Invalid target repository directory: {localRepo.FullName}."); return(ReturnCodes.InvalidArguments); } int currentPageCount = 0; int pageIdentifier = 1; FileInfo currentPageFile = outputFile; var modelDictionary = new ModelDictionary(); var currentLinks = new ModelIndexLinks { Self = currentPageFile.FullName }; foreach (string file in Directory.EnumerateFiles(localRepo.FullName, "*.json", new EnumerationOptions { RecurseSubdirectories = true })) { if (file.ToLower().EndsWith(".expanded.json")) { continue; } // TODO: Debug plumbing. // Outputs.WriteDebug($"Processing: {file}"); try { var modelFile = new FileInfo(file); ModelIndexEntry indexEntry = ParsingUtils.ParseModelFileForIndex(modelFile); modelDictionary.Add(indexEntry.Dtmi, indexEntry); currentPageCount += 1; if (currentPageCount == pageLimit) { var nextPageFile = new FileInfo( Path.Combine( currentPageFile.Directory.FullName, $"index.page.{pageIdentifier}.json")); currentLinks.Next = nextPageFile.FullName; var nextLinks = new ModelIndexLinks { Self = nextPageFile.FullName, Prev = currentLinks.Self }; var modelIndex = new ModelIndex(modelDictionary, currentLinks); IndexPageUtils.WritePage(modelIndex); currentPageCount -= pageLimit; modelDictionary = new ModelDictionary(); currentPageFile = nextPageFile; currentLinks = nextLinks; pageIdentifier += 1; } } catch (Exception e) { Outputs.WriteError($"Failure processing model file: {file}, {e.Message}"); return(ReturnCodes.ProcessingError); } } if (modelDictionary.Count > 0) { var modelIndex = new ModelIndex(modelDictionary, currentLinks); IndexPageUtils.WritePage(modelIndex); } return(ReturnCodes.Success); }