private static void DrawTreeRecursive(IReadOnlyArchiveDirectory archiveDirectory, string linePrefix)
        {
            foreach (var directory in archiveDirectory.Directories.All.OrderBy(d => d.Name))
            {
                Console.WriteLine($"{linePrefix} |- {directory.Name}");

                DrawTreeRecursive(directory, $"{linePrefix}    ");
            }

            foreach (var file in archiveDirectory.Files.OrderBy(f => f.Name))
            {
                Console.WriteLine($"{linePrefix} |- {file.Name}");
            }
        }
Example #2
0
        private void RenderDirectory(IReadOnlyArchiveDirectory archiveDirectory, int indent = 0)
        {
            string indentString = new string(' ', indent * 2);

            foreach (var subdirectory in archiveDirectory.Directories.All)
            {
                Console.WriteLine($"{indentString}> {subdirectory.Name}");
                RenderDirectory(subdirectory, indent + 1);
            }
            foreach (var file in archiveDirectory.Files)
            {
                Console.WriteLine($"{indentString}- {file.Name}");
            }
        }
Example #3
0
        public static async Task <PackageExplorer> LoadAsync(IReadOnlyArchiveDirectory source)
        {
            var definitionEntry    = source.Files.GetFile("definition.json");
            var definitionDocument = LoadJsonDocument <PackageDefinition>(definitionEntry);

            var tagsEntry    = source.Files.GetFile("tags.json");
            var tagsDocument = LoadJsonDocument <IReadOnlyDictionary <string, IReadOnlyList <string> > >(tagsEntry);
            var tags         = new Dictionary <string, IResourceCollection>();

            var rootDirectiory = new PackageDirectory("", "", null);

            var packageExplorer = new PackageExplorer
            {
                Source        = source,
                RootDirectory = rootDirectiory,
                Definition    = definitionDocument
            };

            void ImportDirectory(IReadOnlyArchiveDirectory directory, PackageDirectory packageDirectory)
            {
                foreach (var childDirectory in directory.Directories)
                {
                    ImportDirectory(childDirectory,
                                    new PackageDirectory(childDirectory.Name, childDirectory.FullName, packageDirectory));
                }

                foreach (var file in directory.Files)
                {
                    if (!file.FullName.EndsWith(".pkgmeta"))
                    {
                        continue;
                    }
                    var contentEntry = directory.Files.GetFile(file.Name.Substring(0, file.Name.Length - 8));

                    var metadataModel = LoadJsonDocument <PackageResourceMetadataModel>(file);

                    var resource = new PackageResource(packageExplorer, packageDirectory, contentEntry, metadataModel);

                    packageExplorer.Resources.Add(resource.FullName, resource);
                    packageDirectory.Resources.Add(resource.Name, resource);

                    foreach (var tagCategory in tagsDocument)
                    {
                        if (tagCategory.Value.Contains(resource.FullName))
                        {
                            if (!tags.TryGetValue(tagCategory.Key, out var taggedResourcesCollection))
                            {
                                taggedResourcesCollection = new PackageResourceCollection();
                                tags[tagCategory.Key]     = taggedResourcesCollection;
                            }

                            var taggedResources = (PackageResourceCollection)taggedResourcesCollection;

                            taggedResources.Add(resource.FullName, resource);
                            resource.Tags.tags.Add(tagCategory.Key);
                        }
                    }
                }
            }

            ImportDirectory(source, rootDirectiory);

            packageExplorer.Tags = new PackageTagsCollection(tags);

            return(packageExplorer);
        }
Example #4
0
        public static async Task <PackageExplorer> LoadAsync(IReadOnlyArchiveDirectory source)
        {
            var definitionEntry    = source.Files.GetFile("definition.json");
            var definitionDocument = LoadJsonDocument <PackageDefinition>(definitionEntry);

            var tagsEntry    = source.Files.GetFile("tags.json");
            var tagsDocument = LoadJsonDocument <IReadOnlyDictionary <string, IReadOnlyList <string> > >(tagsEntry);
            var tags         = new Dictionary <string, IResourceCollection>();

            var rootDirectiory = new PackageDirectory("", null);

            var packageExplorer = new PackageExplorer
            {
                Source        = source,
                RootDirectory = rootDirectiory,
                Definition    = definitionDocument
            };

            var resourcesDirectory = source.Directories.GetDirectory("resources");
            var contentsDirectory  = source.Directories.GetDirectory("contents");

            // Does this package contain any resources?
            if (resourcesDirectory != null)
            {
                foreach (var resourceFile in resourcesDirectory.Files)
                {
                    var metadataModel = LoadJsonDocument <PackageResourceMetadataModel>(resourceFile);

                    // Directory
                    string[] elements = metadataModel.FullName
                                        .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    var parentDirectory = rootDirectiory;
                    for (int i = 0; i < elements.Length - 1; i++)
                    {
                        string element = elements[i];

                        PackageDirectory findDirectory = null;
                        foreach (var directory in parentDirectory.Directories)
                        {
                            if (directory.Name == element)
                            {
                                findDirectory = directory;
                                break;
                            }
                        }
                        if (findDirectory == null)
                        {
                            findDirectory = new PackageDirectory(element, parentDirectory);
                            parentDirectory.Directories.Add(findDirectory);
                        }
                        parentDirectory = findDirectory;
                    }

                    // Resource
                    var contentFile = contentsDirectory.Files.GetFile(metadataModel.ContentId);
                    var resource    = new PackageResource(packageExplorer, parentDirectory, contentFile, metadataModel);

                    packageExplorer.Resources.Add(resource.FullName, resource);
                    parentDirectory.Resources.Add(resource.Name, resource);

                    // Tags
                    foreach (var tagCategory in tagsDocument)
                    {
                        if (tagCategory.Value.Contains(resource.FullName))
                        {
                            if (!tags.TryGetValue(tagCategory.Key, out var taggedResourcesCollection))
                            {
                                taggedResourcesCollection = new PackageResourceCollection();
                                tags[tagCategory.Key]     = taggedResourcesCollection;
                            }

                            var taggedResources = (PackageResourceCollection)taggedResourcesCollection;

                            taggedResources.Add(resource.FullName, resource);
                            resource.Tags.tags.Add(tagCategory.Key);
                        }
                    }
                }
            }

            packageExplorer.Tags = new PackageTagsCollection(tags);

            return(packageExplorer);
        }