@@ -37,20 +37,36 @@ get_md5sum() {
3737 md5sum " ${file} " | awk ' { print $1}'
3838}
3939
40-
41- find_schema_class () {
40+ find_schema_class_file () {
4241 # The schema class heuristic is a bit hacky for now, we just expect
4342 # the filename containing the schema code to end with Schema or is named InputModel
4443 # We might want to improve this in the future
45- schema_class_file=" $( find src -name " *Schema.scala" -o -name " InputModel.scala" | head -n 1) "
44+ find src -name " *Schema.scala" -o -name " InputModel.scala" | head -n 1 || return 0
45+ }
46+
47+ find_schema_class () {
48+ local schema_class_file=" $1 "
4649 schema_class_name=" $( basename " ${schema_class_file} " .scala) "
4750 schema_package=" $( awk ' $1 == "package" { print $2 }' " ${schema_class_file} " ) "
4851
4952 echo " ${schema_package} .${schema_class_name} "
5053}
5154
55+ find_avro_library () {
56+ local schema_class_file=" $1 "
57+
58+ if grep -q " import com.sksamuel.avro4s" " ${schema_class_file} " ; then
59+ echo " avro4s"
60+ elif grep -q " import vulcan" " ${schema_class_file} " ; then
61+ echo " vulcan"
62+ else
63+ error " Could not find any avro library import in ${schema_class_file} "
64+ fi
65+
66+ }
67+
5268generate_schema_generator_code () {
53- local schema_class=" $1 "
69+ local schema_class=" $1 " schema_library= " $2 "
5470
5571 schema_class_name=" ${schema_class##* .} "
5672 schema_package=" ${schema_class% .* } "
@@ -60,7 +76,7 @@ generate_schema_generator_code() {
6076 sed \
6177 -e " s/__SCHEMA_CLASS_NAME__/${schema_class_name} /g" \
6278 -e " s/__SCHEMA_PACKAGE__/${schema_package} /g" \
63- " ${SCRIPT_DIR} /generators/VulcanSchemaGenerator .tmpl.scala"
79+ " ${SCRIPT_DIR} /generators/${schema_library^} SchemaGenerator .tmpl.scala"
6480}
6581
6682run_schema_generator_code () {
@@ -96,7 +112,13 @@ generator_code_file="${generator_source_folder}/SchemaGenerator.scala"
96112
97113[[ ! -f " ${target_schema_file} " ]] || checksum_before=" $( get_md5sum " ${target_schema_file} " ) "
98114
99- generate_schema_generator_code " $( find_schema_class) " > " ${generator_code_file} "
115+ schema_class_file=" $( find_schema_class_file) "
116+ [[ -n " ${schema_class_file} " ]] || error " Could not find any schema class file"
117+
118+ schema_class=" $( find_schema_class " ${schema_class_file} " ) "
119+ schema_library=" $( find_avro_library " ${schema_class_file} " ) "
120+
121+ generate_schema_generator_code " ${schema_class} " " ${schema_library} " > " ${generator_code_file} "
100122run_schema_generator_code " ${generator_code_file} " " ${target_schema_file} "
101123
102124if ! is_git_tracked " ${target_schema_file} " ; then
105127
106128checksum_after=" $( get_md5sum " ${target_schema_file} " ) "
107129if [[ " ${checksum_after} " != " ${checksum_before:- } " ]]; then
108- error " Schema file \" ${target_schema_file} \" was not consistent with code. Please commit the updated version."
130+ error " Schema file \" ${target_schema_file} \" was missing or not consistent with code. Please commit the updated version."
109131fi
0 commit comments