diff --git a/Cake.FileHelpers.Tests/FileHelperTests.cs b/Cake.FileHelpers.Tests/FileHelperTests.cs index dc31580..74a2eea 100644 --- a/Cake.FileHelpers.Tests/FileHelperTests.cs +++ b/Cake.FileHelpers.Tests/FileHelperTests.cs @@ -59,7 +59,7 @@ public void TestWriteAndReadLines () } [Fact] - public void FindTextInFiles () + public void FindTextInFilesGlob () { SetupFiles (); @@ -69,6 +69,19 @@ public void FindTextInFiles () Assert.Single(files); } + [Fact] + public void FindTextInFiles () + { + SetupFiles(); + + var files = context.CakeContext.Globber.GetFiles ("./testdata/*.txt"); + + var monkeyFiles = context.CakeContext.FindTextInFiles (files, "Monkey"); + + Assert.NotNull (monkeyFiles); + Assert.Single (monkeyFiles); + } + [Fact] public void FindRegexInFiles () { @@ -77,7 +90,7 @@ public void FindRegexInFiles () var files = context.CakeContext.FindRegexInFiles ("./testdata/*.txt", @"\s{1}Monkey\s{1,}"); Assert.NotNull (files); - Assert.Single(files); + Assert.Single (files); } diff --git a/Cake.FileHelpers/FileHelpers.cs b/Cake.FileHelpers/FileHelpers.cs index bbc1be0..664bd0c 100644 --- a/Cake.FileHelpers/FileHelpers.cs +++ b/Cake.FileHelpers/FileHelpers.cs @@ -229,22 +229,34 @@ public static FilePath[] FindRegexInFiles (this ICakeContext context, string glo /// /// The files which match the regular expression and globber pattern. /// The context. - /// The globber pattern to match files to replace text in. - /// The regular expression to find. + /// The globber pattern to match files to find text in. + /// The substring to find. [CakeMethodAlias] - public static FilePath[] FindTextInFiles (this ICakeContext context, string globberPattern, string findPattern) + public static FilePath[] FindTextInFiles (this ICakeContext context, string globberPattern, string substring) { var files = context.Globber.GetFiles (globberPattern); + return FindTextInFiles(context, files, substring); + } - var results = new ConcurrentBag (); + /// + /// Finds files with the given text in files matching the given collection of files. + /// + /// The files which match the regular expession in the files. + /// The context. + /// The files to find text in. + /// The substring to find. + [CakeMethodAlias] + public static FilePath[] FindTextInFiles(this ICakeContext context, IEnumerable files, string substring) + { + var results = new ConcurrentBag(); - Parallel.ForEach (files, f => { - var contents = FileReadText (context, f); - if (contents.Contains (findPattern)) - results.Add (f); + Parallel.ForEach(files, f => { + var contents = FileReadText(context, f); + if (contents.Contains(substring)) + results.Add(f); }); - return results.ToArray (); + return results.ToArray(); } ///