在运行时获取$(SolutionDir)

时间:2011-09-05 09:30:24

标签: c# visual-studio-2008 msbuild task

我已经设法在运行时使用此任务获取$(SolutionDir)变量:

<Target Name="GenerateCode" Outputs="$(IntermediateOutputPath)ResourcesPath-Constants.cs">
  <Exec Command=
        "
        echo namespace SRE.Widgets.Blend &gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        echo { &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        echo     public static partial class ResourcesPath &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        echo     { &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        echo         public static string SOLUTION_DIR = @&quot;$(SolutionDir)&quot;; &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        echo     } &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        echo } &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.cs
        " />
</Target>
<Target Name="CompileGeneratedCode">
  <ItemGroup>
    <Compile Include="$(IntermediateOutputPath)ResourcesPath-Constants.cs" />
  </ItemGroup>
</Target>
<PropertyGroup>
  <CompileDependsOn>
    GenerateCode;
    CompileGeneratedCode;
    $(CompileDependsOn);
  </CompileDependsOn>
</PropertyGroup>

我遇到的问题是,由于任务没有“输入”,因此始终会构建此项目。这很奇怪,因为它打破了增量编译。这个项目用于不同的解决方案,我只想在改变解决方案时进行编译。

我尝试使用Inputs =“$(SolutionPath)。如果你只使用一个解决方案,但是当你从另一个解决方案中使用项目时,sln.cache会被破坏并永远停止构建任务。如果删除“输入”变量。再次使用它的唯一方法是删除.sln.cache文件。

有没有人知道如何停止项目的每次编译?

编辑:

确定。我找到了解决方案:

<Target Name="GenerateTmpCode">
  <Exec
    Command=
        "
        echo namespace SRE.Widgets.Blend &gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        echo { &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        echo     public static partial class ResourcesPath &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        echo     { &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        echo         public static string SOLUTION_DIR = @&quot;$(SolutionDir)&quot;; &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        echo     } &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        echo } &gt;&gt; $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp
        fc $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp $(IntermediateOutputPath)ResourcesPath-Constants.g.cs
        "
    ContinueOnError="true">
    <Output PropertyName="CommandExitCode" TaskParameter="ExitCode"/>
  </Exec>
</Target>

<Target Name="GenerateCode"
        Condition="'$(CommandExitCode)'!='0'"
        Outputs="$(IntermediateOutputPath)ResourcesPath-Constants.cs"
        Inputs="$(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp" >
  <Exec Command=
        "
        copy $(IntermediateOutputPath)ResourcesPath-Constants.g.cs.tmp $(IntermediateOutputPath)ResourcesPath-Constants.g.cs
        " />
</Target>

<Target Name="CompileGeneratedCode">
  <ItemGroup>
    <Compile Include="$(IntermediateOutputPath)ResourcesPath-Constants.g.cs">
      <AutoGen>True</AutoGen>
    </Compile>
  </ItemGroup>
</Target>

<PropertyGroup>
  <CompileDependsOn>
    GenerateTmpCode;
    GenerateCode;
    CompileGeneratedCode;
    $(CompileDependsOn);
  </CompileDependsOn>
</PropertyGroup>

有两个目标。第一个创建一个部分类,其中包含一个临时文件中带有$(solutiondir)值的字符串,并将其与当前使用的部分类进行比较。如果有必要,第二个将临时文件复制到当前文件上。

0 个答案:

没有答案