|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Safety measures |
| 4 | +set -o errexit # Leave immediately if a command returns an error |
| 5 | +set -o nounset # Leave immediately if an unitialized value is used |
| 6 | +set -o pipefail # Leave immediately if a command fails in a pipe |
| 7 | + |
| 8 | +[[ "${BASH_VERSION}" =~ ^(5|4\.[0-9]).* ]] && shopt -s inherit_errexit |
| 9 | + |
| 10 | +SCRIPT_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | + |
| 12 | +##################################################################### |
| 13 | +# Helper functions |
| 14 | +##################################################################### |
| 15 | + |
| 16 | +error() { |
| 17 | + local msg="$1" exit_code="${2:-1}" |
| 18 | + echo "ERROR: ${msg}">&2 |
| 19 | + exit "${exit_code}" |
| 20 | +} |
| 21 | + |
| 22 | +check_binary_exists() { |
| 23 | + local binary="$1" |
| 24 | + command -v "${binary}" &>/dev/null || error "${binary} is required but it's not installed" |
| 25 | +} |
| 26 | + |
| 27 | +clean_temporary_folder() { |
| 28 | + [[ -z "${generator_source_folder:-}" ]] || rm -rf "${generator_source_folder}" |
| 29 | +} |
| 30 | + |
| 31 | +is_git_tracked() { |
| 32 | + git ls-files --error-unmatch "$1" &> /dev/null || return 1 |
| 33 | +} |
| 34 | + |
| 35 | +get_md5sum() { |
| 36 | + local file="$1" |
| 37 | + md5sum "${file}" | awk '{ print $1}' |
| 38 | +} |
| 39 | + |
| 40 | +find_schema_class_file() { |
| 41 | + # The schema class heuristic is a bit hacky for now, we try to find a file |
| 42 | + # where a class has been annotated with the schema annotation |
| 43 | + # Otherwise we fallback on finding the filename containing the schema code |
| 44 | + # to end with Schema or is named InputModel |
| 45 | + # We might want to improve this in the future |
| 46 | + schema_class_file="$(grep -lr "^@schema" src | head -n 1 || return 0)" |
| 47 | + |
| 48 | + if [[ -z "${schema_class_file}" ]]; then |
| 49 | + schema_class_file="$(find src -name "*Schema.scala" -o -name "InputModel.scala" | head -n 1 || return 0)" |
| 50 | + fi |
| 51 | + |
| 52 | + echo "${schema_class_file}" |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +find_schema_class() { |
| 57 | + local schema_class_file="$1" |
| 58 | + schema_class_name="$(basename "${schema_class_file}" .scala)" |
| 59 | + schema_package="$(awk ' $1 == "package" { print $2 }' "${schema_class_file}")" |
| 60 | + |
| 61 | + echo "${schema_package}.${schema_class_name}" |
| 62 | +} |
| 63 | + |
| 64 | +find_avro_library() { |
| 65 | + local schema_class_file="$1" |
| 66 | + |
| 67 | + if grep -q "import com.sksamuel.avro4s" "${schema_class_file}"; then |
| 68 | + echo "avro4s" |
| 69 | + elif grep -q "import vulcan" "${schema_class_file}"; then |
| 70 | + echo "vulcan" |
| 71 | + else |
| 72 | + error "Could not find any avro library import in ${schema_class_file}" |
| 73 | + fi |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +generate_schema_generator_code() { |
| 78 | + local schema_class="$1" schema_library="$2" |
| 79 | + |
| 80 | + schema_class_name="${schema_class##*.}" |
| 81 | + schema_package="${schema_class%.*}" |
| 82 | + |
| 83 | + # only schema class using vulcan are supported for now |
| 84 | + # but we might add support for avro4s in the future |
| 85 | + sed \ |
| 86 | + -e "s/__SCHEMA_CLASS_NAME__/${schema_class_name}/g" \ |
| 87 | + -e "s/__SCHEMA_PACKAGE__/${schema_package}/g" \ |
| 88 | + "${SCRIPT_DIR}/generators/${schema_library^}SchemaGenerator.tmpl.scala" |
| 89 | +} |
| 90 | + |
| 91 | +run_schema_generator_code() { |
| 92 | + local generator_code_file="$1" target_schema_file="$2" |
| 93 | + |
| 94 | + generator_source_folder="$(dirname "${generator_code_file}")" |
| 95 | + |
| 96 | + sbt_command="" |
| 97 | + # When fork is enabled, it seems we can't avoid all sbt logs to be printed |
| 98 | + # so we just disable it |
| 99 | + sbt_command+="set fork := false;" |
| 100 | + # We tell sbt to look for our generator code in the temporary folder in addition |
| 101 | + # to the existing source code, so we can run our generator code alongside the existing code |
| 102 | + # We need that as the generator code import the schema class |
| 103 | + sbt_command+="set Compile / unmanagedSourceDirectories += file(\"${generator_source_folder}\");" |
| 104 | + sbt_command+="runMain kp_pre_commit_hooks.generateSchemaFile ${target_schema_file}" |
| 105 | + |
| 106 | + sbt -batch -error "${sbt_command}" |
| 107 | + # Add a last linefeed to make pre-commit end-of-line fixer happy |
| 108 | + echo >> "${target_schema_file}" |
| 109 | +} |
| 110 | + |
| 111 | +##################################################################### |
| 112 | +# Main code |
| 113 | +##################################################################### |
| 114 | + |
| 115 | +trap clean_temporary_folder EXIT |
| 116 | + |
| 117 | +check_binary_exists "sbt" |
| 118 | + |
| 119 | +target_schema_file="schemas/schema.avsc" |
| 120 | + |
| 121 | +generator_source_folder="$(mktemp -d)" |
| 122 | +generator_code_file="${generator_source_folder}/SchemaGenerator.scala" |
| 123 | + |
| 124 | +[[ ! -f "${target_schema_file}" ]] || checksum_before="$(get_md5sum "${target_schema_file}")" |
| 125 | + |
| 126 | +schema_class_file="$(find_schema_class_file)" |
| 127 | +[[ -n "${schema_class_file}" ]] || error "Could not find any schema class file" |
| 128 | + |
| 129 | +schema_class="$(find_schema_class "${schema_class_file}")" |
| 130 | +schema_library="$(find_avro_library "${schema_class_file}")" |
| 131 | + |
| 132 | +generate_schema_generator_code "${schema_class}" "${schema_library}" > "${generator_code_file}" |
| 133 | +run_schema_generator_code "${generator_code_file}" "${target_schema_file}" |
| 134 | + |
| 135 | +if ! is_git_tracked "${target_schema_file}"; then |
| 136 | + error "Schema file \"${target_schema_file}\" is not tracked by git. Please commit it." |
| 137 | +fi |
| 138 | + |
| 139 | +checksum_after="$(get_md5sum "${target_schema_file}")" |
| 140 | +if [[ "${checksum_after}" != "${checksum_before:-}" ]]; then |
| 141 | + error "Schema file \"${target_schema_file}\" was missing or not consistent with code. Please commit the updated version." |
| 142 | +fi |
0 commit comments