From fa9f8cec3c690fccb19df8d1e5130fa858ff391f Mon Sep 17 00:00:00 2001 From: Tim Siegel Date: Thu, 31 Aug 2023 18:34:56 -0400 Subject: [PATCH 1/3] 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. --- scripts/prepareExercises.sh | 77 +++++++++++++++++++++++++++++++++++++ scripts/removeAnchors.sh | 23 ----------- scripts/resetSolutions.sh | 22 ----------- text/chapter2.md | 5 +-- 4 files changed, 79 insertions(+), 48 deletions(-) create mode 100755 scripts/prepareExercises.sh delete mode 100755 scripts/removeAnchors.sh delete mode 100755 scripts/resetSolutions.sh diff --git a/scripts/prepareExercises.sh b/scripts/prepareExercises.sh new file mode 100755 index 000000000..73efcfc6e --- /dev/null +++ b/scripts/prepareExercises.sh @@ -0,0 +1,77 @@ +#! /bin/sh + +# This script removes meta information that is not intended for readers of the book +# from the exercises + +all= +none=1 +removeAnchors= +removeSolutions= + +show_help () { + cat <&2 +Usage: $0 [anchors] [solutions] [all] + +Prepare code in exercises/* for reading. If no option is specified, +`all` is used by default. + +OPTIONS + all Perform all tasks + anchors Remove code anchors to improve readability + solutions Remove exercise solutions + +EOF +} + +for opt in "$@"; do + # Reset none, since some action was requested + none= + case $opt in + all ) + all=1 + ;; + help ) + show_help + exit 0 + ;; + anchors ) + removeAnchors=1 + ;; + solutions ) + removeSolutions=1 + ;; + * ) + show_help + exit 1 + ;; + esac +done + +if [ x$all != x -o x$none != x ]; then + # Either 'all' was specified, or default to all actions + removeAnchors=1 + removeSolutions=1 +fi + +# Echo commands to shell +set -x +# Exit on first failure +set -e + +# All .purs & .js files of chapter exercises. +FILES=$(find exercises \( -name '*.purs' -o -name '*.js' \) -print) + +for f in $FILES; do + # Delete lines starting with an 'ANCHOR' comment + [ -n $removeAnchors ] && perl -ni -e 'print if !/^\s*(--|\/\/) ANCHOR/' $f + + # Delete lines with a note to delete them + [ -n $removeSolutions ] && perl -ni -e 'print if !/This line should have been automatically deleted/' $f +done + +# Move 'no-peeking' sources out of the compilation path +if [ -n $removeSolutions ]; then + for d in exercises/chapter*; do + [ -d "$d/test/no-peeking" ] && mv "$d/test/no-peeking" "$d" + done +fi 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" ``` From dd5a1df15848c39e2766c7fe35ac42bec13c735c Mon Sep 17 00:00:00 2001 From: Miles Frain Date: Tue, 5 Sep 2023 20:03:39 -0700 Subject: [PATCH 2/3] Simplify scripts/prepareExercises.sh --- scripts/prepareExercises.sh | 83 ++++++++----------------------------- 1 file changed, 17 insertions(+), 66 deletions(-) diff --git a/scripts/prepareExercises.sh b/scripts/prepareExercises.sh index 73efcfc6e..841e96075 100755 --- a/scripts/prepareExercises.sh +++ b/scripts/prepareExercises.sh @@ -1,77 +1,28 @@ -#! /bin/sh +#!/usr/bin/env bash # This script removes meta information that is not intended for readers of the book -# from the exercises - -all= -none=1 -removeAnchors= -removeSolutions= - -show_help () { - cat <&2 -Usage: $0 [anchors] [solutions] [all] - -Prepare code in exercises/* for reading. If no option is specified, -`all` is used by default. - -OPTIONS - all Perform all tasks - anchors Remove code anchors to improve readability - solutions Remove exercise solutions - -EOF -} - -for opt in "$@"; do - # Reset none, since some action was requested - none= - case $opt in - all ) - all=1 - ;; - help ) - show_help - exit 0 - ;; - anchors ) - removeAnchors=1 - ;; - solutions ) - removeSolutions=1 - ;; - * ) - show_help - exit 1 - ;; - esac -done - -if [ x$all != x -o x$none != x ]; then - # Either 'all' was specified, or default to all actions - removeAnchors=1 - removeSolutions=1 -fi # Echo commands to shell set -x # Exit on first failure set -e -# All .purs & .js files of chapter exercises. -FILES=$(find exercises \( -name '*.purs' -o -name '*.js' \) -print) +# 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 - [ -n $removeAnchors ] && perl -ni -e 'print if !/^\s*(--|\/\/) ANCHOR/' $f + 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 - [ -n $removeSolutions ] && perl -ni -e 'print if !/This line should have been automatically deleted/' $f -done + # Delete lines with a note to delete them + perl -ni -e 'print if !/This line should have been automatically deleted/' $f + done -# Move 'no-peeking' sources out of the compilation path -if [ -n $removeSolutions ]; then - for d in exercises/chapter*; do - [ -d "$d/test/no-peeking" ] && mv "$d/test/no-peeking" "$d" - done -fi + # 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 From af1065c184e181ce8af02d00cd8874913dcfdeab Mon Sep 17 00:00:00 2001 From: Miles Frain Date: Tue, 5 Sep 2023 20:06:52 -0700 Subject: [PATCH 3/3] Add prepareExercises.sh to CI --- .github/workflows/tests.yml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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