///
/// Copyright © 2003-2008 JetBrains s.r.o.
/// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder).
///
using System;
using System.Collections;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace JetBrains.Build.Common.Infra
{
///
/// The base class for deriving tasks from it, defines the attribute bag.
///
public abstract class TaskBase : Task
{
#region Data
///
/// .
///
private readonly Hashtable myBag = new Hashtable();
#endregion
#region Operations
///
/// Checks whether a bag entry is present.
///
public bool BagContains(AttributeName name)
{
return Bag[name] != null;
}
///
/// Gets a typed value from the bag. Throws if a value is missing.
///
public T BagGet(AttributeName name)
{
return TaskHelper.GetValue(Bag, name);
}
///
/// Gets a typed value from the bag. Returns the if an entry is missing from the bag.
///
public T BagGet(AttributeName name, T defaultvalue)
{
object entry = Bag[name];
return (T)(entry ?? defaultvalue);
}
///
/// Gets a typed value from the bag. Null (a missing value) is OK.
///
public T BagGetTry(AttributeName name)
{
return (T)Bag[name];
}
///
/// Puts a typed value to the bag. Null (a missing value) is OK.
///
public void BagSet(AttributeName name, T value)
{
Bag[name] = value;
}
#endregion
#region Implementation
///
/// Gets the task attributes bag.
///
protected Hashtable Bag
{
get
{
return myBag;
}
}
///
/// The method to be overriden in inheriting tasks.
/// Throw an exception in case of an errror.
///
protected abstract void ExecuteTask();
///
/// Gets a string value from the bag, throws on an error.
///
protected string GetStringValue(AttributeName attribute)
{
return TaskHelper.GetStringValue(Bag, attribute);
}
///
/// Gets a value from the bag, throws on an error.
///
protected T GetValue(AttributeName attribute)
{
return TaskHelper.GetValue(Bag, attribute);
}
#endregion
#region Overrides
///
///When overridden in a derived class, executes the task.
///
///
///
///true if the task successfully executed; otherwise, false.
///
///
public override bool Execute()
{
try
{
ExecuteTask();
return true;
}
catch(Exception ex)
{
Log.LogError(ex.Message);
Log.LogMessage(MessageImportance.Normal, ex.ToString());
return false;
}
}
#endregion
}
}