|
Maybe this isn't the right way to do this, but I needed to be able to compare file contents (not just timestamps), and I couldn't find another way to do this comparison with existing code. School me if I'm just lost. :)
I'm proposing the addition of an attribute called difffile to if/ifnot which will work with the attribute comparefile (or the element comparefiles, which is just collateral damage to me) to determine whether the contents of difffile matches the contents of comparefile.
I'm happy with the way it's working for me, but I'm open to suggestions that will make it more ubiquitous if somebody's got a better idea than this patcheroo. My documentation sux, sorry; I'm learnin' better XML commentary and NDoc sometime next month.
--- copy of iftask.c_ Sun May 25 20:20:24 2003 +++ iftask.cs Thu Sep 04 00:17:52 2003 @@ -105,6 +105,30 @@ /// </if> /// ]]></code> /// </example> + /// + /// + /// <example> + /// <para> + /// Compares the contents of the source file with one or more target files, + /// if different do something. + /// </para> + /// <code> + /// <![CDATA[ + /// <if difffile="myfile.cs" comparefile="shadow\myfile.cs"> + /// <echo message="myfile.cs is different than shadow\myfile.cs" /> + /// </if> + /// ]]></code> + /// or + /// <code> + /// <![CDATA[ + /// <if difffile="myfile.cs"> + /// <comparefiles> + /// <includes name="*.cs"/> + /// </comparefiles> + /// <echo message="myfile.cs is different than at least one of shadow\*.cs"/> + /// </if> + /// ]]></code> + /// </example> [TaskName("if")] public class IfTask : TaskContainer { #region Private Instance Fields @@ -114,21 +138,34 @@ private string _targetName = null; private string _uptodateFile = null; private FileSet _compareFiles = null; + private string _diffFile = null;
#endregion Private Instance Fields
#region Public Instance Properties
- /// <summary> - /// The file to compare if uptodate - /// </summary> - [TaskAttribute("uptodatefile")] - public string PrimaryFile { - get { return _uptodateFile; } - set {_uptodateFile = Project.GetFullPath(value);} - } + /// <summary> + /// The file to compare if different contents; + /// requires that comparefile be set also. + /// </summary> + [TaskAttribute("difffile")] + public string DiffFile + { + get { return _diffFile; } + set { _diffFile = Project.GetFullPath(value); } + } + + /// <summary> + /// The file to compare if uptodate + /// </summary> + [TaskAttribute("uptodatefile")] + public string PrimaryFile + { + get { return _uptodateFile; } + set {_uptodateFile = Project.GetFullPath(value);} + }
- /// <summary> + /// <summary> /// The file to check against for the uptodate file. /// </summary> [TaskAttribute("comparefile")] @@ -225,6 +262,45 @@ if (!ret) return false; }
+ // Compare contents of difffile and comparefile(s). + if(_diffFile != null) { + FileInfo thisFile = new FileInfo(_diffFile); + FileInfo thatFile = null; + if(thisFile == null) + { + ret = false; + } + else + { + StreamReader thisSR = new StreamReader(_diffFile), thatSR = null; + string thisFileContents = thisSR.ReadToEnd(), thatFileContents = null; + bool isAMatch = false; + foreach( string fileName in _compareFiles.FileNames ) + { + if(!isAMatch) + { + thatFile = new FileInfo(fileName); + thatSR = new StreamReader(fileName); + thatFileContents = thatSR.ReadToEnd(); + isAMatch = (thisFileContents != thatFileContents); + if(isAMatch) + { + Log(Level.Verbose, "{2}:{0} is different than {1}.", thatFile.Name, thisFile.Name, LogPrefix); + } + else + { + Log(Level.Verbose, "{2}:{0} is the same as {1}.", thatFile.Name, thisFile.Name, LogPrefix); + } + } + } + // Don't forget to hang up when you're done -- the GAC is slow sometimes. + thisSR.Close(); + thatSR.Close(); + ret = ret && isAMatch; + } + if(!ret) return false; + } + return ret; } } @@ -272,7 +348,31 @@ /// </if> /// ]]></code> /// </example> - [TaskName("ifnot")] + /// + /// + /// <example> + /// <para> + /// Compares the contents of the source file with one or more target files, + /// if not different do something. + /// </para> + /// <code> + /// <![CDATA[ + /// <ifnot difffile="myfile.cs" comparefile="shadow\myfile.cs"> + /// <echo message="myfile.cs is not different than shadow\myfile.cs" /> + /// </if> + /// ]]></code> + /// or + /// <code> + /// <![CDATA[ + /// <ifnot difffile="myfile.cs"> + /// <comparefiles> + /// <includes name="*.cs"/> + /// </comparefiles> + /// <echo message="myfile.cs is not different than any of shadow\*.cs"/> + /// </if> + /// ]]></code> + /// </example> + [TaskName("ifnot")] public class IfNotTask : IfTask { #region Override implementation of IfTask
|
iftask.patch
Description: iftask.patch
