Ejemplo n.º 1
0
        private static ManifestEntry BuildEntry(XElement element)
        {
            RuntimeHelpers.EnsureSufficientExecutionStack();
            if (element.NodeType != XmlNodeType.Element)
            {
                throw new InvalidOperationException($"Invalid manifest format. Expected a 'File' or a 'Directory' node:" +
                                                    $" '{element.ToString()}'");
            }

            if (string.Equals(element.Name.LocalName, "File", StringComparison.Ordinal))
            {
                var entryName = EnsureName(element);
                var path      = EnsureElement(element, "ResourcePath");
                var pathValue = EnsureText(path);
                return(new ManifestFile(entryName, pathValue));
            }

            if (string.Equals(element.Name.LocalName, "Directory", StringComparison.Ordinal))
            {
                var directoryName = EnsureName(element);
                var children      = new List <ManifestEntry>();
                foreach (var child in element.Elements())
                {
                    children.Add(BuildEntry(child));
                }

                ValidateEntries(children);

                return(ManifestDirectory.CreateDirectory(directoryName, children.ToArray()));
            }

            throw new InvalidOperationException($"Invalid manifest format.Expected a 'File' or a 'Directory' node. " +
                                                $"Got '{element.Name.LocalName}' instead.");
        }
        internal EmbeddedFilesManifest(ManifestDirectory rootDirectory)
        {
            if (rootDirectory == null)
            {
                throw new ArgumentNullException(nameof(rootDirectory));
            }

            _rootDirectory = rootDirectory;
        }
Ejemplo n.º 3
0
        protected internal virtual void SetParent(ManifestDirectory directory)
        {
            if (Parent != null)
            {
                throw new InvalidOperationException("Directory already has a parent.");
            }

            Parent = directory;
        }
Ejemplo n.º 4
0
        public ManifestDirectoryInfo(ManifestDirectory directory, DateTimeOffset lastModified)
        {
            if (directory == null)
            {
                throw new ArgumentNullException(nameof(directory));
            }

            Directory    = directory;
            LastModified = lastModified;
        }
Ejemplo n.º 5
0
        public void TraversingWithDot_ReturnsSelf()
        {
            // Arrange
            var directory = ManifestDirectory.CreateDirectory("a", Array.Empty <ManifestEntry>());

            // Act
            var result = directory.Traverse(".");

            // Assert
            Assert.Same(directory, result);
        }
Ejemplo n.º 6
0
        public void TraversingRootDirectoryWithDotDot_ReturnsSinkDirectory()
        {
            // Arrange
            var directory = ManifestDirectory.CreateRootDirectory(Array.Empty <ManifestEntry>());

            // Act
            var result = directory.Traverse("..");

            // Assert
            Assert.Equal(ManifestEntry.UnknownPath, result);
        }
Ejemplo n.º 7
0
        public void TraversingANonExistingFile_ReturnsUnknownPath()
        {
            // Arrange
            var directory = ManifestDirectory.CreateDirectory("a", Array.Empty <ManifestEntry>());

            // Act
            var result = directory.Traverse("missing.txt");

            // Assert
            Assert.Equal(ManifestEntry.UnknownPath, result);
        }
Ejemplo n.º 8
0
        public void TraversingWithDotDot_ReturnsParent()
        {
            // Arrange
            var childDirectory = ManifestDirectory.CreateDirectory("b", Array.Empty <ManifestEntry>());
            var directory      = ManifestDirectory.CreateDirectory("a", new[] { childDirectory });

            // Act
            var result = childDirectory.Traverse("..");

            // Assert
            Assert.Equal(directory, result);
        }
Ejemplo n.º 9
0
        public static EmbeddedFilesManifest Parse(Assembly assembly, string name)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var stream = assembly.GetManifestResourceStream(name);

            if (stream == null)
            {
                throw new InvalidOperationException($"Could not load the embedded file manifest " +
                                                    $"'{name}' for assembly '{assembly.GetName().Name}'.");
            }

            var document = XDocument.Load(stream);

            var manifest        = EnsureElement(document, "Manifest");
            var manifestVersion = EnsureElement(manifest, "ManifestVersion");
            var version         = EnsureText(manifestVersion);

            if (!string.Equals("1.0", version, StringComparison.Ordinal))
            {
                throw new InvalidOperationException($"The embedded file manifest '{name}' for " +
                                                    $"assembly '{assembly.GetName().Name}' specifies an unsupported file format" +
                                                    $" version: '{version}'.");
            }
            var fileSystem = EnsureElement(manifest, "FileSystem");

            var entries     = fileSystem.Elements();
            var entriesList = new List <ManifestEntry>();

            foreach (var element in entries)
            {
                var entry = BuildEntry(element);
                entriesList.Add(entry);
            }

            ValidateEntries(entriesList);

            var rootDirectory = ManifestDirectory.CreateRootDirectory(entriesList.ToArray());

            return(new EmbeddedFilesManifest(rootDirectory));
        }
Ejemplo n.º 10
0
        public ManifestDirectoryContents(Assembly assembly, ManifestDirectory directory, DateTimeOffset lastModified)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            if (directory == null)
            {
                throw new ArgumentNullException(nameof(directory));
            }

            Assembly      = assembly;
            Directory     = directory;
            _lastModified = lastModified;
        }
Ejemplo n.º 11
0
        internal static void ValidateChildrenAndSetParent(ManifestEntry[] children, ManifestDirectory parent)
        {
            foreach (var child in children)
            {
                if (child == UnknownPath)
                {
                    throw new InvalidOperationException($"Invalid entry type '{nameof(ManifestSinkDirectory)}'");
                }

                if (child is ManifestRootDirectory)
                {
                    throw new InvalidOperationException($"Can't add a root folder as a child");
                }

                child.SetParent(parent);
            }
        }
Ejemplo n.º 12
0
        public static ManifestDirectory CreateDirectory(string name, ManifestEntry[] children)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException($"'{nameof(name)}' must not be null, empty or whitespace.", nameof(name));
            }

            if (children == null)
            {
                throw new ArgumentNullException(nameof(children));
            }

            var result = new ManifestDirectory(name, children);
            ValidateChildrenAndSetParent(children, result);

            return result;
        }
Ejemplo n.º 13
0
        public void ScopingAFolderAndTryingToGetAScopedFile_ReturnsSinkDirectory()
        {
            // Arrange
            var directory = ManifestDirectory.CreateRootDirectory(new[] {
                ManifestDirectory.CreateDirectory("a",
                                                  new[] { new ManifestFile("test1.txt", "text.txt") }),
                ManifestDirectory.CreateDirectory("b",
                                                  new[] { new ManifestFile("test2.txt", "test2.txt") })
            });

            var newRoot = ((ManifestDirectory)directory.Traverse("a")).ToRootDirectory();

            // Act
            var result = newRoot.Traverse("../b/test.txt");

            // Assert
            Assert.Same(ManifestEntry.UnknownPath, result);
        }
Ejemplo n.º 14
0
        public void ResolveEntry_AllowsSingleDirectorySeparator(string path)
        {
            // Arrange
            var manifest = new EmbeddedFilesManifest(
                ManifestDirectory.CreateRootDirectory(
                    new[]
            {
                ManifestDirectory.CreateDirectory("wwwroot",
                                                  new[]
                {
                    new ManifestFile("jquery.validate.js", "wwwroot.jquery.validate.js")
                })
            }));
            // Act
            var entry = manifest.ResolveEntry(path);

            // Assert
            Assert.NotNull(entry);
        }