使用 MSBuild,如何将变量设置为在文件系统层次结构中更高位置找到的文件夹名称?
我正在使用 MSBuild 进行 CI 流程,并且正在尝试找到一种方法来获取文件系统层次结构中较高位置的文件夹的文件夹名称。该文件夹包含 CI 的所有工具。
我正在寻找的文件夹可以位于每台电脑上的不同位置。
假设 MSBuild 脚本正在 D:\Workdir\BlaBla\Project1 中执行,我感兴趣的文件夹是 D:\Workdir\CI
我希望脚本按如下方式遍历文件系统:
- 查看当前目录( D:\WorkDir\BlaBla\Project1),检查 CI 文件夹(未找到)
- 在层次结构 (D:\Workdir\BlaBla) 中向上查找 CI 文件夹(未找到)
- 再次向上(D:\Workdir) 并检查 CI 文件夹(找到!)
这在 MSBuild 中可行吗?
注意
只是为了澄清,我想在属性中获取结果(文件夹的位置)。
这是我到目前为止所拥有的并试图改进它;)
<PropertyGroup>
<CI_PathName Condition=" Exists ('..\CI') ">..\CI</CI_PathName >
<CI_PathName Condition=" Exists ('..\..\CI') ">..\..\CI</CI_PathName >
<CI_PathName Condition=" Exists ('..\..\..\CI') ">..\..\..\CI</CI_PathName >
</PropertyGroup>
这有效,但它不是最佳的......
I'm using MSBuild for our CI process and I'm trying to find a way to get the folder name of a folder higher up in the filesystem hierarchy. This folder would hold all the tools for the CI.
The folder I'm looking for can be located at various places on each PC.
Let's say that the MSBuild script is executing in the D:\Workdir\BlaBla\Project1 and the folder I'm interested in is D:\Workdir\CI
I would like the script to traverse the filesystem as follows:
- Look in current dir (D:\WorkDir\BlaBla\Project1), check for CI folder (not found)
- Go up in the hierarchy (D:\Workdir\BlaBla) and check for CI folder (not found)
- Go up again (D:\Workdir) and check for CI folder (Found!)
Is this feasible in MSBuild?
NOTES
Just to clarify, I want to get the result (the location of the folder) in a property.
Here's what I have so far and trying to better it ;)
<PropertyGroup>
<CI_PathName Condition=" Exists ('..\CI') ">..\CI</CI_PathName >
<CI_PathName Condition=" Exists ('..\..\CI') ">..\..\CI</CI_PathName >
<CI_PathName Condition=" Exists ('..\..\..\CI') ">..\..\..\CI</CI_PathName >
</PropertyGroup>
This works but it is not optimal...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用内联任务来执行此操作:
You can do this using an inline task:
是的,您可以使用“Exec”标签并在 MS Build 的任何目标中设置其“Command”属性。以文本模式打开项目文件,您可以在您可能想要的目标中添加命令(例如,如果您希望脚本在构建之前执行,则目标为“BeforeBuild”)
编辑:
以文本模式打开项目文件,导航到以下部分:。在该目标节点中添加 Exec 节点。完成后,您的目标节点可能如下所示:
Yes, you can use the "Exec" tag and set its "Command" attribute in any Target of MS Build. Open the project file in text mode and you can add your command inside the Target that you might want (for example, Target "BeforeBuild", if you want your script to be executed before the Build)
EDIT:
Open your project file in text mode, navigate to the section: <Target Name="BeforeBuild">. Add the Exec node within this Target Node. Once you are done your Target node might look like this: