/// /// 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.ComponentModel; using System.Globalization; using JetBrains.Build.Common.Infra; using Microsoft.Build.Framework; namespace JetBrains.Build.Common.Tasks { /// /// Invokes the function for the given arguments. /// public class StringFormat : TaskBase { #region Attributes /// /// Gets or sets the list of arguments to use in formatting. /// If you need to specify number-specific etc formatting, use . /// public string[] Arguments { get; set; } /// /// If specified, a collection of CLR type names of the . /// The length of the argument types collection must be equal to the number of . /// The CLR type names are case-insensitive full names of types from mscorlib, or assembly-qualified names in case of other assemblies. /// public string[] ArgumentTypes { get; set; } /// /// Specifies the culture info for the formatting, if applicable. /// By default, that's . /// public string CultureInfo { get; set; } /// /// Gets or sets the format string. /// public string Format { get; set; } /// /// Gets the resulting formatted string. /// [Output] public string Result { get; set; } #endregion #region Implementation private object[] GetArguments() { string[] argumentstrings = Arguments ?? new string[] {}; // If there are types specified, make the conversion object[] @params; if((ArgumentTypes != null) && (ArgumentTypes.Length != 0)) { if(ArgumentTypes.Length != Arguments.Length) throw new InvalidOperationException(string.Format("There are {0} arguments and {1} argument types. There should either be no argument types, or exactly as many as the arguments.", argumentstrings.Length, ArgumentTypes.Length)); @params = new object[argumentstrings.Length]; for(int nArg = 0; nArg < argumentstrings.Length; nArg++) @params[nArg] = TypeDescriptor.GetConverter(Type.GetType(ArgumentTypes[nArg], true, true)).ConvertFromInvariantString(argumentstrings[nArg]); } else @params = argumentstrings; return @params; } private CultureInfo GetCultureInfo() { CultureInfo cultureinfo; if(string.IsNullOrEmpty(CultureInfo)) cultureinfo = System.Globalization.CultureInfo.InvariantCulture; else { cultureinfo = (CultureInfo)TypeDescriptor.GetConverter(typeof(CultureInfo)).ConvertFromInvariantString(CultureInfo); if(cultureinfo == null) throw new InvalidOperationException(string.Format("Could not parse the culture info.")); } return cultureinfo; } private string GetFormatString() { return Format ?? ""; } #endregion #region Overrides /// /// The method to be overriden in inheriting tasks. /// Throw an exception in case of an errror. /// protected override void ExecuteTask() { Result = string.Format(GetCultureInfo(), GetFormatString(), GetArguments()); } #endregion } }