/* 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); }
/// <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); } } }