Skip to content

Commit 743d9bb

Browse files
committed
[Java] Add a task to verify the generated Ir codecs are in sync with the current version of the generator. If the generator changed then the build will fail unless codecs are updated first.
1 parent 08d2f21 commit 743d9bb

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

build.gradle

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import java.nio.file.Files
2+
import java.nio.file.Paths
3+
import java.nio.file.StandardOpenOption
4+
import java.security.MessageDigest
5+
16
/*
27
* Copyright 2013-2021 Real Logic Limited.
38
*
@@ -20,7 +25,7 @@ plugins {
2025
id "com.github.ben-manes.versions" version "0.39.0"
2126
}
2227

23-
defaultTasks 'clean', 'build'
28+
defaultTasks 'clean', 'verifyJavaIrCodecsInSync', 'build'
2429

2530
static def getBuildJavaVersion() {
2631
def buildJavaVersion = System.getenv('BUILD_JAVA_VERSION') ?: JavaVersion.current().getMajorVersion()
@@ -660,7 +665,7 @@ task generateCSharpCodecs {
660665
'generateCSharpCodecsWithXIncludes'
661666
}
662667

663-
task generateJavaIrCodecs(type: JavaExec) {
668+
task generateJavaIrCodecs(type: JavaExec, dependsOn: 'computeOriginalIrHash') {
664669
mainClass = 'uk.co.real_logic.sbe.SbeTool'
665670
classpath = project(':sbe-all').sourceSets.main.runtimeClasspath
666671
systemProperties(
@@ -708,6 +713,32 @@ task testReport(type: TestReport) {
708713
reportOn subprojects*.test
709714
}
710715

716+
final codecsDir = Paths.get('sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/generated').toFile()
717+
final outputDir = Paths.get('sbe-tool/build')
718+
final originalHashFile = outputDir.resolve('original.sha256').toFile()
719+
final newHashFile = outputDir.resolve('new.sha256').toFile()
720+
721+
task computeOriginalIrHash(type: ChecksumTask) {
722+
inputDirectory = codecsDir
723+
outputFile = originalHashFile
724+
}
725+
726+
task computeUpdatedIrHash(type: ChecksumTask, dependsOn: 'generateJavaIrCodecs') {
727+
inputDirectory = codecsDir
728+
outputFile = newHashFile
729+
}
730+
731+
task verifyJavaIrCodecsInSync(dependsOn: 'computeUpdatedIrHash') {
732+
doLast {
733+
final byte[] oldHash = Files.readAllBytes(originalHashFile.toPath())
734+
final byte[] newHash = Files.readAllBytes(newHashFile.toPath())
735+
if (!Arrays.equals(oldHash, newHash))
736+
{
737+
throw new GradleException("Java Ir codecs are out of sync!")
738+
}
739+
}
740+
}
741+
711742
def isNonStable = { String version ->
712743
def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
713744
def regex = /^[0-9,.v-]+(-r)?$/
@@ -725,3 +756,34 @@ wrapper {
725756
gradleVersion = '7.1.1'
726757
distributionType = 'ALL'
727758
}
759+
760+
class ChecksumTask extends DefaultTask
761+
{
762+
private static final MessageDigest SHA_256 = MessageDigest.getInstance("SHA-256");
763+
764+
@InputDirectory
765+
File inputDirectory
766+
767+
@OutputFile
768+
File outputFile
769+
770+
@TaskAction
771+
def checksum() {
772+
SHA_256.reset()
773+
774+
final File[] files = inputDirectory.listFiles();
775+
for (final File f : files)
776+
{
777+
SHA_256.update(Files.readAllBytes(f.toPath()))
778+
}
779+
780+
final byte[] hash = SHA_256.digest()
781+
782+
Files.write(
783+
outputFile.toPath(),
784+
hash,
785+
StandardOpenOption.CREATE,
786+
StandardOpenOption.WRITE,
787+
StandardOpenOption.TRUNCATE_EXISTING)
788+
}
789+
}

0 commit comments

Comments
 (0)