|
16 | 16 | */ |
17 | 17 | package org.sonarsource.cloudnative.gradle |
18 | 18 |
|
| 19 | +import java.io.File |
| 20 | +import java.util.HashSet |
| 21 | +import java.util.jar.JarInputStream |
| 22 | +import org.gradle.api.GradleException |
19 | 23 | import org.gradle.api.Project |
20 | 24 | import org.gradle.api.artifacts.dsl.RepositoryHandler |
21 | 25 | import org.gradle.api.artifacts.repositories.MavenArtifactRepository |
@@ -65,3 +69,38 @@ fun Exec.callMake(arg: String) { |
65 | 69 | commandLine("./make.sh", arg) |
66 | 70 | } |
67 | 71 | } |
| 72 | + |
| 73 | +fun enforceJarSize( |
| 74 | + file: File, |
| 75 | + minSize: Long, |
| 76 | + maxSize: Long, |
| 77 | +) { |
| 78 | + val size = file.length() |
| 79 | + if (size < minSize) { |
| 80 | + throw GradleException("${file.path} size ($size) too small. Min is $minSize") |
| 81 | + } else if (size > maxSize) { |
| 82 | + throw GradleException("${file.path} size ($size) too large. Max is $maxSize") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fun checkJarEntriesPathUniqueness(file: File) { |
| 87 | + val allNames = HashSet<String>() |
| 88 | + val duplicatedNames = HashSet<String>() |
| 89 | + file.inputStream().use { input -> |
| 90 | + JarInputStream(input).use { jarInput -> |
| 91 | + for (jarEntry in generateSequence { jarInput.nextJarEntry }) { |
| 92 | + if (!allNames.add(jarEntry.name)) { |
| 93 | + duplicatedNames.add(jarEntry.name) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if (duplicatedNames.isNotEmpty()) { |
| 99 | + throw GradleException("Duplicated entries in the jar: '${file.path}': ${duplicatedNames.joinToString(", ")}") |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fun Project.commitHashProvider(ref: String = "HEAD") = |
| 104 | + providers.exec { |
| 105 | + commandLine("git", "rev-parse", ref) |
| 106 | + }.standardOutput.asText |
0 commit comments