Skip to content

Commit

Permalink
Implement method to replace text in a file using regex patterns per f…
Browse files Browse the repository at this point in the history
…sprojects#622

The current implementation requires that the caller specify the encoding
for the files that they are processing. Future enhancements may include
auto-detection of the file encoding to make usage a little cleaner.
  • Loading branch information
ryanewtaylor committed Feb 9, 2015
1 parent 0d82fea commit 4b54c23
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/app/FakeLib/FileHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ let WriteConfigFile configFileName parameters =
/// - `files` - The files to process.
let ReplaceInFiles replacements files = processTemplates replacements files

/// Replace all occurences of the regex pattern with the given replacement in the specified file
/// ## Parameters
///
/// - `file` - The path of the file to process
/// - `pattern` - The string to search for a match
/// - `replacement` - The replacement string
/// - `encoding` - The encoding to use when reading and writing the file
let RegexReplaceInFileWithEncoding pattern (replacement:string) encoding file =
let oldContent = File.ReadAllText(file, encoding)
let newContent = System.Text.RegularExpressions.Regex.Replace(oldContent, pattern, replacement)
File.WriteAllText(file, newContent, encoding)

/// Replace all occurences of the regex pattern with the given replacement in the specified files
/// ## Parameters
///
/// - `file` - The path of the file to process
/// - `pattern` - The string to search for a match
/// - `replacement` - The replacement string
/// - `encoding` - The encoding to use when reading and writing the files
let RegexReplaceInFilesWithEncoding pattern (replacement:string) encoding files =
files |> Seq.iter (RegexReplaceInFileWithEncoding pattern replacement encoding)

/// Get the version a file.
/// ## Parameters
///
Expand Down

0 comments on commit 4b54c23

Please sign in to comment.