Esempio n. 1
0
        public byte[] GetBinaryFileContents(string fileName)
        {
            IFile file = _fileSystem.GetInputFile(fileName);

            using (var stream = file.OpenRead())
            {
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);
                return(buffer);
            }
        }
        /// <summary>
        /// Gets the absolute input path that contains the specified file or directory. If the provided
        /// file or directory path is absolute, this returns the input path that contains the specified
        /// path (note that the specified file or directory does not need to exist and this just returns
        /// the input path that would contain the file or directory based only on path information). If
        /// the provided path is relative, this checks all input paths for the existence of the file
        /// or directory and returns the first one where it exists.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The file path.</param>
        /// <returns>The input path that contains the specified file,
        /// or <c>null</c> if no input path does.</returns>
        public static NormalizedPath GetContainingInputPath(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
        {
            _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
            path.ThrowIfNull(nameof(path));

            if (path.IsAbsolute)
            {
                return(fileSystem.GetContainingInputPathForAbsolutePath(path));
            }

            // Try to find a file first
            IFile file = fileSystem.GetInputFile(path);

            if (file.Exists)
            {
                return(fileSystem.GetContainingInputPath(file.Path));
            }

            // Then try to find a directory
            IEnumerable <(NormalizedPath x, IDirectory)> rootDirectories =
                fileSystem.InputPaths
                .Reverse()
                .Select(x => (x, fileSystem.GetRootDirectory(x.Combine(path))));
            IEnumerable <(NormalizedPath x, IDirectory)> existingRootDirectories = rootDirectories.Where(x => x.Item2.Exists);

            return(existingRootDirectories.Select(x => fileSystem.RootPath.Combine(x.Item1)).FirstOrDefault());
        }
        private IFile GetInputFile(FilePath filePath)
        {
            // Find the requested file
            // ...as specified
            IFile file = _fileSystem.GetInputFile(filePath);

            if (file.Exists)
            {
                return(file);
            }

            // ...with extension (if not already)
            if (!filePath.HasExtension || filePath.Extension != ".less")
            {
                FilePath extensionPath = filePath.AppendExtension(".less");
                IFile    extensionFile = _fileSystem.GetInputFile(extensionPath);
                if (extensionFile.Exists)
                {
                    return(extensionFile);
                }

                // ...and with underscore prefix (if not already)
                if (!extensionPath.FileName.FullPath.StartsWith("_"))
                {
                    extensionPath = extensionPath.Directory.CombineFile("_" + extensionPath.FileName.FullPath);
                    extensionFile = _fileSystem.GetInputFile(extensionPath);
                    if (extensionFile.Exists)
                    {
                        return(extensionFile);
                    }
                }
            }

            // ...with underscore prefix (if not already)
            if (!filePath.FileName.FullPath.StartsWith("_"))
            {
                filePath = filePath.Directory.CombineFile("_" + filePath.FileName.FullPath);
                IFile underscoreFile = _fileSystem.GetInputFile(filePath);
                if (underscoreFile.Exists)
                {
                    return(underscoreFile);
                }
            }

            // Can't find it, default to the original
            return(file);
        }
        /// <inheritdoc/>
        public IFile GetFile(NormalizedPath path)
        {
            path.ThrowIfNull(nameof(path));

            if (!path.IsRelative)
            {
                throw new ArgumentException("Path must be relative", nameof(path));
            }

            return(_fileSystem.GetInputFile(Path.Combine(path)));
        }
Esempio n. 5
0
        /// <inheritdoc/>
        public IFile GetFile(FilePath path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!path.IsRelative)
            {
                throw new ArgumentException("Path must be relative", nameof(path));
            }

            return(_fileSystem.GetInputFile(Path.CombineFile(path)));
        }
Esempio n. 6
0
        private async Task <string> GetFileAsync(FilePath filePath, FilePath requestedFilePath)
        {
            string scss = null;
            IFile  file = _fileSystem.GetInputFile(filePath);

            if (file.Exists)
            {
                if (requestedFilePath.IsRelative)
                {
                    _parentAbsolutePaths.AddOrUpdate(requestedFilePath, file.Path, (x, y) => file.Path);
                }
                scss = await file.ReadAllTextAsync();
            }
            return(scss);
        }
Esempio n. 7
0
        private bool GetFile(FilePath filePath, FilePath requestedFilePath, out string scss)
        {
            scss = null;
            IFile file = _fileSystem.GetInputFile(filePath);

            if (file.Exists)
            {
                if (requestedFilePath.IsRelative)
                {
                    _parentAbsolutePaths.AddOrUpdate(requestedFilePath, file.Path, (x, y) => file.Path);
                }
                scss = file.ReadAllText();
                return(true);
            }
            return(false);
        }
Esempio n. 8
0
            private IReadOnlyFileSystem GetFileSystem()
            {
                IReadOnlyFileSystem fileSystem   = Substitute.For <IReadOnlyFileSystem>();
                IFileProvider       fileProvider = GetFileProvider();

                fileSystem.GetInputFile(Arg.Any <FilePath>()).Returns(x =>
                {
                    FilePath path = x.ArgAt <FilePath>(0);
                    if (!path.IsAbsolute)
                    {
                        path = new FilePath("/" + path.FullPath);
                    }
                    return(fileProvider.GetFile(path));
                });
                fileSystem.GetInputDirectory(Arg.Any <DirectoryPath>()).Returns(x => fileProvider.GetDirectory(x.ArgAt <DirectoryPath>(0)));
                return(fileSystem);
            }