// NAnt - A .NET build tool
// Copyright (C) 2001-2002 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)
// Ian MacLean (imaclean@gmail.com)
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using NAnt.Core.Attributes;
using NAnt.Core.Types;
using NAnt.Core.Util;
namespace NAnt.Core.Tasks {
///
/// Loads tasks form a given assembly or all assemblies in a given directory
/// or .
///
///
///
/// Load tasks from a single assembly.
///
///
///
/// ]]>
///
///
///
///
/// Scan a single directory for task assemblies.
///
///
///
/// ]]>
///
///
///
///
/// Use a containing both a directory and an
/// assembly.
///
///
///
///
///
///
///
///
/// ]]>
///
///
[TaskName("loadtasks")]
public class LoadTasksTask : Task {
#region Private Instance Fields
private FileInfo _assembly;
private DirectoryInfo _path;
private FileSet _fileset = new FileSet();
#endregion Private Instance Fields
#region Public Instance Properties
///
/// An assembly to load tasks from.
///
[TaskAttribute("assembly")]
public FileInfo AssemblyPath {
get { return _assembly; }
set { _assembly = value; }
}
///
/// A directory to scan for task assemblies.
///
[TaskAttribute("path")]
public DirectoryInfo Path {
get { return _path; }
set { _path = value; }
}
///
/// Used to select which directories or individual assemblies to scan.
///
[BuildElement("fileset")]
public FileSet TaskFileSet {
get { return _fileset; }
set { _fileset = value; }
}
#endregion Public Instance Properties
#region Override implemenation of Task
///
/// Executes the Load Tasks task.
///
/// Specified assembly or path does not exist.
[ReflectionPermission(SecurityAction.Demand, Flags=ReflectionPermissionFlag.NoFlags)]
protected override void ExecuteTask() {
// ensure base directory is set, even if fileset was not initialized
// from XML
if (TaskFileSet.BaseDirectory == null) {
TaskFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
}
if (AssemblyPath != null) { // single file case
if (!AssemblyPath.Exists) {
throw new BuildException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1132"),
AssemblyPath.FullName), Location);
}
TaskFileSet.FileNames.Add(AssemblyPath.FullName);
} else if (Path != null) {
if (!Path.Exists) {
throw new BuildException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1131"),
Path), Location);
}
TaskFileSet.DirectoryNames.Add(Path.FullName);
}
// process the fileset
foreach (string assemblyPath in TaskFileSet.FileNames) {
try {
TypeFactory.ScanAssembly(assemblyPath, this);
} catch (Exception ex) {
string message = string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1130"), assemblyPath);
if (FailOnError) {
throw new BuildException(message, Location, ex);
} else {
Log(Level.Error, message + " " + ex.Message);
}
}
}
// process the path
foreach (string scanPath in TaskFileSet.DirectoryNames) {
try {
TypeFactory.ScanDir(scanPath, this, FailOnError);
} catch (BuildException) {
throw;
} catch (Exception ex) {
throw new BuildException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1130"), scanPath),
Location, ex);
}
}
}
///
/// Validates the attributes.
///
/// Both and are set.
protected override void InitializeTask(XmlNode taskNode) {
//verify that our params are correct
if (AssemblyPath != null && Path != null) {
throw new BuildException("Both asssembly and path attributes are set."
+ " Use one or the other.", Location);
}
}
#endregion Override implemenation of Task
}
}