diff --git a/bin/bootstrap_practice_exercise.exs b/bin/bootstrap_practice_exercise.exs index 269249a43..7c1bacd38 100644 --- a/bin/bootstrap_practice_exercise.exs +++ b/bin/bootstrap_practice_exercise.exs @@ -1,8 +1,8 @@ # Generate all files required for a practice exercise. # File content is filled as much as possible, but some, including tests, need manual input. # -# Run the following command from the root of the repo: -# $ elixir bin/boostrap_practice_exercise.exs complex-numbers +# Do not run directly, instead use this script: +# $ bin/generate_practice_exercise.sh complex-numbers # Pass the name of the exercise (e. g., "complex-numbers") as an argument Mix.install([ @@ -143,9 +143,6 @@ module = ## Step 1: create folder structure -Mix.Generator.create_directory("exercises/practice/#{exercise}") -Mix.Generator.create_directory("exercises/practice/#{exercise}/.docs") -Mix.Generator.create_directory("exercises/practice/#{exercise}/.meta") Mix.Generator.create_directory("exercises/practice/#{exercise}/lib") Mix.Generator.create_directory("exercises/practice/#{exercise}/test") @@ -243,38 +240,6 @@ url = :inets.start() :ssl.start() -# .docs/instructions.md -{:ok, {_status, _header, description}} = - :httpc.request(:get, {url ++ ~c"/description.md", []}, [], []) - -Mix.Generator.create_file("exercises/practice/#{exercise}/.docs/instructions.md", description) - -# .meta/config.json -{:ok, {_status, _header, metadata}} = - :httpc.request(:get, {url ++ ~c"/metadata.toml", []}, [], []) - -metadata = - metadata - |> to_string - |> Toml.decode!() - -config = %{ - authors: [], - contributors: [], - files: %{ - solution: ["lib/#{exercise_snake_case}.ex"], - test: ["test/#{exercise_snake_case}_test.exs"], - example: [".meta/example.ex"] - } -} - -config = - Map.merge(metadata, config) - |> Jason.encode!(pretty: true) - -Mix.Generator.create_file("exercises/practice/#{exercise}/.meta/config.json", config) -IO.puts("Don't forget to add your name and the names of contributors") - # tests and lib files {:ok, {_status, _header, data}} = :httpc.request(:get, {url ++ ~c"/canonical-data.json", []}, [], []) diff --git a/bin/generate_practice_exercise.sh b/bin/generate_practice_exercise.sh new file mode 100755 index 000000000..aaca7cfb7 --- /dev/null +++ b/bin/generate_practice_exercise.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +# Exit if anything fails. +set -eo pipefail + +# If argument not provided, print usage and exit +if [ -z "$1" ]; then + echo "Usage: bin/generate_practice_exercise.sh " + exit 1 +fi + +SLUG="$1" +exercise_dir="exercises/practice/${SLUG}" + +# build configlet +echo "Fetching latest version of configlet..." +./bin/fetch-configlet + +# Preparing config.json +echo "Adding instructions and configuration files..." +UUID=$(bin/configlet uuid) +jq --arg slug "$SLUG" --arg uuid "$UUID" \ + '.exercises.practice += [{slug: $slug, name: "TODO", uuid: $uuid, practices: [], prerequisites: [], difficulty: 5}]' \ + config.json > config.json.tmp +mv config.json.tmp config.json + +# Create instructions and config files +./bin/configlet sync --update --yes --docs --filepaths --metadata --exercise "$SLUG" + +# Create Elixir files +echo "Creating Elixir files..." +elixir bin/bootstrap_practice_exercise.exs $SLUG + +echo "All stub files were created. After implementing the solution, tests and configuration, please run:" +echo " elixir bin/check_practice_exercise_order.exs --write" +echo " ./bin/configlet sync --update --tests --exercise ${SLUG}" +echo " ./bin/configlet fmt --update --yes --exercise ${SLUG}"