Skip to content

Commit

Permalink
Merge pull request #18 from Cheesebaron/master
Browse files Browse the repository at this point in the history
Add helper alias to find text in IEnumerable of FilePaths
  • Loading branch information
Redth authored Aug 21, 2018
2 parents 2e12098 + a35d8c5 commit 07b60f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
17 changes: 15 additions & 2 deletions Cake.FileHelpers.Tests/FileHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void TestWriteAndReadLines ()
}

[Fact]
public void FindTextInFiles ()
public void FindTextInFilesGlob ()
{
SetupFiles ();

Expand All @@ -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 ()
{
Expand All @@ -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);
}


Expand Down
30 changes: 21 additions & 9 deletions Cake.FileHelpers/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,34 @@ public static FilePath[] FindRegexInFiles (this ICakeContext context, string glo
/// </summary>
/// <returns>The files which match the regular expression and globber pattern.</returns>
/// <param name="context">The context.</param>
/// <param name="globberPattern">The globber pattern to match files to replace text in.</param>
/// <param name="findPattern">The regular expression to find.</param>
/// <param name="globberPattern">The globber pattern to match files to find text in.</param>
/// <param name="substring">The substring to find.</param>
[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<FilePath> ();
/// <summary>
/// Finds files with the given text in files matching the given collection of files.
/// </summary>
/// <returns>The files which match the regular expession in the files.</returns>
/// <param name="context">The context.</param>
/// <param name="files">The files to find text in.</param>
/// <param name="substring">The substring to find.</param>
[CakeMethodAlias]
public static FilePath[] FindTextInFiles(this ICakeContext context, IEnumerable<FilePath> files, string substring)
{
var results = new ConcurrentBag<FilePath>();

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();
}

/// <summary>
Expand Down

0 comments on commit 07b60f9

Please sign in to comment.