使用MSBuild 4.0,是否可以“简化”包含路径的属性?

发布于 2024-12-03 10:03:27 字数 339 浏览 3 评论 0原文

假设我有一个包含的属性

c:\workdir\project\wonder\subproj\..\..\common

,并且我想将其减少/简化为

c:\workdir\project\common

此外,我还希望能够从当前目录或指定目录获取相对路径。

使用相同的起始属性并将当前工作目录设置为

c:\workdir\project\wonder

我希望它减少为

..\common

Suppose that I have a property containing

c:\workdir\project\wonder\subproj\..\..\common

and I want to reduce/simplify it to

c:\workdir\project\common

Furthermore, I would also like to be able to get a relative path from the current directory or specified.

Using the same starting property and having the current working directory set to

c:\workdir\project\wonder

I want it reduced to

..\common

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2024-12-10 10:03:27

第一部分(解析路径中的“..”)相对简单,您只需使用 属性函数

<PropertyGroup>
  <Folder>c:\workdir\project\wonder\subproj\..\..\common</Folder>
</PropertyGroup>
<Message Text="Folder $(Folder)" />     
<Message Text="Shortened path $([System.IO.Path]::GetFullPath($(Folder)))" />

输出:

Folder c:\workdir\project\wonder\subproj\..\..\common
Shortened path c:\workdir\project\common

第二部分 - 相对于另一条路径缩短一条路径 - 需要使用一个神奇的函数 Uri.MakeRelativeUri() 和将其包装在内联任务(或外部任务库)内。声明你的任务:

<UsingTask TaskName="RelativePath" TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
 <ParameterGroup>
    <Target Required="true" />
    <BaseDirectory Required="true" />
    <Result Output="true" />
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs"><![CDATA[
Uri fromUri = new Uri(new DirectoryInfo(BaseDirectory).FullName
  + Path.DirectorySeparatorChar);
Uri toUri = new Uri(new DirectoryInfo(Target).FullName);
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
Result = relativeUri.ToString().Replace('/', Path.DirectorySeparatorChar);
]]></Code>
    </Task>
</UsingTask>

然后按如下方式使用它:

<PropertyGroup>
  <Folder>c:\workdir\project\wonder\subproj\..\..\common</Folder>
  <WorkingDir>c:\workdir\project\wonder</WorkingDir>
</PropertyGroup>
<Message Text="Folder $(Folder)" />
<Message Text="Base directory $(WorkingDir)" />
<RelativePath Target="$(Folder)" BaseDirectory="$(WorkingDir)">
  <Output PropertyName="Relative" TaskParameter="Result"/>
</RelativePath>
<Message Text="Relative path $(Relative)" />

输出:

Folder c:\workdir\project\wonder\subproj\..\..\common
Base directory c:\workdir\project\wonder
Relative path ..\common

The first part (resolving '..' in path) is relatively simple, you can do this using just property functions:

<PropertyGroup>
  <Folder>c:\workdir\project\wonder\subproj\..\..\common</Folder>
</PropertyGroup>
<Message Text="Folder $(Folder)" />     
<Message Text="Shortened path $([System.IO.Path]::GetFullPath($(Folder)))" />

Output:

Folder c:\workdir\project\wonder\subproj\..\..\common
Shortened path c:\workdir\project\common

The second part - shortening one path relatively to another - requires the use of a magical function Uri.MakeRelativeUri() and wrapping it inside an inline task (or an external task library). Declare your task:

<UsingTask TaskName="RelativePath" TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
 <ParameterGroup>
    <Target Required="true" />
    <BaseDirectory Required="true" />
    <Result Output="true" />
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs"><![CDATA[
Uri fromUri = new Uri(new DirectoryInfo(BaseDirectory).FullName
  + Path.DirectorySeparatorChar);
Uri toUri = new Uri(new DirectoryInfo(Target).FullName);
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
Result = relativeUri.ToString().Replace('/', Path.DirectorySeparatorChar);
]]></Code>
    </Task>
</UsingTask>

Then use it as follows:

<PropertyGroup>
  <Folder>c:\workdir\project\wonder\subproj\..\..\common</Folder>
  <WorkingDir>c:\workdir\project\wonder</WorkingDir>
</PropertyGroup>
<Message Text="Folder $(Folder)" />
<Message Text="Base directory $(WorkingDir)" />
<RelativePath Target="$(Folder)" BaseDirectory="$(WorkingDir)">
  <Output PropertyName="Relative" TaskParameter="Result"/>
</RelativePath>
<Message Text="Relative path $(Relative)" />

Output:

Folder c:\workdir\project\wonder\subproj\..\..\common
Base directory c:\workdir\project\wonder
Relative path ..\common
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文