From 427f6986ed5638cfb5d13d454c057d37e04c8427 Mon Sep 17 00:00:00 2001 From: Tim Siegel Date: Wed, 6 Sep 2023 14:30:00 -0400 Subject: [PATCH] scripts: merge removeAnchors.sh and resetSolutions.sh (#457) * scripts: merge removeAnchors.sh and resetSolutions.sh Keeping it all in a single `scripts/prepareExercises.sh` avoids duplication. Also remove the complex file finding; simply getting all source files within the `exercises` directory is a perfect fit for what is needed. This fixes Issue #430. * Simplify scripts/prepareExercises.sh * Add prepareExercises.sh to CI --------- Co-authored-by: Miles Frain --- .github/workflows/tests.yml | 10 ++-------- scripts/prepareExercises.sh | 28 ++++++++++++++++++++++++++++ scripts/removeAnchors.sh | 23 ----------------------- scripts/resetSolutions.sh | 22 ---------------------- text/chapter2.md | 5 ++--- 5 files changed, 32 insertions(+), 56 deletions(-) create mode 100755 scripts/prepareExercises.sh delete mode 100755 scripts/removeAnchors.sh delete mode 100755 scripts/resetSolutions.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7cd811f36..25c1a77dc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,14 +49,8 @@ jobs: - name: Run tests run: ./scripts/testAll.sh - - name: Remove anchors - run: ./scripts/removeAnchors.sh - - - name: Run tests again - run: ./scripts/testAll.sh - - - name: Reset solutions - run: ./scripts/resetSolutions.sh + - name: Prepare exercises + run: ./scripts/prepareExercises.sh - name: Run tests again run: ./scripts/testAll.sh diff --git a/scripts/prepareExercises.sh b/scripts/prepareExercises.sh new file mode 100755 index 000000000..841e96075 --- /dev/null +++ b/scripts/prepareExercises.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# This script removes meta information that is not intended for readers of the book + +# Echo commands to shell +set -x +# Exit on first failure +set -e + +# For all chapters +for d in exercises/chapter*; do + # All .purs & .js files of chapter exercises + FILES=$(find $d/src $d/test -name '*.purs' -o -name '*.js') + + for f in $FILES; do + # Delete lines starting with an 'ANCHOR' comment + perl -ni -e 'print if !/^\s*(--|\/\/) ANCHOR/' $f + + # Delete lines with a note to delete them + perl -ni -e 'print if !/This line should have been automatically deleted/' $f + done + + # If there's a no-peeking directory + if [ -d $d/test/no-peeking ]; then + # Move 'no-peeking' sources out of the compilation path + mv $d/test/no-peeking $d + fi +done diff --git a/scripts/removeAnchors.sh b/scripts/removeAnchors.sh deleted file mode 100755 index 3062bb3bd..000000000 --- a/scripts/removeAnchors.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -# This script removes all code anchors to improve readability - -# Echo commands to shell -set -x -# Exit on first failure -set -e - -# All .purs & .js files in the src/ and test/ directories of chapter exercises. -FIND_FILES_PATTERN='\./exercises/chapter[0-9]{1,2}/(src|test)/.*\.(purs|js)' - -EXTENDED_REGEX_FLAGS="-regextype posix-extended" -# BSD find has different flags for extended regex -if [[ $(uname) == 'FreeBSD' || $(uname) == 'Darwin' ]]; then - EXTENDED_REGEX_FLAGS='-E' -fi -FILES=$(find . -type f $EXTENDED_REGEX_FLAGS -regex $FIND_FILES_PATTERN) - -for f in $FILES; do - # Delete lines starting with an 'ANCHOR' comment - perl -ni -e 'print if !/^\s*(--|\/\/) ANCHOR/' $f -done diff --git a/scripts/resetSolutions.sh b/scripts/resetSolutions.sh deleted file mode 100755 index 0088aec5e..000000000 --- a/scripts/resetSolutions.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -# This script automatically resets exercises so they are ready to be solved. -# - Removes lines with a note to delete them. -# - Moves the no-peeking directory outside of the compilation path. - -# Echo commands to shell -set -x -# Exit on first failure -set -e - -# For all chapters -for d in exercises/*; do - # if directory (excludes LICENSE file) - if [ -d $d ]; then - perl -ni -e 'print if !/This line should have been automatically deleted/' $d/test/Main.purs - fi - # if there's a no-peeking directory - if [ -d $d/test/no-peeking ]; then - mv $d/test/no-peeking $d/no-peeking - fi -done diff --git a/text/chapter2.md b/text/chapter2.md index 697a2ed2a..69863fc40 100644 --- a/text/chapter2.md +++ b/text/chapter2.md @@ -24,12 +24,11 @@ Now that you've installed the necessary development tools, clone this book's rep git clone https://github.com/purescript-contrib/purescript-book.git ``` -The book repo contains PureScript example code and unit tests for the exercises that accompany each chapter. There's some initial setup required to reset the exercise solutions so they are ready to be solved by you. Use the `resetSolutions.sh` script to simplify this process. While at it, you should also strip out all the anchor comments with the `removeAnchors.sh` script (these anchors are used for copying code snippets into the book's rendered markdown, and you probably don't need this clutter in your local repo): +The book repo contains PureScript example code and unit tests for the exercises that accompany each chapter. There's some initial setup required to reset the exercise solutions so they are ready to be solved by you. Use the `prepareExercises.sh` script to simplify this process: ```sh cd purescript-book -./scripts/resetSolutions.sh -./scripts/removeAnchors.sh +./scripts/prepareExercises.sh git add . git commit --all --message "Exercises ready to be solved" ```