|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +print_usage() { |
| 4 | + echo "Usage: ./canonical_data_check.sh -t path/to/track -s path/to/problem/specifications" |
| 5 | +} |
| 6 | + |
| 7 | +# Execution begins |
| 8 | + |
| 9 | +command -v jq >/dev/null 2>&1 || { |
| 10 | + echo >&2 "This script requires jq but it's not installed. Aborting." |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +num_args=$# |
| 15 | + |
| 16 | +if [ $num_args -eq 0 ] |
| 17 | +then |
| 18 | + print_usage |
| 19 | + exit 0 |
| 20 | +fi |
| 21 | + |
| 22 | +path_to_track= |
| 23 | +path_to_problem_specifications= |
| 24 | + |
| 25 | +while getopts ":t:s:" option |
| 26 | +do |
| 27 | + case "$option" in |
| 28 | + "t") |
| 29 | + path_to_track="$OPTARG" |
| 30 | + ;; |
| 31 | + "s") |
| 32 | + path_to_problem_specifications="$OPTARG" |
| 33 | + ;; |
| 34 | + *) |
| 35 | + echo "Unrecognized option. Aborting." |
| 36 | + print_usage |
| 37 | + exit 1 |
| 38 | + ;; |
| 39 | + esac |
| 40 | +done |
| 41 | + |
| 42 | +if [ -z "$path_to_track" ] |
| 43 | +then |
| 44 | + echo "Path to track missing." |
| 45 | + print_usage |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +if [ -z "$path_to_problem_specifications" ] |
| 50 | +then |
| 51 | + echo "Path to problem specifications missing." |
| 52 | + print_usage |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +config_file_path="$path_to_track/config.json" |
| 57 | + |
| 58 | +if ! [ -f "$config_file_path" ] |
| 59 | +then |
| 60 | + echo "Config file not found at $config_file_path." |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +track_exercise_slugs=$(jq '.exercises[] | select(has("deprecated") | not) | .slug' $config_file_path | tr -d "\"") |
| 65 | + |
| 66 | +for slug in $track_exercise_slugs |
| 67 | +do |
| 68 | + canonical_data_folder_path="$path_to_problem_specifications/exercises/$slug" |
| 69 | + |
| 70 | + if ! [ -d "$canonical_data_folder_path" ] |
| 71 | + then |
| 72 | + echo "Canonical data folder $canonical_data_folder_path not found. Aborting." |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + |
| 76 | + canonical_data_file_path="$canonical_data_folder_path/canonical-data.json" |
| 77 | + |
| 78 | + if ! [ -f "$canonical_data_file_path" ] |
| 79 | + then |
| 80 | + # echo "$slug: no canonical data found." |
| 81 | + continue |
| 82 | + fi |
| 83 | + |
| 84 | + canonical_data_version=$(jq '.version' $canonical_data_file_path | tr -d "\"") |
| 85 | + |
| 86 | + track_exercise_version_file_path="$path_to_track/exercises/$slug/.meta/version" |
| 87 | + |
| 88 | + if ! [ -f "$track_exercise_version_file_path" ] |
| 89 | + then |
| 90 | + echo "$slug: needs update or version file (v$canonical_data_version)." |
| 91 | + continue |
| 92 | + fi |
| 93 | + |
| 94 | + track_data_version=$(cat $track_exercise_version_file_path) |
| 95 | + |
| 96 | + if [ "$track_data_version" = "$canonical_data_version" ] |
| 97 | + then |
| 98 | + # echo "$slug: up-to-date." |
| 99 | + continue |
| 100 | + else |
| 101 | + echo "$slug: needs update (v$track_data_version -> v$canonical_data_version)." |
| 102 | + fi |
| 103 | + |
| 104 | +done |
0 commit comments