Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search and replace script to be used instead of sed to apply patches #364

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")