我不记得在哪里,但找到了如何从搜索中排除文件夹的示例.我们的问题是搜索node_modules会导致长路径异常.
Func<IFileSystemInfo, bool> exclude_node_modules = fileSystemInfo=>!fileSystemInfo.Path.FullPath.Contains("node_modules");
var solutions = GetFiles("./**/*.sln", exclude_node_modules);
Run Code Online (Sandbox Code Playgroud)
解决此问题的任何帮助都会有所帮助.
为了加速文件系统的递归遍历,Cake 利用 .NET 内置功能来实现这一点,但它受到 Windows 旧的 260 个字符限制的限制。因此,当它在大多数用例中快得多时,它会在太深的文件夹结构(例如 ie Node 模块可能引入的文件夹结构)上失败。
您可以通过逐个文件夹迭代并在输入之前对要排除的文件夹应用谓词来解决此问题。
在我的示例中,使用以下文件夹结构
Repo directory
| build.cake
| test.sln
|
\---src
| test.sln
|
+---proj1
| | test.sln
| |
| \---node_modules
| node.sln
|
+---proj2
| | test.sln
| |
| \---node_modules
| node.sln
|
+---proj3
| | test.sln
| |
| \---node_modules
| node.sln
|
\---proj4
| test.sln
|
\---node_modules
node.sln
Run Code Online (Sandbox Code Playgroud)
我们想要的是从 repo 目录递归地找到所有解决方案,而不是进入该node_modules目录并且找不到node.sln
下面建议的解决方案是创建一个名为的实用方法RecursiveGetFile来为您执行此操作:
// find and iterate all solution files
foreach(var filePath in RecursiveGetFile(
Context,
"./",
"*.sln",
path=>!path.EndsWith("node_modules", StringComparison.OrdinalIgnoreCase)
))
{
Information("{0}", filePath);
}
// Utility method to recursively find files
public static IEnumerable<FilePath> RecursiveGetFile(
ICakeContext context,
DirectoryPath directoryPath,
string filter,
Func<string, bool> predicate
)
{
var directory = context.FileSystem.GetDirectory(context.MakeAbsolute(directoryPath));
foreach(var file in directory.GetFiles(filter, SearchScope.Current))
{
yield return file.Path;
}
foreach(var file in directory.GetDirectories("*.*", SearchScope.Current)
.Where(dir=>predicate(dir.Path.FullPath))
.SelectMany(childDirectory=>RecursiveGetFile(context, childDirectory.Path, filter, predicate))
)
{
yield return file;
}
}
Run Code Online (Sandbox Code Playgroud)
该脚本的输出类似于
RepoRoot/test.sln
RepoRoot/src/test.sln
RepoRoot/src/proj1/test.sln
RepoRoot/src/proj2/test.sln
RepoRoot/src/proj3/test.sln
RepoRoot/src/proj4/test.sln
Run Code Online (Sandbox Code Playgroud)
这通过跳过已知的麻烦制造者来避免 260 字符问题,如果其他未知路径有相同的问题,则不会修复。
| 归档时间: |
|
| 查看次数: |
325 次 |
| 最近记录: |