// NAnt - A .NET build tool
// Copyright (C) 2001-2003 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Gerry Shaw (gerry_shaw@yahoo.com)
using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using NAnt.Core.Attributes;
using NAnt.Core.Functions;
namespace NAnt.Core.Tasks {
///
/// Sets properties with system information.
///
///
/// Sets a number of properties with information about the system environment. The intent of this task is for nightly build logs to have a record of system information so that the build was performed on.
///
///
/// Property
/// Value
///
/// -
/// <>.clr.version
/// Common Language Runtime version number.
///
/// -
/// <>.env.*
/// Environment variables (e.g., <>.env.PATH).
///
/// -
/// <>.os.platform
/// Operating system platform ID.
///
/// -
/// <>.os.version
/// Operating system version.
///
/// -
/// <>.os
/// Operating system version string.
///
/// -
/// <>.os.folder.applicationdata
/// The directory that serves as a common repository for application-specific data for the current roaming user.
///
/// -
/// <>.os.folder.commonapplicationdata
/// The directory that serves as a common repository for application-specific data that is used by all users.
///
/// -
/// <>.os.folder.commonprogramfiles
/// The directory for components that are shared across applications.
///
/// -
/// <>.os.folder.desktopdirectory
/// The directory used to physically store file objects on the desktop. Do not confuse this directory with the desktop folder itself, which is a virtual folder.
///
/// -
/// <>.os.folder.programfiles
/// The Program Files directory.
///
/// -
/// <>.os.folder.system
/// The System directory.
///
/// -
/// <>.os.folder.temp
/// The temporary directory.
///
///
///
/// When the name of an environment variable is not a valid property name,
/// the task will fail. In that case, set to
/// to allow that environment variable to be
/// skipped.
///
///
/// we advise you to use the following functions instead:
///
///
///
/// Function
/// Description
///
/// -
///
/// Gets a object that identifies this operating system.
///
/// -
///
/// Gets the path to a system special folder.
///
/// -
///
/// Returns the value of a environment variable.
///
/// -
///
/// Gets the path to the temporary directory.
///
/// -
///
/// Gets the Common Language Runtime version.
///
///
///
///
/// Register the properties with the default property prefix.
///
///
/// ]]>
///
///
///
/// Register the properties without a prefix.
///
///
/// ]]>
///
///
///
/// Register properties and display a summary.
///
///
/// ]]>
///
///
[TaskName("sysinfo")]
public class SysInfoTask : Task {
#region Private Instance Fields
private string _prefix = "sys.";
#endregion Private Instance Fields
#region Public Instance Properties
///
/// The string to prefix the property names with. The default is "sys.".
///
[TaskAttribute("prefix", Required=false)]
public string Prefix {
get { return _prefix; }
set { _prefix = value; }
}
#endregion Public Instance Properties
#region Override implementation of Task
protected override void ExecuteTask() {
Log(Level.Info, "Setting system information properties under " + Prefix + "*");
// set properties
Properties[Prefix + "clr.version"] = Environment.Version.ToString();
Properties[Prefix + "os.platform"] = Environment.OSVersion.Platform.ToString(CultureInfo.InvariantCulture);
Properties[Prefix + "os.version"] = Environment.OSVersion.Version.ToString();
Properties[Prefix + "os.folder.applicationdata"] = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Properties[Prefix + "os.folder.commonapplicationData"] = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
Properties[Prefix + "os.folder.commonprogramFiles"] = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
Properties[Prefix + "os.folder.desktopdirectory"] = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Properties[Prefix + "os.folder.programfiles"] = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
Properties[Prefix + "os.folder.system"] = Environment.GetFolderPath(Environment.SpecialFolder.System);
Properties[Prefix + "os.folder.temp"] = Path.GetTempPath();
Properties[Prefix + "os"] = Environment.OSVersion.ToString();
// set environment variables
IDictionary variables = Environment.GetEnvironmentVariables();
foreach (string name in variables.Keys) {
try {
Properties[Prefix + "env." + name] = (string) variables[name];
} catch (Exception ex) {
if (!FailOnError) {
Log(Level.Warning, "Property could not be created for"
+ " environment variable '{0}' : {1}", name,
ex.Message);
} else {
throw;
}
}
}
// display the properties
if (Verbose) {
foreach (DictionaryEntry entry in Properties) {
string name = (string) entry.Key;
if (name.StartsWith(Prefix)) {
Log(Level.Info, name + " = " + entry.Value.ToString());
}
}
}
}
#endregion Override implementation of Task
}
}