/// /// 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.IO; using System.Text; using System.Text.RegularExpressions; using JetBrains.Build.Common.Infra; using Microsoft.Build.Framework; namespace JetBrains.Build.Common.Tasks { /// /// Replaces text in the text files against a regexp. /// public class ReplaceText : TaskBase { #region Attributes /// /// Gets or sets whether the search should be case-sensitive. /// [Required] public bool CaseSensitive { get { return (bool)Bag[AttributeName.CaseSensitive]; } set { Bag[AttributeName.CaseSensitive] = value; } } /// /// Gets or sets the files to replace text within. /// [Required] public ITaskItem[] InputFiles { get { return (ITaskItem[])Bag[AttributeName.InputFiles]; } set { Bag[AttributeName.InputFiles] = value; } } /// /// Gets or sets the matching pattern. /// [Required] public string What { get { return (string)Bag[AttributeName.What]; } set { Bag[AttributeName.What] = value; } } /// /// Gets or sets the replacement string. /// [Required] public string With { get { return (string)Bag[AttributeName.With]; } set { Bag[AttributeName.With] = value; } } #endregion #region Implementation /// /// Processes a single file. /// private static void ReplaceTextInFile(string pathname, Regex what, string with) { var fi = new FileInfo(pathname); if(!fi.Exists) throw new InvalidOperationException(string.Format("The file “{0}” does not exist.", fi.FullName)); // Read string sInput; using(Stream stream = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) using(var sr = new StreamReader(stream)) sInput = sr.ReadToEnd(); // Replace string sOutput = what.Replace(sInput, with); // Write using(Stream stream = fi.Open(FileMode.Create, FileAccess.Write, FileShare.Read)) using(var sw = new StreamWriter(stream, Encoding.UTF8)) sw.Write(sOutput); } #endregion #region Overrides /// /// The method to be overriden in inheriting tasks. /// Throw an exception in case of an errror. /// protected override void ExecuteTask() { var regex = new Regex(GetStringValue(AttributeName.What), RegexOptions.Multiline | (GetValue(AttributeName.CaseSensitive) ? 0 : RegexOptions.IgnoreCase)); string sWith = GetStringValue(AttributeName.With); foreach(ITaskItem item in GetValue(AttributeName.InputFiles)) ReplaceTextInFile(item.ItemSpec, regex, sWith); } #endregion } }