54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
namespace SQLLinter.Common.Helpers;
|
|
|
|
public static class FileHelpers
|
|
{
|
|
public static List<string> FindFilesWithMask(List<string> paths)
|
|
{
|
|
return paths.SelectMany(path =>
|
|
{
|
|
var fullPath = Path.GetFullPath(path);
|
|
var directory = Path.GetDirectoryName(fullPath);
|
|
if (directory == null) return Enumerable.Empty<string>();
|
|
|
|
string pattern = Path.GetFileName(fullPath);
|
|
|
|
// Если маска есть в директории
|
|
if (directory.Contains("*") || directory.Contains("?"))
|
|
{
|
|
var root = Path.GetPathRoot(directory);
|
|
if (root == null) return Enumerable.Empty<string>();
|
|
|
|
string relative = directory.Substring(root.Length);
|
|
|
|
return ExpandDirectories(root, relative)
|
|
.SelectMany(dir => Directory.EnumerateFiles(dir, pattern));
|
|
}
|
|
else
|
|
{
|
|
return Directory.Exists(directory)
|
|
? Directory.EnumerateFiles(directory, pattern)
|
|
: Enumerable.Empty<string>();
|
|
}
|
|
}).ToList();
|
|
}
|
|
|
|
public static IEnumerable<string> ExpandDirectories(string root, string relative)
|
|
{
|
|
string[] parts = relative.Split(Path.DirectorySeparatorChar);
|
|
|
|
IEnumerable<string> dirs = new[] { root };
|
|
|
|
foreach (var part in parts)
|
|
{
|
|
dirs = part.Contains("*") || part.Contains("?")
|
|
? dirs.SelectMany(d => Directory.Exists(d)
|
|
? Directory.EnumerateDirectories(d, part)
|
|
: Enumerable.Empty<string>())
|
|
: dirs.Select(d => Path.Combine(d, part));
|
|
}
|
|
|
|
return dirs.Where(Directory.Exists);
|
|
}
|
|
|
|
}
|