Example #1
0
        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);
        }