Here's a patch for the <if> tasks that allows the conditional of whether a script is being run on a specific version of Windows. I haven't added tests since it calls straight into the .Net OperatingSystem class (which would have to be mocked). This functionality could probably be extended something along the lines of 'windowsVersionAtLeast' , checks for running on Mono, etc.
I needed to do this to do different things depending on whether I'm running on a machine with IIS 5 or 6 (so I needed to check for 'Windows Server 2003')
I use Nant every day so thanks to everyone who's contributed code.
Cheers,
Mike
--
Index: IfTask.cs
===================================================================
RCS file: /cvsroot/nant/nant/src/NAnt.Core/Tasks/IfTask.cs,v
retrieving revision 1.17
diff -u -r1.17 IfTask.cs
--- IfTask.cs 25 May 2003 08:20:25 -0000 1.17
+++ IfTask.cs 3 Jun 2003 02:53:09 -0000
@@ -79,8 +79,29 @@
/// </if>
/// ]]></code>
/// </example>
- ///
- ///
+ /// <example>
+ /// <para>Check running on a specific version of Windows</para>
+ /// <code>
+ /// <![CDATA[
+ /// <target name="myTarget"/>
+ /// <if windowsversion="Windows XP">
+ /// <echo message="Running on Windows XP."/>
+ /// </if>
+ /// ]]></code>
+ /// <para>
+ /// You can check for the following versions:
+ /// <list type="bullet">
+ /// <item>Windows 95</item>
+ /// <item>Windows 98</item>
+ /// <item>Windows Me</item>
+ /// <item>Windows NT 3.51</item>
+ /// <item>Windows NT 4.0</item>
+ /// <item>Windows 2000 (any version)</item>
+ /// <item>Windows XP</item>
+ /// <item>Windows Server 2003</item>
+ /// </list>
+ /// </para>
+ /// </example>
/// <para>
/// Note: For dates you probably want to use <ifnot/>.
/// That way you can say, if these files aren't uptodate, then do this.
@@ -112,6 +133,7 @@
private string _propNameTrue = null;
private string _propNameExists = null;
private string _targetName = null;
+ private string _windowsVersion = null;
private string _uptodateFile = null;
private FileSet _compareFiles = null;
@@ -177,11 +199,21 @@ set { _targetName = value; } }
- #endregion Public Instance Properties + /// <summary> + /// Used to test whether running on a specific version of windows. + /// </summary> + [TaskAttribute("windowsversion")] + public string WindowsVersion { + get { return _windowsVersion; } + set { _windowsVersion = value; } + } + + #endregion Public Instance Properties
#region Protected Instance Properties
- protected virtual bool ConditionsTrue { + protected virtual bool ConditionsTrue + { get { bool ret = true;
@@ -191,6 +223,12 @@ if (!ret) return false; }
+ //check Windows Version + if(_windowsVersion != null) + { + return checkWindowsVersion(); + } + //Check for Property existence if(_propNameExists != null) { ret = ret && (Properties[_propNameExists] != null); @@ -231,9 +269,78 @@
#endregion Protected Instance Properties
+ private bool checkWindowsVersion()
+ {
+ // Largely Copied from MSDN Library - ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/enu_kbvcsharpnetkb/vcsharpnetkb/304283.htm
+ // Get OperatingSystem information from the system namespace.
+ System.OperatingSystem osInfo = System.Environment.OSVersion;
+ + // Determine the platform.
+ switch(osInfo.Platform)
+ {
+ // Platform is Windows 95, Windows 98,
+ // Windows 98 Second Edition, or Windows Me.
+ case System.PlatformID.Win32Windows:
+ + switch (osInfo.Version.Minor)
+ {
+ case 0:
+ if (_windowsVersion.Trim().ToLower().Equals("windows 95"))
+ return true;
+ break;
+ case 10:
+ if (_windowsVersion.Trim().ToLower().Equals("windows 98"))
+ return true;
+ break;
+ case 90:
+ if (_windowsVersion.Trim().ToLower().Equals("windows me"))
+ return true;
+ break;
+ }
+ break;
+ + // Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,
+ // or Windows XP.
+ case System.PlatformID.Win32NT:
+
+ switch(osInfo.Version.Major)
+ {
+ case 3:
+ if (_windowsVersion.Trim().ToLower().Equals("windows nt 3.51"))
+ return true;
+ break;
+ case 4:
+ if (_windowsVersion.Trim().ToLower().Equals("windows nt 4.0"))
+ return true;
+ break;
+ case 5:
+ if (osInfo.Version.Minor==0)
+ {
+ if (_windowsVersion.Trim().ToLower().Equals("windows 2000"))
+ return true;
+ }
+ else if (osInfo.Version.Minor == 1)
+ {
+ if (_windowsVersion.Trim().ToLower().Equals("windows xp"))
+ return true;
+ }
+ else if (osInfo.Version.Minor == 2)
+ {
+ if (_windowsVersion.Trim().ToLower().Equals("windows server 2003"))
+ return true;
+ }
+ break;
+ }break;
+ }
+
+ return false;
+ }
+
+
#region Override implementation of TaskContainer
- protected override void ExecuteTask() { + protected override void ExecuteTask() + { if(ConditionsTrue) { base.ExecuteTask(); }
------------------------------------------------------- This SF.net email is sponsored by: eBay Get office equipment for less on eBay! http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5 _______________________________________________ Nant-developers mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/nant-developers