Skip to content

Commit

Permalink
scripts: merge removeAnchors.sh and resetSolutions.sh
Browse files Browse the repository at this point in the history
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 purescript-contrib#430.
  • Loading branch information
softmoth committed Aug 31, 2023
1 parent 4edad29 commit fa9f8ce
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 48 deletions.
77 changes: 77 additions & 0 deletions scripts/prepareExercises.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF >&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
23 changes: 0 additions & 23 deletions scripts/removeAnchors.sh

This file was deleted.

22 changes: 0 additions & 22 deletions scripts/resetSolutions.sh

This file was deleted.

5 changes: 2 additions & 3 deletions text/chapter2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
Expand Down

0 comments on commit fa9f8ce

Please sign in to comment.