Skip to content

Commit c579893

Browse files
Merge pull request #31 from Kpler/fix/PTFM-9207/fix-some-issues-in-kafka-schema-generation
fix: improve avro library detection
2 parents a9a7941 + 60071b4 commit c579893

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

kafka/check-local-schemas.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,27 @@ find_schema_class() {
6161
echo "${schema_package}.${schema_class_name}"
6262
}
6363

64+
is_library_used() {
65+
local library="$1" candidate_class_file="$2"
66+
67+
# if the library is not directly found in the candidate class file
68+
# we fallback on checking the build.sbt file itself
69+
# This doesn't fully protect against from indirect library loading
70+
# but it's a good enough heuristic for now
71+
for candidate in "${candidate_class_file}" build.sbt; do
72+
if grep -q -E "[^#]*${library}" "${candidate}"; then
73+
return 0
74+
fi
75+
done
76+
return 1
77+
}
78+
6479
find_avro_library() {
6580
local schema_class_file="$1"
6681

67-
if grep -q "import com.sksamuel.avro4s" "${schema_class_file}"; then
82+
if is_library_used "com.sksamuel.avro4s" "${schema_class_file}"; then
6883
echo "avro4s"
69-
elif grep -q "import vulcan" "${schema_class_file}"; then
84+
elif is_library_used "vulcan" "${schema_class_file}"; then
7085
echo "vulcan"
7186
else
7287
error "Could not find any avro library import in ${schema_class_file}"
@@ -101,6 +116,10 @@ run_schema_generator_code() {
101116
# to the existing source code, so we can run our generator code alongside the existing code
102117
# We need that as the generator code import the schema class
103118
sbt_command+="set Compile / unmanagedSourceDirectories += file(\"${generator_source_folder}\");"
119+
# Dynamically add the required dependencies to the build.sbt file
120+
sbt_command+="set libraryDependencies += \"com.lihaoyi\" %% \"upickle\" % \"3.1.3\";"
121+
sbt_command+="set libraryDependencies += \"com.lihaoyi\" %% \"os-lib\" % \"0.9.1\";"
122+
104123
sbt_command+="runMain kp_pre_commit_hooks.generateSchemaFile ${target_schema_file}"
105124

106125
sbt -batch -error "${sbt_command}"

0 commit comments

Comments
 (0)