Skip to content

Commit

Permalink
ktlint format the code in dataconnect/buildSrc by running ./gradlew k…
Browse files Browse the repository at this point in the history
…tlintCheck
  • Loading branch information
dconeybe committed Nov 20, 2024
1 parent 4600046 commit 0c41373
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.gradle.kotlin.dsl.property
data class OperatingSystem(
@get:Input val type: Type,
@get:Input val arch: Architecture,
@get:Internal val description: String?,
@get:Internal val description: String?
) : java.io.Serializable {

enum class Type {
Expand Down Expand Up @@ -73,15 +73,16 @@ fun OperatingSystem.Companion.provider(
val arch = osArch?.let { OperatingSystem.Architecture.forName(it) }

if (type === null || arch === null) {
throw GradleException("unable to determine operating system from $description " +
" (type=$type, arch=$arch) (error code qecxcvcf8n)"
throw GradleException(
"unable to determine operating system from $description " +
" (type=$type, arch=$arch) (error code qecxcvcf8n)"
)
}

OperatingSystem(
type = OperatingSystem.Type.Linux,
arch = OperatingSystem.Architecture.X86_64,
description=description,
description = description
)
}

Expand Down Expand Up @@ -135,4 +136,3 @@ private fun OperatingSystem.Architecture.Companion.forLowerCaseName(osArch: Stri
"aarch64", "arm-v8", "arm64" -> OperatingSystem.Architecture.Arm64
else -> null
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import io.ktor.client.plugins.logging.Logging
import io.ktor.client.request.prepareGet
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.jvm.javaio.copyTo
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.attribute.FileTime
import java.nio.file.attribute.PosixFilePermission
import java.security.MessageDigest
import java.text.NumberFormat
import kotlinx.coroutines.runBlocking
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
Expand All @@ -47,13 +54,6 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.newInstance
import org.pgpainless.sop.SOPImpl
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.attribute.FileTime
import java.nio.file.attribute.PosixFilePermission
import java.security.MessageDigest
import java.text.NumberFormat

abstract class DownloadNodeJsTask : DefaultTask() {

Expand Down Expand Up @@ -121,7 +121,7 @@ internal fun DownloadOfficialVersion.Companion.describe(source: DownloadOfficial
"null"
} else source.run {
"DownloadNodeJsTask.Source.DownloadOfficialVersion(" +
"version=${version.orNull}, operatingSystem=${operatingSystem.orNull})"
"version=${version.orNull}, operatingSystem=${operatingSystem.orNull})"
}

internal fun Source.Companion.describe(source: Source?): String = when (source) {
Expand Down Expand Up @@ -174,7 +174,7 @@ internal val DownloadOfficialVersion.downloadFileName: String
OperatingSystem.Type.Linux -> Pair("linux", "tar.gz")
else -> throw GradleException(
"unable to determine node.js download URL for operating system type: $type " +
"(operatingSystem=$os) (error code ead53smf45)"
"(operatingSystem=$os) (error code ead53smf45)"
)
}
val osArch = when (os.arch) {
Expand All @@ -189,7 +189,7 @@ internal val DownloadOfficialVersion.downloadFileName: String

private data class DownloadedNodeJsFiles(
val binaryDistribution: File,
val shasums: File,
val shasums: File
)

private fun Task.downloadOfficialVersion(source: DownloadOfficialVersion, outputDirectory: File) {
Expand All @@ -204,8 +204,10 @@ private fun Task.downloadOfficialVersion(source: DownloadOfficialVersion, output
} else if (downloadedFiles.binaryDistribution.name.endsWith(".zip")) {
unzip(downloadedFiles.binaryDistribution, outputDirectory)
} else {
throw GradleException("Unsupported archive: ${downloadedFiles.binaryDistribution.absolutePath} " +
"(only .tar.gz and .zip extensions are supported) (error code pvrvw8sk9t)")
throw GradleException(
"Unsupported archive: ${downloadedFiles.binaryDistribution.absolutePath} " +
"(only .tar.gz and .zip extensions are supported) (error code pvrvw8sk9t)"
)
}
}

Expand Down Expand Up @@ -233,7 +235,12 @@ private fun Task.untar(file: File, destDir: File) {
try {
Files.setLastModifiedTime(outputFile.toPath(), lastModifiedTime)
} catch (e: IOException) {
logger.debug("Ignoring error from Files.setLastModifiedTime({}, {}): {}", outputFile.absolutePath, lastModifiedTime, e.toString())
logger.debug(
"Ignoring error from Files.setLastModifiedTime({}, {}): {}",
outputFile.absolutePath,
lastModifiedTime,
e.toString()
)
}

val newPermissions = buildSet {
Expand All @@ -253,66 +260,92 @@ private fun Task.untar(file: File, destDir: File) {
try {
Files.setPosixFilePermissions(outputFile.toPath(), newPermissions)
} catch (e: UnsupportedOperationException) {
logger.debug("Ignoring error from Files.setPosixFilePermissions({}, {}}): {}", outputFile.absolutePath, newPermissions, e.toString())
logger.debug(
"Ignoring error from Files.setPosixFilePermissions({}, {}}): {}",
outputFile.absolutePath,
newPermissions,
e.toString()
)
}
}
}
}
}
val extractedByteCountStr = NumberFormat.getNumberInstance().format(extractedByteCount)
logger.info("Extracted {} files ({} bytes) from {} to {}", extractedFileCount, extractedByteCountStr, file.absolutePath, destDir.absolutePath)
logger.info(
"Extracted {} files ({} bytes) from {} to {}",
extractedFileCount,
extractedByteCountStr,
file.absolutePath,
destDir.absolutePath
)
}
private fun Task.unzip(file: File, destDir: File) {
logger.info("Extracting {} to {}", file.absolutePath, destDir.absolutePath)
var extractedFileCount = 0
var extractedByteCount = 0L
file.inputStream().use { fileInputStream ->
ZipArchiveInputStream(fileInputStream).use { zipInputStream ->
while (true) {
val zipEntry: ZipArchiveEntry = zipInputStream.nextEntry ?: break
if (zipEntry.isDirectory) {
continue
}
val outputFile = File(destDir, zipEntry.name).absoluteFile
logger.debug("Extracting {}", outputFile.absolutePath)
outputFile.parentFile.mkdirs()
outputFile.outputStream().use { fileOutputStream ->
extractedByteCount += zipInputStream.copyTo(fileOutputStream)
}
extractedFileCount++

val lastModifiedTime = FileTime.from(zipEntry.lastModifiedTime.toInstant())
try {
Files.setLastModifiedTime(outputFile.toPath(), lastModifiedTime)
} catch (e: IOException) {
logger.debug("Ignoring error from Files.setLastModifiedTime({}, {}): {}", outputFile.absolutePath, lastModifiedTime, e.toString())
}
while (true) {
val zipEntry: ZipArchiveEntry = zipInputStream.nextEntry ?: break
if (zipEntry.isDirectory) {
continue
}
val outputFile = File(destDir, zipEntry.name).absoluteFile
logger.debug("Extracting {}", outputFile.absolutePath)
outputFile.parentFile.mkdirs()
outputFile.outputStream().use { fileOutputStream ->
extractedByteCount += zipInputStream.copyTo(fileOutputStream)
}
extractedFileCount++

val lastModifiedTime = FileTime.from(zipEntry.lastModifiedTime.toInstant())
try {
Files.setLastModifiedTime(outputFile.toPath(), lastModifiedTime)
} catch (e: IOException) {
logger.debug(
"Ignoring error from Files.setLastModifiedTime({}, {}): {}",
outputFile.absolutePath,
lastModifiedTime,
e.toString()
)
}

val newPermissions = buildSet {
add(PosixFilePermission.OWNER_READ)
add(PosixFilePermission.OWNER_WRITE)
val newPermissions = buildSet {
add(PosixFilePermission.OWNER_READ)
add(PosixFilePermission.OWNER_WRITE)

add(PosixFilePermission.GROUP_READ)
add(PosixFilePermission.OTHERS_READ)
add(PosixFilePermission.GROUP_READ)
add(PosixFilePermission.OTHERS_READ)

val mode = zipEntry.unixMode
if ((mode and 0x100) == 0x100) {
add(PosixFilePermission.OWNER_EXECUTE)
add(PosixFilePermission.GROUP_EXECUTE)
add(PosixFilePermission.OTHERS_EXECUTE)
}
}
try {
Files.setPosixFilePermissions(outputFile.toPath(), newPermissions)
} catch (e: UnsupportedOperationException) {
logger.debug("Ignoring error from Files.setPosixFilePermissions({}, {}}): {}", outputFile.absolutePath, newPermissions, e.toString())
val mode = zipEntry.unixMode
if ((mode and 0x100) == 0x100) {
add(PosixFilePermission.OWNER_EXECUTE)
add(PosixFilePermission.GROUP_EXECUTE)
add(PosixFilePermission.OTHERS_EXECUTE)
}

}
try {
Files.setPosixFilePermissions(outputFile.toPath(), newPermissions)
} catch (e: UnsupportedOperationException) {
logger.debug(
"Ignoring error from Files.setPosixFilePermissions({}, {}}): {}",
outputFile.absolutePath,
newPermissions,
e.toString()
)
}
}
}
}
val extractedByteCountStr = NumberFormat.getNumberInstance().format(extractedByteCount)
logger.info("Extracted {} files ({} bytes) from {} to {}", extractedFileCount, extractedByteCountStr, file.absolutePath, destDir.absolutePath)
logger.info(
"Extracted {} files ({} bytes) from {} to {}",
extractedFileCount,
extractedByteCountStr,
file.absolutePath,
destDir.absolutePath
)
}

private fun Task.verifySha256Digest(file: File, expectedSha256Digest: String) {
Expand All @@ -333,8 +366,10 @@ private fun Task.verifySha256Digest(file: File, expectedSha256Digest: String) {
if (expectedSha256Digest == actualSha256Digest) {
logger.info("{} had the expected SHA256 digest: {}", file.absolutePath, expectedSha256Digest)
} else {
throw GradleException("Incorrect SHA256 digest of ${file.absolutePath}: " +
"$actualSha256Digest (expected $expectedSha256Digest)")
throw GradleException(
"Incorrect SHA256 digest of ${file.absolutePath}: " +
"$actualSha256Digest (expected $expectedSha256Digest)"
)
}
}

Expand All @@ -357,8 +392,8 @@ private fun Task.getExpectedSha256DigestFromShasumsFile(
}.distinct()

val sha = shas.singleOrNull() ?: throw GradleException(
"$shasumsFilePath defines ${shas.size} SHA256 hashes for "
+ "$desiredFileName, but expected exactly 1"
"$shasumsFilePath defines ${shas.size} SHA256 hashes for " +
"$desiredFileName, but expected exactly 1"
)

logger.info("Found SHA256 sum of {} in {}: {}", desiredFileName, shasumsFilePath, sha)
Expand Down Expand Up @@ -408,7 +443,7 @@ private fun Task.downloadNodeJsBinaryDistribution(

return DownloadedNodeJsFiles(
binaryDistribution = binaryDistributionFile,
shasums = shasumsFile,
shasums = shasumsFile
)
}

Expand All @@ -429,8 +464,8 @@ private suspend fun Task.downloadFile(httpClient: HttpClient, url: String, destF
val maxNumDownloadBytesStr = numberFormat.format(maxNumDownloadBytes)
throw GradleException(
"Downloading $url failed: maximum file size $maxNumDownloadBytesStr bytes exceeded; " +
"cancelled after downloading $actualNumBytesDownloadedStr bytes " +
"(error code hvmhysn5vy)"
"cancelled after downloading $actualNumBytesDownloadedStr bytes " +
"(error code hvmhysn5vy)"
)
}

Expand All @@ -440,7 +475,7 @@ private suspend fun Task.downloadFile(httpClient: HttpClient, url: String, destF
private fun Task.verifyNodeJsShaSumsSignature(file: File): ByteArray {
logger.info(
"Verifying that ${file.absolutePath} has a valid signature " +
"from the node.js release signing keys"
"from the node.js release signing keys"
)

val keysListPath = "com/google/firebase/example/dataconnect/gradle/nodejs_release_signing_keys/keys.list"
Expand Down Expand Up @@ -475,27 +510,3 @@ private fun Task.loadResource(path: String): ByteArray {
}
}
}
























Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ internal fun Task.runCommand(logFile: File, configure: ExecSpec.() -> Unit) {
effectiveLogFile?.let { logger.warn("{}", it.readText()) }
throw exception
}
}
}

0 comments on commit 0c41373

Please sign in to comment.