firmware_tools/differences/json.groovy
Dan Hawkins c1344e9084 Squashed 'Commons_scripts/' content from commit 0326879
git-subtree-dir: Commons_scripts
git-subtree-split: 0326879391
2022-02-13 17:04:54 +00:00

51 lines
1.5 KiB
Groovy

def comparefiles(importedjsonfile, currentjsonfile) {
echo "Comparing json 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 . "${currentjsonfile}" > temp2.txt
if (\$lastexitcode -ge 1) { exit 1 }
# echo Diffing sorted json files
diff.exe -c temp1.txt temp2.txt
\$lec = \$lastexitcode
Remove-Item temp1.txt
Remove-Item temp2.txt
if (\$lec -ge 1) { exit 1 }
"""
}
}
return this;