查找与MSBuild的父文件夹的文件 [英] Find a file in the parent folders with msbuild

查看:139
本文介绍了查找与MSBuild的父文件夹的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的MSBuild就可以创建一个由 Microsoft.Common.Targets build.proj.user 文件$ C>构建文件。

In MsBuild it is possible to create a build.proj.user file which is parsed by the Microsoft.Common.Targets build file.

我想有一个类似的系统在地方,可以在该文件夹的根目录。用户文件并进行的MSBuild拿起配置设置从该文件。

I want to have a similar system in place where it is possible to have a .user file in the root of the folder and make msbuild pick up the config settings from this file.

就拿这些路径:

c:\working\build.proj.user
c:\working\solution1\build.proj.user
c:\working\solution1\project1\
c:\working\solution1\project2\
c:\working\solution1\project3\build.proj.user

c:\working\solution2\
c:\working\solution2\project1\
c:\working\solution2\project2\

我想做到这一点的解决方法1 / PROJECT1文件 C:\工作\解决方法1 \ build.proj.user 的读取和溶液2 / PROJECT1文件 C:\工作\ build.proj.user

I want to achieve that for solution1/project1 the file c:\working\solution1\build.proj.user is read and for solution2/project1 the file c:\working\build.proj.user

的目的是允许集成测试的ConnectionString属性每溶液和或项目进行定制。

The purpose is to allow integration test connectionstring properties to be customized per solution and or project.

目前的解决方案,我看到的是:

The current solutions I see are:

  • 请自定义的MSBuild任务,会去寻找这个文件
  • 构造一个shell命令来查找文件。
  • 有它硬codeD看看父路径的父和父

我不是任何一个解决方案的粉丝,不知道如果没有达到我的目标(用的MSBuild)的更优雅的方式。

I am not a fan of either solution and wonder if there isn't a more elegant way of achieving my goal (with msbuild).

推荐答案

添加到您的项目文件:

<Import Project="build.proj.user" Condition="Exists('build.proj.user')"/>
<Import Project="..\build.proj.user" Condition="!Exists('build.proj.user') and Exists('..\build.proj.user')"/>
<Import Project="..\..\build.proj.user" Condition="!Exists('build.proj.user') and !Exists('..\build.proj.user') and Exists('..\..\build.proj.user')"/>

编辑: 您也可以使用的MSBuild内联任务该做的。这是有点慢,但更通用的:) 内联任务是从的MSBuild 4.0支持

You can also do it using MsBuild inline task. It is little bit slower, but more generic :) Inline tasks are supported from MsBuild 4.0

<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="4.0">
  <UsingTask TaskName="FindUserFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <CurrentDirName ParameterType="System.String" Required="true" />
      <FileToFind ParameterType="System.String" Required="true" />
      <UserFileName ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System"/>
      <Using Namespace="System.IO"/>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
          Log.LogMessage("FindUserFile parameters:");
          Log.LogMessage("CurrentDirName = " + CurrentDirName);
          Log.LogMessage("FileToFind = " + FileToFind);

          while(CurrentDirName != Directory.GetDirectoryRoot(CurrentDirName) && !File.Exists(CurrentDirName + Path.DirectorySeparatorChar + FileToFind))
             CurrentDirName = Directory.GetParent(CurrentDirName).FullName;
          if(File.Exists(CurrentDirName + Path.DirectorySeparatorChar + FileToFind)) 
             UserFileName = CurrentDirName + Path.DirectorySeparatorChar + FileToFind;

          Log.LogMessage("FindUserFile output properties:");
          Log.LogMessage("UserFileName = " + UserFileName);
        ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="FindUserFileTest" >
    <FindUserFile CurrentDirName="$(MSBuildThisFileDirectory)" FileToFind="build.proj.user">
     <Output PropertyName="UserFileName" TaskParameter="UserFileName" />
    </FindUserFile>

    <Message Text="UserFileName = $(UserFileName)"/>
    <Error Condition="!Exists('$(UserFileName)')" Text="File not found!"/>

  </Target>
</Project>

如何工作: FindUserFile是用C#语言的内联任务。它试图找到FileToFind参数中指定的文件。然后循环槽所有父文件夹,并返回UserFileName输出特性的FileToFind文件中第一次出现。 UserFileName输出特性是,如果没有找到文件空字符串。

How it works: FindUserFile is inline task written in C# language. It tries to find file specified in FileToFind parameter. Then iterate trough all parent folders and it returns the first occurrence of the FileToFind file in UserFileName output property. UserFileName output property is empty string if file was not found.

这篇关于查找与MSBuild的父文件夹的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆