-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search and replace script to be used instead of sed to apply patches
- Loading branch information
1 parent
6fef38a
commit f39faf0
Showing
4 changed files
with
25 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.util.regex.PatternSyntaxException | ||
import java.nio.file.Files | ||
|
||
import scala.util.chaining.* | ||
|
||
given scala.util.CommandLineParser.FromString[Path] = Paths.get(_) | ||
|
||
@main def searchAndReplace(file: Path, textOrPattern: String, replacement: String): Unit = | ||
val input = io.Source.fromFile(file.toFile()).mkString | ||
input | ||
.replace(textOrPattern, replacement) | ||
.pipe: input => | ||
try textOrPattern.r.replaceAllIn(input, replacement) | ||
catch case _: PatternSyntaxException => input | ||
.pipe: output => | ||
if input != output then | ||
println(s"Successfully applied pattern '$textOrPattern' in $file") | ||
Files.write(file, output.getBytes()) | ||
else | ||
System.err.println(s"Failed to apply pattern '$textOrPattern' in $file") |