Skip to content

Commit

Permalink
Replace GNU-specific commands with more portable alternatives (#682)
Browse files Browse the repository at this point in the history
* Remove -i from sed invocation for compatibility

* Improve cross platform compatibility for make_new_lint.sh

- Replace grep -z (GNU-only) with an equivalent awk invocation.
- Replace sed -i (option behaves differently on Mac vs GNU) with tmpfile
- Replace sed -z (GNU-only). New lints are prepended instead of appended

* Update Contributing guide requirements

The contributor-facing scripts (make_new_lint, make_test_crate,
regenerate_test_rustdocs) no longer have any dependencies on GNU-only
command line tools, so those have been removed from the guide.

* Update scripts/make_new_lint.sh

---------

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>
  • Loading branch information
jw013 and obi1kenobi authored Mar 5, 2024
1 parent 781abd0 commit e0d61c7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
14 changes: 4 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,9 @@ expected output files to include the new items, and you can get back on track.
## Development Environment

While cargo-semver-checks is cross platform, the development task automation scripts in the scripts
directory are not. In particular, they require
directory are not. In particular, a relatively modern `bash` (at least version 4.0) is required.

- GNU command line tools (coreutils, grep, sed, etc.)
- a relatively modern `bash` (at least version 4.0)
- `curl`
- [`jq`](https://jqlang.github.io/jq/)

Linux users likely have all of these already installed or available via their package manager.
Linux users likely have bash 5+ already installed or available via their package manager.
Windows users can get a bash + GNU command line environment via WSL or git bash.
Mac users can install GNU tools via homebrew. The scripts will not work
correctly using the default bash and BSD command line utilities that come with
Mac OS.
Mac users can install an updated bash via homebrew. The scripts will not work
correctly using the default version of bash that comes with Mac OS.
26 changes: 14 additions & 12 deletions scripts/make_new_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,21 @@ fi

# Add the new lint to the `add_lints!()` macro.
echo -n "Registering lint in src/query.rs ..."
set +e
# -E = extended regex mode, which behaves more similarly to regex in most programming languages.
# -z = use \0 as separators instead of \n, so that we can do multi-line matches.
grep -Ez --regexp "add_lints\!\\([^)]+[ ]+$NEW_LINT_NAME," "$SRC_QUERY_FILE" >/dev/null
OUTPUT="$?"
set -e
if [[ "$OUTPUT" == "0" ]]; then
echo ' already exists.'
if awk -v lint_name="$NEW_LINT_NAME" '
/^add_lints!\(/ { searching = 1 }
searching && $0 ~ "[[:space:]]" lint_name "," { found = 1; exit }
END { if (found) { exit 0 } else { exit 1 } }
' "$SRC_QUERY_FILE"; then
printf ' already exists.\n'
else
# -E = extended regex mode, which behaves more similarly to regex in most programming languages.
# -z = use \0 as separators instead of \n, so that we can do multi-line matches.
sed -i'' -Ez "s/add_lints\!\\(([^)]+)\\)/add_lints\!(\\1 $NEW_LINT_NAME,\n)/" "$SRC_QUERY_FILE"
echo ' done!'
tmp="${SRC_QUERY_FILE}.tmp"
sed -e '/^add_lints!($/ a\'"
$NEW_LINT_NAME," "$SRC_QUERY_FILE" > "$tmp" && mv -- "$tmp" "$SRC_QUERY_FILE" || {
code=$?
rm -f "$tmp"
exit "$code"
}
printf ' done!\n'
fi

echo ''
Expand Down
4 changes: 2 additions & 2 deletions scripts/make_new_test_crate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if [[ -d "$NEW_LINT_TEST_CRATES_DIR" ]]; then
echo ' already exists.'
else
cp -R "$TEST_CRATES_DIR/template" "$NEW_LINT_TEST_CRATES_DIR"
sed -i'' "s/template/$NEW_TEST_CRATE/g" "$NEW_LINT_TEST_CRATES_DIR/old/Cargo.toml"
sed -i'' "s/template/$NEW_TEST_CRATE/g" "$NEW_LINT_TEST_CRATES_DIR/new/Cargo.toml"
sed -e "s/template/$NEW_TEST_CRATE/g" "$TEST_CRATES_DIR/template/old/Cargo.toml" > "$NEW_LINT_TEST_CRATES_DIR/old/Cargo.toml"
sed -e "s/template/$NEW_TEST_CRATE/g" "$TEST_CRATES_DIR/template/new/Cargo.toml" > "$NEW_LINT_TEST_CRATES_DIR/new/Cargo.toml"
echo ' done!'
fi

0 comments on commit e0d61c7

Please # to comment.