firmware_tools/differences/json.groovy
2021-06-30 23:04:07 +01:00

52 lines
1.5 KiB
Groovy

def comparefiles(importedjsonfile, currentjsonfile) {
echo "Comparing file: ${importedjsonfile} with: ${currentjsonfile}"
if (isUnix()) {
sh """
which jq
if [ \$? -ne 0 ]; then exit 1; fi
# jq -S sorts json output
diff -c <(jq -S . "${importedjsonfile}") <(jq -S . "${currentjsonfile}")
"""
}
else {
// Jq and diffutils can be installed on windows with chocolatey
powershell """
# echo Checking utilities exist
# set
# echo %PATH%
try {
if (Get-Command jq) {}
}
catch {
echo "jq is unavailable"
exit $false
}
try {
if (Get-Command diff) {}
}
catch {
echo "diff is unavailable"
exit $false
}
# echo Sorting json in files to compare
# jq -S sorts json output
jq -S . "${importedjsonfile}" > temp1.txt
if (\$lastexitcode -ge 1) { exit 1 }
jq -S . "${importedjsonfile}" > temp2.txt
if (\$lastexitcode -ge 1) { exit 1 }
# echo Diffing sorted json files
diff -c temp1.txt temp2.txt
\$el = \$lastexitcode
Remove-Item temp1.txt
Remove-Item temp2.txt
# echo Diffing result: \$el
if (\$el -ge 1) { exit 1 }
"""
}
}
return this;