// 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)
// Gert Driesen (gert.driesen@ardatis.com)
using System;
using System.IO;
using System.Globalization;
using NAnt.Core.Attributes;
using NAnt.Core.Functions;
using NAnt.Core.Util;
namespace NAnt.Core.Tasks {
///
/// Checks if a resource is available at runtime.
///
///
///
/// The specified property is set to if the
/// requested resource is available at runtime, and
/// if the resource is not available.
///
///
/// we advise you to use the following functions instead:
///
///
///
/// Function
/// Description
///
/// -
///
/// Determines whether the specified file exists.
///
/// -
///
/// Determines whether the given path refers to an existing directory on disk.
///
/// -
///
/// Checks whether the specified framework exists..
///
/// -
///
/// Checks whether the SDK for the specified framework is installed.
///
///
///
///
///
/// Sets the myfile.present property to if the
/// file is available on the filesystem and if the
/// file is not available.
///
///
///
/// ]]>
///
///
///
///
/// Sets the build.dir.present property to
/// if the directory is available on the filesystem and
/// if the directory is not available.
///
///
///
/// ]]>
///
///
///
///
/// Sets the mono-0.21.framework.present property to
/// if the Mono 0.21 framework is available on the current system and
/// if the framework is not available.
///
///
///
/// ]]>
///
///
///
///
/// Sets the net-1.1.frameworksdk.present property to
/// if the .NET 1.1 Framework SDK is available on the current system and
/// if the SDK is not available.
///
///
///
/// ]]>
///
///
[TaskName("available")]
[Obsolete("Use functions instead.", false)]
public class AvailableTask : Task {
///
/// Defines the possible resource checks.
///
public enum ResourceType : int {
///
/// Determines whether a given file exists.
///
File = 1,
///
/// Determines whether a given directory exists.
///
Directory = 2,
///
/// Determines whether a given framework is available.
///
Framework = 3,
///
/// Determines whether a given SDK is available.
///
FrameworkSDK = 4
}
#region Public Instance Properties
///
/// The resource which must be available.
///
[TaskAttribute("resource", Required=true)]
[StringValidator(AllowEmpty=false)]
public string Resource {
get { return _resource; }
set { _resource = StringUtils.ConvertEmptyToNull(value); }
}
///
/// The type of resource which must be present.
///
[TaskAttribute("type", Required=true)]
public ResourceType Type {
get { return _resourceType; }
set {
if (!Enum.IsDefined(typeof(ResourceType), value)) {
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "An invalid type {0} was specified.", value));
} else {
this._resourceType = value;
}
}
}
///
/// The property that must be set if the resource is available.
///
[TaskAttribute("property", Required=true)]
[StringValidator(AllowEmpty=false)]
public string PropertyName {
get { return _propertyName; }
set { _propertyName = StringUtils.ConvertEmptyToNull(value); }
}
#endregion Public Instance Properties
#region Override implementation of Task
///
/// Executes the task.
///
///
///
/// Sets the property identified by to
/// when the resource exists and to
/// when the resource doesn't exist.
///
///
/// The availability of the resource could not be evaluated.
protected override void ExecuteTask() {
Project.Properties[PropertyName] = Evaluate().ToString(CultureInfo.InvariantCulture);
}
#endregion Override implementation of Task
#region Protected Instance Methods
///
/// Evaluates the availability of a resource.
///
///
/// if the resource is available; otherwise,
/// .
///
/// The availability of the resource could not be evaluated.
protected virtual bool Evaluate() {
bool resourceAvailable = false;
switch(Type) {
case ResourceType.File:
resourceAvailable = CheckFile();
break;
case ResourceType.Directory:
resourceAvailable = CheckDirectory();
break;
case ResourceType.Framework:
resourceAvailable = CheckFramework();
break;
case ResourceType.FrameworkSDK:
resourceAvailable = CheckFrameworkSDK();
break;
default:
throw new BuildException(string.Format(CultureInfo.InvariantCulture, "No resource check is implemented for {0}", Type));
}
if (!resourceAvailable) {
Log(Level.Verbose, "Unable to find {0} {1}.", Type, Resource);
}
return resourceAvailable;
}
#endregion Protected Instance Methods
#region Private Instance Methods
///
/// Checks if the file specified in the property is
/// available on the filesystem.
///
///
/// when the file exists; otherwise, .
///
private bool CheckFile() {
try {
FileInfo fileInfo = new FileInfo(Project.GetFullPath(Resource));
return fileInfo.Exists;
} catch (ArgumentException ex) {
throw new BuildException(string.Format(
CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1104"),
Resource), Location, ex);
}
}
///
/// Checks if the directory specified in the
/// property is available on the filesystem.
///
///
/// when the directory exists; otherwise, .
///
private bool CheckDirectory() {
try {
DirectoryInfo dirInfo = new DirectoryInfo(Project.GetFullPath(Resource));
return dirInfo.Exists;
} catch (ArgumentException ex) {
throw new BuildException(string.Format(
CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1103"),
Resource), Location, ex);
}
}
///
/// Checks if the framework specified in the
/// property is available on the current system.
///
///
/// when the framework is available; otherwise,
/// .
///
private bool CheckFramework() {
return Project.Frameworks.Contains(Resource);
}
///
/// Checks if the SDK for the framework specified in the
/// property is available on the current system.
///
///
/// when the SDK for the specified framework is
/// available; otherwise, .
///
private bool CheckFrameworkSDK() {
FrameworkInfo framework = Project.Frameworks[Resource];
if (framework != null) {
return framework.SdkDirectory != null;
} else {
return false;
}
}
#endregion Private Instance Methods
#region Private Instance Fields
private ResourceType _resourceType;
private string _resource;
private string _propertyName;
#endregion Private Instance Fields
}
}