Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing DateTime stamps in Msbuild

Tags:

c#

msbuild

I am trying to compare two date times stamps in msbuild. I have done the following:

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

However, it throws the following:

error MSB4086: A numeric comparison was attempted on "@(Compile.ModifiedTime)" that evaluates to "@(Compile.ModifiedTime)" instead of a number, in condition "('%(Compile.ExcludeFromStyleCop)' != 'true') and ('%(Compile.ExcludeFromSourceAnalysis)' != 'true') and (@(Compile.ModifiedTime) > @(Compile.cache.ModifiedTime))". [c:\dev\apt\DotNetMvc\src\Apt.Lib.Data.Elmah\Apt.Lib.Data.Elmah.csproj]

How does one compare two date time stamps in msbuild?

like image 457
badazzhindu Avatar asked Sep 11 '13 21:09

badazzhindu


3 Answers

A much simpler solution here is to convert the DateTime object into an integer using a property function to access the Ticks property.

For a named file:

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

Or from item metadata:

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

You can then compare items using conditional expressions like the following:

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

In the case of the question here, I believe this should work:

Condition="($([System.DateTime]::Parse('%(Compile.ModifiedTime)').Ticks) > ($([System.DateTime]::Parse('%(Compile.cache.ModifiedTime)').Ticks)"
like image 105
Roger Sanders Avatar answered Oct 16 '22 12:10

Roger Sanders


Try to use inline task: (msbuild v4+ is required, I guess) (I didn't tested it)

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

Usage in MsBuild script:

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

<Copy Condition="$(DateCompareResult)=='0'" .../>
like image 39
Ludwo Avatar answered Oct 16 '22 11:10

Ludwo


Here is what we went with:

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

We used it as follows:

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

And updated the cache (after the stylecop task is executed):

<Touch
    Files="My.StyleCop.cache"
    AlwaysCreate="true"
    Condition="'$(StyleCopViolationCount)' == '0'"/>
<CreateItem Include="Apt.StyleCop.cache">`enter code here`
    <Output TaskParameter="Include" ItemName="FileWrites"/>
</CreateItem>
like image 1
badazzhindu Avatar answered Oct 16 '22 12:10

badazzhindu