Skip to content

Instantly share code, notes, and snippets.

@yuler
Last active October 31, 2024 10:38
Show Gist options
  • Select an option

  • Save yuler/3e2dbd1a735767e71469c726f0ec0d22 to your computer and use it in GitHub Desktop.

Select an option

Save yuler/3e2dbd1a735767e71469c726f0ec0d22 to your computer and use it in GitHub Desktop.
A bash for fetch exercism.org
#!/usr/bin/env bash
WORKSPACE=$HOME/Exercism/ruby
TRACK=ruby
EXTENSION=rb
cd "$WORKSPACE" || exit
# Get exercise name
if [ -n "$1" ]; then
name="$1"
else
read -rp "Enter exercise name: " name
fi
filename=$(echo "$name" | tr '-' '_')
# Clear other directory
dirs=$(ls -d ./*/)
for dir in $dirs; do
echo "remove: $dir"
rm -rf "$dir"
done
echo "download: exercise $name"
if ! exercism download --track=$TRACK --force --exercise="$name" >/dev/null 2>&1; then
echo "Failed to download exercise: $name"
exit 1
fi
# Remove `skip` flag in test file
echo "change: ./$name/${filename}_test.$EXTENSION test file"
sed -i "" -e "s/^\([[:space:]]*\)skip$/\1# skip/g" "./$name/${filename}_test.$EXTENSION"
# Open files in VSCode
code "$name/README.md"
code "$name/${filename}.$EXTENSION"
code "$name/${filename}_test.$EXTENSION"
# Tips
cat <<EOF
Tips:
---
cd $name
exercism test
exercism submit
readme: $name/README.md
code: $name/${filename}.$EXTENSION
tests: $name/${filename}_test.$EXTENSION
solutions: $name/solutions.md
EOF
# Fetch community solutions
echo "fetch: exercise $name community solutions"
# Get the token from the exercism configuration
TOKEN=$(exercism configure 2>&1 | grep "Token" | sed "s/.*[[:space:]]//")
# Invoke the exercism API to get the community solutions for the exercise
# Refs: https://github.com/exercism/website/blob/223cc20499ca037858ae4aeda5a3e1df366f6ede/config/routes/api.rb#L100C20-L100C39
printf "# Solutions\n\n" >"$name/solutions.md"
curl -H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
"https://api.exercism.io/v2/tracks/$TRACK/exercises/$name/community_solutions" |
jq -r ".results[:8][].uuid" |
while IFS= read -r uuid; do
echo "fetch: solution for uuid $uuid"
printf "## %s\n\n" "$uuid" >>"$name/solutions.md"
printf "\`\`\`$TRACK\n" >>"$name/solutions.md"
curl -s -H "Authorization: Bearer $TOKEN" "https://exercism.org/api/v1/solutions/$uuid/files/${name//-/_}.$EXTENSION" >>"$name/solutions.md"
printf "\n\`\`\`\n\n" >>"$name/solutions.md"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment