Skip to content

Commit

Permalink
Add search and replace script to be used instead of sed to apply patches
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMazur committed Jul 22, 2024
1 parent 6fef38a commit f39faf0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion project-builder/mill/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -e

if [ $# -ne 7]; then
if [ $# -ne 7 ]; then
echo "Wrong number of script arguments, expected $0 <repo_dir> <scala-version> <targets> <maven_repo> <sbt_version?> <project_config?> <extra-scalacOption?> <disabled-scalacOptions?>, got $#: $@"
exit 1
fi
Expand Down
8 changes: 1 addition & 7 deletions project-builder/mill/prepare-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ for elem in $(echo "${projectConfig}" | jq -r '.sourcePatches // [] | .[] | @bas
echo "Path: $path"
echo "Pattern: $pattern"
echo "Replacement: $replaceWith"

set -x
# Cannot determinate did sed script was applied, so perform two ops each time
# Don't use in-place option for easier cross-platform compat (macos vs unix)
(sed "s/$pattern/$replaceWith/" "$path" > $path.tmp && mv $path.tmp $path ) || true
(sed -E "s/$pattern/$replaceWith/" "$path" > $path.tmp && mv $path.tmp $path ) || true
set +x
scala-cli run ${scriptDir}/../../scripts/searchAndReplace.scala -- "${repoDir}/${path}" "${pattern}" "${replaceWith}"
done

prepareScript="${OPENCB_SCRIPT_DIR:?OPENCB_SCRIPT_DIR not defined}/prepare-scripts/${projectName}.sh"
Expand Down
10 changes: 1 addition & 9 deletions project-builder/sbt/prepare-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,7 @@ for elem in $(echo "${projectConfig}" | jq -r '.sourcePatches // [] | .[] | @bas
echo "Path: $path"
echo "Pattern: $pattern"
echo "Replacement: $replaceWith"

set -x
# Cannot determinate did sed script was applied, so perform two ops each time
# Don't use in-place option for easier cross-platform compat (macos vs unix)
repoPath=$repoDir/$path
(sed "s/$pattern/$replaceWith/" "$repoPath" > $repoPath.tmp && mv $repoPath.tmp $repoPath ) || true
(sed -E "s/$pattern/$replaceWith/" "$repoPath" > $repoPath.tmp && mv $repoPath.tmp $repoPath ) || true

set +x
scala-cli run ${scriptDir}/../../scripts/searchAndReplace.scala -- "${repoDir}/${path}" "${pattern}" "${replaceWith}"
done

prepareScript="${OPENCB_SCRIPT_DIR:?OPENCB_SCRIPT_DIR not defined}/prepare-scripts/${projectName}.sh"
Expand Down
22 changes: 22 additions & 0 deletions scripts/searchAndReplace.scala
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")

0 comments on commit f39faf0

Please sign in to comment.