开发者

Get $(SolutionDir) in runtime

I've managed to get the $(SolutionDir) variable in runtime using this task:

<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>

The problem i have is that, as the task has no "Inputs", this project is always built. This is weird because it breaks the incremental compilation. This project is used in diferent solutions and I only want it to be compiled if I change of solution.

I've tried to use Inputs="$(SolutionPath). This works well if you only use one s开发者_运维技巧olution but when you use the project from another solution the sln.cache gets corrupted and stops building the task forever. No matter if you remove the "Inputs" variable. The only way to get it working again is to delete the .sln.cache file.

Does anyone know any way to stop the every-time-compilation of the project?

EDIT:

Ok. I've found the solution:

<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>

There are two targets. The first one creates a partial class containing a string with the $(solutiondir) value in a temporary file and compares it with the current partial class used. The second one copy the temporary file over the current if it is necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜