Skip to content

Commit

Permalink
improve generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegillet committed May 17, 2024
1 parent 62d55ca commit d3b42cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
39 changes: 2 additions & 37 deletions bin/bootstrap_practice_exercise.exs
Original file line number Diff line number Diff line change
@@ -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([
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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", []}, [], [])
Expand Down
37 changes: 37 additions & 0 deletions bin/generate_practice_exercise.sh
Original file line number Diff line number Diff line change
@@ -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 <exercise-slug>"
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}"

0 comments on commit d3b42cf

Please sign in to comment.