比较Msbuild中的DateTime标记

时间:2013-09-11 21:18:17

标签: c# msbuild

我正在尝试比较msbuild中的两个日期时间戳。我做了以下事情:

<CreateItem Include="@(Compile)->'@(Compile).cache'" 
            Condition="('%(Compile.ExcludeFromStyleCop)' != 'true') and ('%(Compile.ExcludeFromSourceAnalysis)' != 'true') and (@(Compile.ModifiedTime) > @(Compile.cache.ModifiedTime))">
    <Output TaskParameter="Include" ItemName="StyleCopFiles"/>
</CreateItem>

然而,它引发了以下内容:

  

错误MSB4086:尝试进行数值比较   “@(Compile.ModifiedTime)”评估为“@(Compile.ModifiedTime)”   而不是数字,条件为“('%(Compile.ExcludeFromStyleCop)'   !='true')和('%(Compile.ExcludeFromSourceAnalysis)'!='true')和   (@(Compile.ModifiedTime)&gt; @(Compile.cache.ModifiedTime))“。   [C:\ dev的\易于\ DotNetMvc \ SRC \ Apt.Lib.Data.Elmah \ Apt.Lib.Data.Elmah.csproj]

如何比较msbuild中的两个日期时间戳?

3 个答案:

答案 0 :(得分:5)

尝试使用内联任务:(我想是msbuild v4 +)(我没有测试过)

<UsingTask
    TaskName="CompareDates"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup>
      <FirstDate ParameterType="System.DateTime" Required="true" />
      <SecondDate ParameterType="System.DateTime" Required="true" />
      <Result ParameterType=" "System.Int32" Output="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System"/>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
        Log.LogMessage("First Date: " + FirstDate, MessageImportance.High);
        Log.LogMessage("Second Date: " + SecondDate, MessageImportance.High);
        Result = DateTime.Compare(FirstDate, SecondDate);
        ]]>
      </Code>
    </Task>
  </UsingTask>

MsBuild脚本中的用法:

<CompareDates FirstDate="$(FirstDate)" SecondDate="$(SecondDate)">
      <Output TaskParameter="Result" PropertyName="DateCompareResult"/>
</CompareDates>

<Copy Condition="$(DateCompareResult)=='0'" .../>

答案 1 :(得分:4)

这里更简单的解决方案是使用属性函数将DateTime对象转换为整数以访问Ticks属性。

对于命名文件:

$([System.IO.File]::GetLastWriteTime('SomeFile.bin').Ticks)

或来自项目元数据:

$([System.DateTime]::Parse('%(ItemGroup.ModifiedTime)').Ticks)

然后,您可以使用条件表达式比较项目,如下所示:

Condition="($([System.DateTime]::Parse('%(ItemGroup.ModifiedTime)').Ticks) > $([System.IO.File]::GetLastWriteTime('SomeFile').Ticks)))"

对于这里的问题,我认为这应该有效:

Condition="($([System.DateTime]::Parse('%(Compile.ModifiedTime)').Ticks) > ($([System.DateTime]::Parse('%(Compile.cache.ModifiedTime)').Ticks)"

答案 2 :(得分:1)

以下是我们的用途:

<UsingTask
      TaskName="CompareDates"
      TaskFactory="CodeTaskFactory"
      AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >

      <ParameterGroup>
        <FileList ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
        <CacheFile ParameterType="System.String" Required="true" />
        <Result ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
      </ParameterGroup>
      <Task>
        <Using Namespace="System"/>
        <Code Type="Fragment" Language="cs">
          <![CDATA[
          var modifiedFiles = new List<ITaskItem>();
          //If the cache file exists, only return files that have been edited more recently than it
          if(File.Exists(CacheFile))
          {
            DateTime cacheTimeStamp = File.GetLastWriteTime(CacheFile);
            foreach(var file in FileList)
            {
                if(DateTime.Compare(DateTime.Parse(file.GetMetadata("ModifiedTime")), cacheTimeStamp) > 0)
                {
                    modifiedFiles.Add(file);
                }
            }
                    Result = modifiedFiles.ToArray();
          }
          //Otherwise, return all files
          else
          {
                Result = FileList;
          }
          ]]>
        </Code>
      </Task>
  </UsingTask>

我们使用如下:

<CompareDates FileList="@(Compile)" CacheFile="My.StyleCop.Cache">
    <Output TaskParameter="Result" ItemName="ChangedFiles"/>
</CompareDates>

更新缓存(执行stylecop任务后):

<Touch
    Files="My.StyleCop.cache"
    AlwaysCreate="true"
    Condition="'$(StyleCopViolationCount)' == '0'"/>
<CreateItem Include="Apt.StyleCop.cache">`enter code here`
    <Output TaskParameter="Include" ItemName="FileWrites"/>
</CreateItem>