/// /// 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 System.IO; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; namespace JetBrains.Build.Common.Infra { /// /// A base task for tool-based tasks, defining the attributes bag. /// public abstract class ToolTaskBase : ToolTask { #region Data protected Hashtable myBag = new Hashtable(); #endregion #region Attributes public Hashtable Bag { get { return myBag; } } /// /// Gets or sets the directory in which the tool task executable resides. /// public ITaskItem ToolDir { get { return BagGetTry(AttributeName.ToolDir); } set { BagSet(AttributeName.ToolDir, value); } } /// /// Gets the name of the environment variable that provides the path to the tool in case the is not defined. /// public virtual string ToolDirEnvName { get { return null; } } #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 Overrides /// ///Returns the fully qualified path to the executable file. /// /// /// ///The fully qualified path to the executable file. /// /// protected override string GenerateFullPathToTool() { if(string.IsNullOrEmpty(ToolName)) throw new InvalidOperationException(string.Format("The tool name must not be empty in the build task.")); if(string.IsNullOrEmpty(Path.GetExtension(ToolName))) throw new InvalidOperationException(string.Format("The tool name “{0}” must include the extension.", ToolName)); // Fetch the tool dir string sToolDir = null; if((ToolDir != null) && (!string.IsNullOrEmpty(ToolDir.ItemSpec))) // Manual tool dir { Log.LogMessage(MessageImportance.Low, "{0} location is given by the ToolDir property value.", ToolName); sToolDir = ToolDir.ItemSpec; } else if(!string.IsNullOrEmpty(ToolDirEnvName)) // Via env { sToolDir = Environment.GetEnvironmentVariable(ToolDirEnvName); Log.LogMessage(MessageImportance.Low, "{0} location is given by the ToolDirEnvName environment variable (“{1}”).", ToolName, Environment.GetEnvironmentVariable(ToolDirEnvName)); } if(!string.IsNullOrEmpty(sToolDir)) // Use if could be obtained return Path.Combine(sToolDir, ToolName); // Lookup an SDK file foreach(var action in new Func[] {delegate { return ToolLocationHelper.GetPathToDotNetFrameworkFile(ToolName, TargetDotNetFrameworkVersion.VersionLatest); }, delegate { return ToolLocationHelper.GetPathToDotNetFrameworkSdkFile(ToolName, TargetDotNetFrameworkVersion.VersionLatest); }, delegate { return ToolLocationHelper.GetPathToSystemFile(ToolName); }, delegate { return ToolLocationHelper.GetPathToDotNetFrameworkFile(ToolName, TargetDotNetFrameworkVersion.Version20); }, delegate { return ToolLocationHelper.GetPathToDotNetFrameworkSdkFile(ToolName, TargetDotNetFrameworkVersion.Version20); }, delegate { return ToolLocationHelper.GetPathToDotNetFrameworkFile(ToolName, TargetDotNetFrameworkVersion.Version11); }, delegate { return ToolLocationHelper.GetPathToDotNetFrameworkSdkFile(ToolName, TargetDotNetFrameworkVersion.Version11); },}) { try { string sFullPath = action(); if((!string.IsNullOrEmpty(sFullPath)) && (File.Exists(sFullPath))) { Log.LogMessage(MessageImportance.Low, "{0} location is given by the ToolLocationHelper (“{1}”).", ToolName, sFullPath); return sFullPath; } } catch(Exception ex) { Log.LogMessage("Warning when probing for {0}: {1}", ToolName, ex.Message); } } Log.LogMessage(MessageImportance.Low, "{0} location could not be determined, leaving to the shell.", ToolName); return ToolName; // Fallback to %PATH% on execution } #endregion } }