Skip to content

Commit 9af2e0e

Browse files
committed
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 purescript-contrib#430.
1 parent 4edad29 commit 9af2e0e

File tree

4 files changed

+88
-48
lines changed

4 files changed

+88
-48
lines changed

scripts/prepareExercises.sh

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#! /bin/sh
2+
3+
# This script removes meta information that is not intended for readers of the book
4+
# from the exercises
5+
6+
all=
7+
none=1
8+
removeAnchors=
9+
removeSolutions=
10+
11+
show_help () {
12+
cat <<EOF >&2
13+
Usage: $0 [anchors] [solutions] [all]
14+
15+
Prepare code in exercises/* for reading. If no option is specified,
16+
`all` is used by default.
17+
18+
OPTIONS
19+
all Perform all tasks
20+
anchors Remove code anchors to improve readability
21+
solutions Remove exercise solutions
22+
23+
EOF
24+
}
25+
26+
for opt in "$@"; do
27+
# Reset none, since some action was requested
28+
none=
29+
case $opt in
30+
all )
31+
all=1
32+
;;
33+
help )
34+
show_help
35+
exit 0
36+
;;
37+
anchors )
38+
removeAnchors=1
39+
;;
40+
solutions )
41+
removeSolutions=1
42+
;;
43+
* )
44+
show_help
45+
exit 1
46+
;;
47+
esac
48+
done
49+
50+
if [ x$all != x -o x$none != x ]; then
51+
# Either 'all' was specified, or default to all actions
52+
removeAnchors=1
53+
removeSolutions=1
54+
fi
55+
56+
cat <<EOF
57+
anch? $removeAnchors
58+
solu? $removeSolutions
59+
60+
all ? $all
61+
none? $none
62+
EOF
63+
exit
64+
65+
# Echo commands to shell
66+
set -x
67+
# Exit on first failure
68+
set -e
69+
70+
# All .purs & .js files of chapter exercises.
71+
FILES=$(find exercises \( -name '*.purs' -o -name '*.js' \) -print)
72+
73+
for f in $FILES; do
74+
# Delete lines starting with an 'ANCHOR' comment
75+
[ -n $removeAnchors ] && perl -ni -e 'print if !/^\s*(--|\/\/) ANCHOR/' $f
76+
77+
# Delete lines with a note to delete them
78+
[ -n $removeSolutions ] && perl -ni -e 'print if !/This line should have been automatically deleted/' $f
79+
done
80+
81+
# Move 'no-peeking' sources out of the compilation path
82+
if [ -n $removeSolutions ]; then
83+
for d in exercises/chapter*; do
84+
[ -d "$d/test/no-peeking" ] && mv "$d/test/no-peeking" "$d"
85+
done
86+
fi

scripts/removeAnchors.sh

-23
This file was deleted.

scripts/resetSolutions.sh

-22
This file was deleted.

text/chapter2.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ Now that you've installed the necessary development tools, clone this book's rep
2424
git clone https://github.com/purescript-contrib/purescript-book.git
2525
```
2626

27-
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):
27+
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:
2828

2929
```sh
3030
cd purescript-book
31-
./scripts/resetSolutions.sh
32-
./scripts/removeAnchors.sh
31+
./scripts/prepareExercises.sh
3332
git add .
3433
git commit --all --message "Exercises ready to be solved"
3534
```

0 commit comments

Comments
 (0)