// 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
//
// Ian Maclean (imaclean@gmail.com)
// Jaroslaw Kowalski (jkowalski@users.sourceforge.net)
// Gert Driesen (gert.driesen@ardatis.com)
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;
using NAnt.Core.Util;
//
// This file defines functions for the NAnt category.
//
// Please note that property::get-value() is defined in ExpressionEvaluator
// class because it needs the intimate knowledge of the expressione evaluation stack.
// Other functions should be defined here.
//
namespace NAnt.Core.Functions {
[FunctionSet("nant", "NAnt")]
public class NAntFunctions : FunctionSetBase {
#region Public Instance Constructors
public NAntFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Gets the base directory of the appdomain in which NAnt is running.
///
///
/// The base directory of the appdomain in which NAnt is running.
///
[Function("get-base-directory")]
public string GetBaseDirectory() {
return AppDomain.CurrentDomain.BaseDirectory;
}
///
/// Gets the NAnt assembly.
///
///
/// The NAnt assembly.
///
[Function("get-assembly")]
public Assembly GetAssembly() {
Assembly assembly = Assembly.GetEntryAssembly();
// check if NAnt was launched as a console application
if (assembly.GetName().Name != "NAnt") {
// NAnt is being used as a class library, so return the
// NAnt.Core assembly
assembly = Assembly.GetExecutingAssembly();
}
return assembly;
}
#endregion Public Instance Methods
}
[FunctionSet("project", "NAnt")]
public class ProjectFunctions : FunctionSetBase {
#region Public Instance Constructors
public ProjectFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Gets the name of the current project.
///
///
/// The name of the current project, or an empty
/// if no name is specified in the build file.
///
[Function("get-name")]
public string GetName() {
return StringUtils.ConvertNullToEmpty(Project.ProjectName);
}
///
/// Gets the form of the build file.
///
///
/// The form of the build file, or
/// an empty if the project is not file backed.
///
[Function("get-buildfile-uri")]
public string GetBuildFileUri() {
if (Project.BuildFileUri != null) {
return Project.BuildFileUri.ToString();
}
return string.Empty;
}
///
/// Gets the local path to the build file.
///
///
/// The local path of the build file, or an empty
/// if the project is not file backed.
///
[Function("get-buildfile-path")]
public string GetBuildFilePath() {
return StringUtils.ConvertNullToEmpty(Project.BuildFileLocalName);
}
///
/// Gets the name of the target that will be executed when no other
/// build targets are specified.
///
///
/// The name of the target that will be executed when no other build
/// targets are specified, or an empty if no
/// default target is defined for the project.
///
[Function("get-default-target")]
public string GetDefaultTarget() {
return StringUtils.ConvertNullToEmpty(Project.DefaultTargetName);
}
///
/// Gets the base directory of the current project.
///
///
/// The base directory of the current project.
///
[Function("get-base-directory")]
public string GetBaseDirectory() {
return Project.BaseDirectory;
}
#endregion Public Instance Methods
}
[FunctionSet("target", "NAnt")]
public class TargetFunctions : FunctionSetBase {
#region Public Instance Constructors
public TargetFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Checks whether the specified target exists.
///
/// The target to test.
///
/// if the specified target exists; otherwise,
/// .
///
///
///
/// Execute target "clean", if it exists.
///
///
///
///
///
/// ]]>
///
///
[Function("exists")]
public bool Exists(string name) {
return (Project.Targets.Find(name) != null);
}
///
/// Gets the name of the target being executed.
///
///
/// A that contains the name of the target
/// being executed.
///
/// No target is being executed.
[Function("get-current-target")]
public string GetCurrentTarget() {
Target target = Project.CurrentTarget;
if (target == null) {
throw new InvalidOperationException("No target is being executed.");
}
return target.Name;
}
///
/// Checks whether the specified target has already been executed.
///
/// The target to test.
///
/// if the specified target has already been
/// executed; otherwise, .
///
/// Target does not exist.
[Function("has-executed")]
public bool HasExecuted(string name) {
Target target = Project.Targets.Find(name);
if (target == null) {
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1097"), name));
}
return target.Executed;
}
#endregion Public Instance Methods
}
[FunctionSet("task", "NAnt")]
public class TaskFunctions : FunctionSetBase {
#region Public Instance Constructors
public TaskFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Checks whether the specified task exists.
///
/// The task to test.
///
/// if the specified task exists; otherwise,
/// .
///
[Function("exists")]
public bool Exists(string name) {
return TypeFactory.TaskBuilders.Contains(name);
}
///
/// Returns the from which the specified task
/// was loaded.
///
/// The name of the task to get the of.
///
/// The from which the specified task was loaded.
///
/// Task is not available.
[Function("get-assembly")]
public Assembly GetAssembly(string name) {
TaskBuilder task = TypeFactory.TaskBuilders[name];
if (task == null) {
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1099"), name));
}
return task.Assembly;
}
#endregion Public Instance Methods
}
[FunctionSet("property", "NAnt")]
public class PropertyFunctions : FunctionSetBase {
#region Public Instance Constructors
public PropertyFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Checks whether the specified property exists.
///
/// The property to test.
///
/// if the specified property exists; otherwise,
/// .
///
///
///
/// Execute a set of tasks if the "build.debug" property
/// exists.
///
///
///
///
///
///
///
/// ]]>
///
///
[Function("exists")]
public bool Exists(string name) {
return Project.Properties.Contains(name);
}
///
/// Checks whether the specified property is read-only.
///
/// The property to test.
///
/// if the specified property is read-only;
/// otherwise, .
///
///
/// Check whether the "debug" property is read-only.
/// property::is-readonly('debug')
///
/// Property has not been set.
[Function("is-readonly")]
public bool IsReadOnly(string name) {
if (!Project.Properties.Contains(name)) {
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1053"), name));
}
return Project.Properties.IsReadOnlyProperty(name);
}
///
/// Checks whether the specified property is a dynamic property.
///
/// The property to test.
///
/// if the specified property is a dynamic
/// property; otherwise, .
///
/// Property has not been set.
///
///
/// Check whether the "debug" property is a dynamic property.
///
/// property::is-dynamic('debug')
///
[Function("is-dynamic")]
public bool IsDynamic(string name) {
if (!Project.Properties.Contains(name)) {
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1053"), name));
}
return Project.Properties.IsDynamicProperty(name);
}
#endregion Public Instance Methods
}
[FunctionSet("framework", "NAnt")]
public class FrameworkFunctions : FunctionSetBase {
#region Public Instance Constructors
public FrameworkFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Checks whether the specified framework exists.
///
/// The framework to test.
///
/// if the specified framework exists; otherwise,
/// .
///
[Function("exists")]
public bool Exists(string name) {
return Project.Frameworks.ContainsKey(name);
}
///
/// Checks whether the SDK for the specified framework is installed.
///
/// The framework to test.
///
/// if the SDK for specified framework is installed;
/// otherwise, .
///
///
///
[Function("sdk-exists")]
public bool SdkExists(string name) {
if (Project.Frameworks.ContainsKey(name)) {
return (Project.Frameworks[name].SdkDirectory != null);
} else {
return false;
}
}
///
/// Gets the identifier of the current target framework.
///
///
/// The identifier of the current target framework.
///
[Function("get-target-framework")]
public string GetTargetFramework() {
return Project.TargetFramework.Name;
}
///
/// Gets the identifier of the runtime framework.
///
///
/// The identifier of the runtime framework.
///
[Function("get-runtime-framework")]
public string GetRuntimeFramework() {
return Project.RuntimeFramework.Name;
}
///
/// Gets the family of the specified framework.
///
/// The framework of which the family should be returned.
///
/// The family of the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-family")]
public string GetFamily(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// return the family of the specified framework
return Project.Frameworks[framework].Family;
}
///
/// Gets the version of the specified framework.
///
/// The framework of which the version should be returned.
///
/// The version of the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-version")]
public Version GetVersion(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// return the family of the specified framework
return Project.Frameworks[framework].Version;
}
///
/// Gets the description of the specified framework.
///
/// The framework of which the description should be returned.
///
/// The description of the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-description")]
public string GetDescription(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// return the description of the specified framework
return Project.Frameworks[framework].Description;
}
///
/// Gets the Common Language Runtime version of the specified framework.
///
/// The framework of which the Common Language Runtime version should be returned.
///
/// The Common Language Runtime version of the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-clr-version")]
public Version GetClrVersion(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// return the family of the specified framework
return Project.Frameworks[framework].ClrVersion;
}
///
/// Gets the framework directory of the specified framework.
///
/// The framework of which the framework directory should be returned.
///
/// The framework directory of the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-framework-directory")]
public string GetFrameworkDirectory(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// return full path to the framework directory of the specified framework
return Project.Frameworks[framework].FrameworkDirectory.FullName;
}
///
/// Gets the assembly directory of the specified framework.
///
/// The framework of which the assembly directory should be returned.
///
/// The assembly directory of the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-assembly-directory")]
public string GetAssemblyDirectory(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// return full path to the assembly directory of the specified framework
return Project.Frameworks[framework].FrameworkAssemblyDirectory.FullName;
}
///
/// Gets the SDK directory of the specified framework.
///
/// The framework of which the SDK directory should be returned.
///
/// The SDK directory of the specified framework, or an empty
/// if the SDK of the specified framework is not
/// installed.
///
/// is not a valid framework identifier.
///
///
[Function("get-sdk-directory")]
public string GetSdkDirectory(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// get the SDK directory of the specified framework
DirectoryInfo sdkDirectory = Project.Frameworks[framework].SdkDirectory;
// return directory or empty string if SDK is not installed
return (sdkDirectory != null) ? sdkDirectory.FullName : string.Empty;
}
///
/// Gets the runtime engine of the specified framework.
///
/// The framework of which the runtime engine should be returned.
///
/// The full path to the runtime engine of the specified framework, or
/// an empty if no runtime engine is defined
/// for the specified framework.
///
/// is not a valid framework identifier.
///
///
[Function("get-runtime-engine")]
public string GetRuntimeEngine(string framework) {
// ensure the framework is valid
CheckFramework(framework);
// getthe runtime engine of the specified framework
FileInfo runtimeEngine = Project.Frameworks[framework].RuntimeEngine;
// return runtime engine or empty string if not defined
return (runtimeEngine != null) ? runtimeEngine.FullName : string.Empty;
}
#endregion Public Instance Methods
#region Private Instance Methods
///
/// Checks whether the specified framework is valid.
///
/// The framework to check.
/// is not a valid framework identifier.
private void CheckFramework(string framework) {
if (!Project.Frameworks.ContainsKey(framework)) {
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
ResourceUtils.GetString("NA1096"), framework));
}
}
#endregion Private Instance Methods
}
[FunctionSet("platform", "NAnt")]
public class PlatformFunctions : FunctionSetBase {
#region Public Instance Constructors
public PlatformFunctions(Project project, PropertyDictionary properties) : base(project, properties) {
}
#endregion Public Instance Constructors
#region Public Instance Methods
///
/// Gets the name of the platform on which NAnt is running.
///
///
/// The name of the platform on which NAnt is running.
///
[Function("get-name")]
public string GetName() {
return Project.PlatformName;
}
#endregion Public Instance Methods
#region Public Static Methods
///
/// Checks whether NAnt is running on the win32 platform.
///
///
/// if NAnt is running on the win32 platform;
/// otherwise, .
///
[Function("is-win32")]
public static bool IsWin32() {
return PlatformHelper.IsWin32;
}
///
/// Checks whether NAnt is running on unix.
///
///
/// if NAnt is running on unix;
/// otherwise, .
///
[Function("is-unix")]
public static bool IsUnix() {
return PlatformHelper.IsUnix;
}
#endregion Public Static Methods
}
}