Example #1
0
        /* Glob */

        /// <summary>
        /// Expands the name of each environment variable (%variable%) and 'up directory' expression ('..\') embedded in the <see cref="Glob" />.
        /// </summary>
        /// <param name="pattern">The glob pattern.</param>
        /// <param name="directory">The current directory (default: <see cref="Environment.CurrentDirectory"/>).</param>
        /// <param name="expandVariables">if set to <c>true</c> expands the environment variables within the <paramref name="pattern" /> and <paramref name="directory" />.</param>
        /// <returns>A string with each variable replaced by its value.</returns>
        public static string Expand(this Glob pattern, string directory = null, bool expandVariables = false)
        {
            if (string.IsNullOrEmpty(directory))
            {
                directory = Environment.CurrentDirectory;
            }
            if (pattern == null)
            {
                return(expandVariables ? Environment.ExpandEnvironmentVariables(directory) : directory);
            }

            int totalOperators = CountUpOperators(pattern.ToString(), out string relativePath);

            directory = MoveUpDirectory(directory, totalOperators);

            directory = Path.Combine(directory, relativePath ?? string.Empty);
            return(expandVariables ? Environment.ExpandEnvironmentVariables(directory) : directory);
        }
Example #2
0
        /// <summary>
        /// Returns the files from the specified directory that matches the <paramref name="pattern"/>.
        /// </summary>
        /// <param name="pattern">The glob pattern.</param>
        /// <param name="directory">The current directory (default: <see cref="Environment.CurrentDirectory"/>).</param>
        /// <param name="searchOption">One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories. (default <see cref="SearchOption.AllDirectories"/>)</param>
        /// <param name="expandVariables">if set to <c>true</c> expands the environment variables within the <paramref name="pattern"/> and <paramref name="directory"/>.</param>
        /// <returns>The files that match the glob pattern from the directory.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="pattern"/> is null.</exception>
        /// <exception cref="DirectoryNotFoundException"><paramref name="directory" /> do not exist.</exception>
        public static IEnumerable <string> ResolvePaths(this Glob pattern, string directory = null, SearchOption searchOption = SearchOption.AllDirectories, bool expandVariables = false)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            // Checking to see if the pattern denotes a existing path. If so, return it.
            string absolutePath = (expandVariables ? Environment.ExpandEnvironmentVariables(pattern.ToString()) : pattern.ToString());

            if (File.Exists(absolutePath))
            {
                yield return(absolutePath);

                yield break;
            }

            // Lookup all files in the directory that match the pattern.
            if (string.IsNullOrEmpty(directory))
            {
                directory = Environment.CurrentDirectory;
            }
            if (expandVariables)
            {
                directory = Environment.ExpandEnvironmentVariables(directory);
            }
            if (!Directory.Exists(directory))
            {
                throw new DirectoryNotFoundException($"Could not find folder at '{directory}'.");
            }

            directory = MoveUpDirectory(directory, CountUpOperators(pattern, out string trimmedPattern));

            foreach (string path in Directory.EnumerateFiles(directory, "*", searchOption))
            {
                if (pattern.IsMatch(path, expandVariables))
                {
                    yield return(path);
                }
            }
        }
Example #3
0
 public bool IsMatch()
 {
     return(Glob.IsMatch(@"C:\Temp\GlobN\mock\lvl-A\file.txt", Pattern));
 }
Example #4
0
        /* Collections */

        /// <summary>
        /// Returns the items from the specified <paramref name="collection"/> that matches the <paramref name="pattern"/>.
        /// </summary>
        /// <param name="collection">The collection of file paths.</param>
        /// <param name="pattern">The glob pattern.</param>
        /// <param name="negate">if set to <c>true</c> return the files that do not match instead.</param>
        /// <returns>The files that matches the pattern.</returns>
        public static IEnumerable <string> Filter(this IEnumerable <string> collection, Glob pattern, bool negate = false)
        {
            foreach (var file in collection)
            {
                if (pattern.IsMatch(file) == !negate)
                {
                    yield return(file);
                }
            }
        }
Example #5
0
 /// <summary>
 /// Returns the files from the specified directory that matches the <paramref name="pattern"/>.
 /// </summary>
 /// <param name="directory">The current directory.</param>
 /// <param name="pattern">The glob pattern.</param>
 /// <param name="searchOption">One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories. (default <see cref="SearchOption.AllDirectories"/>)</param>
 /// <param name="expandVariables">if set to <c>true</c> expands the environment variables within the <paramref name="pattern"/> and <paramref name="directory"/>.</param>
 /// <returns>The files that match the glob pattern from the directory.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="pattern"/> is null.</exception>
 /// <exception cref="DirectoryNotFoundException"><paramref name="directory" /> do not exist.</exception>
 public static IEnumerable <FileInfo> ResolvePaths(this DirectoryInfo directory, Glob pattern, SearchOption searchOption = SearchOption.AllDirectories, bool expandVariables = false)
 {
     foreach (string filePath in ResolvePaths(pattern, directory.FullName, searchOption, expandVariables))
     {
         yield return(new FileInfo(filePath));
     }
     ;
 }