/// <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());
        }
Ejemplo n.º 2
0
        public bool TryImport(string requestedFile, string parentPath, out string scss, out string map)
        {
            scss = null;
            map  = null;

            // Modify the requested file if we have an import path function
            string modifiedParentPath = null;

            if (_importPathFunc != null)
            {
                requestedFile      = _importPathFunc(requestedFile);
                modifiedParentPath = _importPathFunc(parentPath);
            }
            if (string.IsNullOrWhiteSpace(requestedFile))
            {
                return(false);
            }

            // Get the input relative path to the parent file
            // Make sure to try checking for a previously processed parent post-modification
            FilePath parentFilePath    = new FilePath(parentPath);
            FilePath requestedFilePath = new FilePath(requestedFile);

            if (parentFilePath.IsRelative &&
                !_parentAbsolutePaths.TryGetValue(parentFilePath, out parentFilePath) &&
                (modifiedParentPath == null || !_parentAbsolutePaths.TryGetValue(modifiedParentPath, out parentFilePath)))
            {
                // Relative parent path and no available absolute path, try with the relative path
                parentFilePath = new FilePath(parentPath);
            }

            // Try to get the relative path to the parent file from inside the input virtual file system
            // But if the parent file isn't under an input path, just use it directly
            DirectoryPath containingInputPath = _fileSystem.GetContainingInputPath(parentFilePath);
            FilePath      parentRelativePath  = containingInputPath != null
                ? containingInputPath.GetRelativePath(parentFilePath)
                : parentFilePath;

            // Find the requested file by first combining with the parent
            FilePath filePath = parentRelativePath.Directory.CombineFile(requestedFilePath);

            if (GetFileVariations(filePath, requestedFilePath, out scss))
            {
                return(true);
            }

            // That didn't work so try it again as a relative path from the input folder
            if (!requestedFilePath.IsAbsolute && GetFileVariations(requestedFilePath, requestedFilePath, out scss))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        public async Task <string> TryImportAsync(string requestedFile, string parentPath)
        {
            // Modify the requested file if we have an import path function
            string modifiedParentPath = null;

            if (_importPathFunc != null)
            {
                requestedFile      = _importPathFunc(requestedFile);
                modifiedParentPath = _importPathFunc(parentPath);
            }
            if (string.IsNullOrWhiteSpace(requestedFile))
            {
                return(null);
            }

            // Get the input relative path to the parent file
            // Make sure to try checking for a previously processed parent post-modification
            NormalizedPath parentFilePath    = new NormalizedPath(parentPath);
            NormalizedPath requestedFilePath = new NormalizedPath(requestedFile);

            if (parentFilePath.IsRelative &&
                !_parentAbsolutePaths.TryGetValue(parentFilePath, out parentFilePath) &&
                (modifiedParentPath == null || !_parentAbsolutePaths.TryGetValue(modifiedParentPath, out parentFilePath)))
            {
                // Relative parent path and no available absolute path, try with the relative path
                parentFilePath = new NormalizedPath(parentPath);
            }

            // Try to get the relative path to the parent file from inside the input virtual file system
            // But if the parent file isn't under an input path, just use it directly
            NormalizedPath containingInputPath = _fileSystem.GetContainingInputPath(parentFilePath);
            NormalizedPath parentRelativePath  = containingInputPath.IsNull
                ? parentFilePath
                : containingInputPath.GetRelativePath(parentFilePath);

            // Find the requested file by first combining with the parent
            NormalizedPath filePath = parentRelativePath.ChangeFileName(requestedFilePath);
            string         scss     = await GetFileVariationsAsync(filePath, requestedFilePath);

            if (scss != null)
            {
                return(scss);
            }

            // That didn't work so try it again as a relative path from the input folder
            scss = await GetFileVariationsAsync(requestedFilePath, requestedFilePath);

            if (!requestedFilePath.IsAbsolute && scss != null)
            {
                return(scss);
            }

            return(null);
        }
Ejemplo n.º 4
0
        public bool TryImport(string requestedFile, string parentPath, out string scss, out string map)
        {
            scss = null;
            map  = null;

            // Get the input relative path to the parent file
            FilePath parentFilePath    = new FilePath(parentPath);
            FilePath requestedFilePath = new FilePath(requestedFile);

            if (parentFilePath.IsRelative && !_parentAbsolutePaths.TryGetValue(parentFilePath, out parentFilePath))
            {
                // Relative parent path and no available absolute path, try with the relative path
                parentFilePath = new FilePath(parentPath);
            }
            DirectoryPath containingPath = _fileSystem.GetContainingInputPath(parentFilePath);

            if (containingPath == null)
            {
                // Couldn't find the containing path, give up at this point
                return(false);
            }
            FilePath parentRelativePath = containingPath.GetRelativePath(parentFilePath);

            // Find the requested file
            // ...as specified
            FilePath filePath = parentRelativePath.Directory.CombineFile(requestedFilePath);

            if (GetFile(filePath, requestedFilePath, out scss))
            {
                return(true);
            }

            // ...with extension (if not already)
            if (!filePath.HasExtension || filePath.Extension != ".scss")
            {
                FilePath extensionPath = filePath.AppendExtension(".scss");
                if (GetFile(extensionPath, requestedFilePath, out scss))
                {
                    return(true);
                }

                // ...and with underscore prefix (if not already)
                if (!extensionPath.FileName.FullPath.StartsWith("_"))
                {
                    extensionPath = extensionPath.Directory.CombineFile("_" + extensionPath.FileName.FullPath);
                    if (GetFile(extensionPath, requestedFilePath, out scss))
                    {
                        return(true);
                    }
                }
            }

            // ...with underscore prefix (if not already)
            if (!filePath.FileName.FullPath.StartsWith("_"))
            {
                filePath = filePath.Directory.CombineFile("_" + filePath.FileName.FullPath);
                if (GetFile(filePath, requestedFilePath, out scss))
                {
                    return(true);
                }
            }

            return(false);
        }