Skip to content

Commit

Permalink
ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Nov 29, 2024
1 parent 68bffd3 commit ba411ed
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ import com.google.firebase.example.dataconnect.gradle.tasks.DownloadNodeJsBinary
import com.google.firebase.example.dataconnect.gradle.tasks.ExtractArchiveTask
import com.google.firebase.example.dataconnect.gradle.tasks.GenerateDataConnectSourcesTask
import com.google.firebase.example.dataconnect.gradle.tasks.SetupFirebaseToolsTask
import java.util.Locale
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
import java.util.Locale

@Suppress("unused")
public abstract class DataConnectGradlePlugin : Plugin<Project> {

override fun apply(project: Project) {
val downloadNodeJsArchiveTask = project.tasks.register<DownloadNodeJsBinaryDistributionArchiveTask>("dataConnectDownloadNodeJs")
val downloadNodeJsArchiveTask = project.tasks.register<DownloadNodeJsBinaryDistributionArchiveTask>(
"dataConnectDownloadNodeJs"
)
val extractNodeJsArchiveTask = project.tasks.register<ExtractArchiveTask>("dataConnectExtractNodeJs")
val setupFirebaseToolsTask = project.tasks.register<SetupFirebaseToolsTask>("dataConnectSetupFirebaseTools")

Expand All @@ -42,9 +44,8 @@ public abstract class DataConnectGradlePlugin : Plugin<Project> {
extractNodeJsArchiveTask = extractNodeJsArchiveTask,
setupFirebaseToolsTask = setupFirebaseToolsTask,
buildDirectory = project.layout.buildDirectory.dir("dataConnect"),
projectLayout = project.layout,
providerFactory = project.providers,
logger = project.logger
projectDirectory = project.layout.projectDirectory,
providerFactory = project.providers
)

configurer.invoke()
Expand All @@ -62,5 +63,4 @@ public abstract class DataConnectGradlePlugin : Plugin<Project> {
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import com.google.firebase.example.dataconnect.gradle.util.nodeJsPaths
import com.google.firebase.example.dataconnect.gradle.util.operatingSystem
import org.gradle.api.GradleException
import org.gradle.api.file.Directory
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFile
import org.gradle.api.logging.Logger
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.TaskProvider
Expand All @@ -23,8 +21,8 @@ internal class DataConnectTasksConfigurer(
private val extractNodeJsArchiveTask: TaskProvider<ExtractArchiveTask>,
private val setupFirebaseToolsTask: TaskProvider<SetupFirebaseToolsTask>,
private val buildDirectory: Provider<Directory>,
projectLayout: ProjectLayout,
providerFactory: ProviderFactory,
projectDirectory: Directory,
providerFactory: ProviderFactory
) : () -> Unit {

override fun invoke() {
Expand All @@ -38,24 +36,25 @@ internal class DataConnectTasksConfigurer(
dataConnectExtension.nodeJsVersion
?: throw GradleException(
"dataconnect.nodeJsVersion must be set in " +
"build.gradle or build.gradle.kts to " +
"specify the version of Node.js (https://nodejs.org) " +
"to install (e.g. \"20.9.0\") (error code 3acj27az2c)"
"build.gradle or build.gradle.kts to " +
"specify the version of Node.js (https://nodejs.org) " +
"to install (e.g. \"20.9.0\") (error code 3acj27az2c)"
)
}

private val operatingSystem: Provider<OperatingSystem> = providerFactory.operatingSystem()

private val nodeJsPaths: Provider<NodeJsPaths> = providerFactory.nodeJsPaths(nodeJsVersion, operatingSystem)
private val nodeJsPaths: Provider<NodeJsPaths> =
providerFactory.nodeJsPaths(nodeJsVersion, operatingSystem)

private val firebaseCliVersion: Provider<String> = providerFactory.provider {
dataConnectExtension.firebaseCliVersion
?: throw GradleException(
"dataconnect.firebaseCliVersion must be set in " +
"build.gradle or build.gradle.kts to " +
"specify the version of the Firebase CLI npm package " +
"(https://www.npmjs.com/package/firebase-tools) to use " +
"(e.g. \"13.25.0\") (error code xbmvkc3mtr)"
"build.gradle or build.gradle.kts to " +
"specify the version of the Firebase CLI npm package " +
"(https://www.npmjs.com/package/firebase-tools) to use " +
"(e.g. \"13.25.0\") (error code xbmvkc3mtr)"
)
}

Expand All @@ -73,7 +72,7 @@ internal class DataConnectTasksConfigurer(

private val dataConnectConfigDir: Provider<Directory> = providerFactory.provider {
dataConnectExtension.dataConnectConfigDir?.let {
projectLayout.projectDirectory.dir(it.path)
projectDirectory.dir(it.path)
}
}

Expand All @@ -83,13 +82,17 @@ internal class DataConnectTasksConfigurer(
archiveUrl.set(nodeJsPaths.map { it.archiveUrl })
shasumsUrl.set(nodeJsPaths.map { it.shasumsUrl })

archiveFile.set(providerFactory.zip(buildDirectory, nodeJsPaths) { buildDirectory, nodeJsPaths ->
buildDirectory.file(nodeJsPaths.archiveFileName)
})

shasumsFile.set(providerFactory.zip(buildDirectory, nodeJsPaths) { buildDirectory, nodeJsPaths ->
buildDirectory.file(nodeJsPaths.shasumsFileName)
})
archiveFile.set(
providerFactory.zip(buildDirectory, nodeJsPaths) { buildDirectory, nodeJsPaths ->
buildDirectory.file(nodeJsPaths.archiveFileName)
}
)

shasumsFile.set(
providerFactory.zip(buildDirectory, nodeJsPaths) { buildDirectory, nodeJsPaths ->
buildDirectory.file(nodeJsPaths.shasumsFileName)
}
)
}

private fun configureExtractNodeJsArchiveTask() = extractNodeJsArchiveTask.configure {
Expand All @@ -98,9 +101,11 @@ internal class DataConnectTasksConfigurer(
pathPrefixComponentStripCount.set(1)
archiveFile.set(downloadNodeJsArchiveTask.flatMap { it.archiveFile })

outputDirectory.set(providerFactory.zip(buildDirectory, nodeJsPaths) { buildDirectory, nodeJsPaths ->
buildDirectory.dir(nodeJsPaths.archiveBaseFileName)
})
outputDirectory.set(
providerFactory.zip(buildDirectory, nodeJsPaths) { buildDirectory, nodeJsPaths ->
buildDirectory.dir(nodeJsPaths.archiveBaseFileName)
}
)
}

private fun configureSetupFirebaseToolsTask() = setupFirebaseToolsTask.configure {
Expand All @@ -113,27 +118,28 @@ internal class DataConnectTasksConfigurer(
npmExecutable.set(this@DataConnectTasksConfigurer.npmExecutable)
}

fun configureGenerateDataConnectSourcesTask(task: TaskProvider<GenerateDataConnectSourcesTask>, variantName: String) = task.configure {
fun configureGenerateDataConnectSourcesTask(
task: TaskProvider<GenerateDataConnectSourcesTask>,
variantName: String
) = task.configure {
group = TASK_GROUP

@Suppress("ktlint:standard:max-line-length")
setOnlyIf(
"dataconnect.dataConnectConfigDir is null; to enable the \"$name\" task, " +
"set dataconnect.dataConnectConfigDir in build.gradle or build.gradle.kts to " +
"the directory that defines the Data Connect schema and " +
"connectors whose Kotlin code to generate code. That is, the directory " +
"containing the dataconnect.yaml file. For details, see " +
"https://firebase.google.com/docs/data-connect/configuration-reference#dataconnect.yaml-configuration " +
"(e.g. file(\"../dataconnect\")) (message code a3ch245mbd)"
"set dataconnect.dataConnectConfigDir in build.gradle or build.gradle.kts to " +
"the directory that defines the Data Connect schema and " +
"connectors whose Kotlin code to generate code. That is, the directory " +
"containing the dataconnect.yaml file. For details, see " +
"https://firebase.google.com/docs/data-connect/configuration-reference#dataconnect.yaml-configuration " +
"(e.g. file(\"../dataconnect\")) (message code a3ch245mbd)"
) { dataConnectConfigDir.isPresent }

dataConnectConfigDir.set(this@DataConnectTasksConfigurer.dataConnectConfigDir)
firebaseExecutable.set(setupFirebaseToolsTask.map { it.firebaseExecutable })
nodeExecutable.set(this@DataConnectTasksConfigurer.nodeExecutable)
tweakedDataConnectConfigDir.set(buildDirectory.map { it.dir("variants/$variantName/config") })
}

}

private const val TASK_GROUP = "Firebase Data Connect"

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package com.google.firebase.example.dataconnect.gradle.tasks
import com.google.firebase.example.dataconnect.gradle.DataConnectGradleException
import com.google.firebase.example.dataconnect.gradle.tasks.DataConnectTaskBase.Worker
import com.google.firebase.example.dataconnect.gradle.util.DataConnectGradleLogger
import kotlin.reflect.full.allSupertypes
import org.gradle.api.DefaultTask
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.nio.file.Files
import java.util.Date
import kotlin.reflect.full.allSupertypes
import kotlin.time.DurationUnit
import kotlin.time.toDuration
import org.gradle.api.DefaultTask
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction

public abstract class DataConnectTaskBase(loggerIdPrefix: String) : DefaultTask() {

Expand Down Expand Up @@ -56,8 +56,8 @@ public abstract class DataConnectTaskBase(loggerIdPrefix: String) : DefaultTask(
val elapsedTime = endTime - startTime
dataConnectLogger.info {
"Task $path completed execution in " +
elapsedTime.toString(DurationUnit.SECONDS, 2) +
" at ${Date()}"
elapsedTime.toString(DurationUnit.SECONDS, 2) +
" at ${Date()}"
}

result.onFailure {
Expand All @@ -84,7 +84,7 @@ internal fun Worker.deleteDirectory(dir: File, fileSystemOperations: FileSystemO
result.onFailure {
throw DataConnectGradleException(
"unable to delete directory: ${dir.absolutePath}: $it " +
"(error code 6trngh6x47)",
"(error code 6trngh6x47)",
it
)
}
Expand All @@ -97,7 +97,7 @@ internal fun Worker.deleteFile(file: File) {
result.onFailure {
throw DataConnectGradleException(
"unable to delete file: ${file.absolutePath}: $it " +
"(error code rprr987jqk)",
"(error code rprr987jqk)",
it
)
}
Expand All @@ -110,7 +110,8 @@ internal fun Worker.createDirectory(dir: File) {
result.onFailure {
throw DataConnectGradleException(
"unable to create directory: ${dir.absolutePath}: $it " +
"(error code j7x4sw7w95)", it
"(error code j7x4sw7w95)",
it
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.google.firebase.example.dataconnect.gradle.util.FileDownloader
import com.google.firebase.example.dataconnect.gradle.util.Sha256SignatureVerifier
import com.google.firebase.example.dataconnect.gradle.util.addCertificatesFromKeyListResource
import com.google.firebase.example.dataconnect.gradle.util.addHashesFromShasumsFile
import java.io.File
import javax.inject.Inject
import kotlinx.coroutines.runBlocking
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.RegularFileProperty
Expand All @@ -31,8 +33,6 @@ import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import java.io.File
import javax.inject.Inject

@CacheableTask
public abstract class DownloadNodeJsBinaryDistributionArchiveTask : DataConnectTaskBase(LOGGER_ID_PREFIX) {
Expand Down Expand Up @@ -99,7 +99,6 @@ public abstract class DownloadNodeJsBinaryDistributionArchiveTask : DataConnectT
val fileDownloader: FileDownloader
}


private companion object {
const val LOGGER_ID_PREFIX = "dnb"
}
Expand Down Expand Up @@ -138,10 +137,20 @@ private fun Worker.run() {
fileDownloader.download(shasumsUrl, shasumsFile, maxNumDownloadBytes = 100_000)
}

verifyNodeJsReleaseSignature(file =archiveFile, shasumsFile =shasumsFile, keyListResourcePath="com/google/firebase/example/dataconnect/gradle/nodejs_release_signing_keys/keys.list", logger)
verifyNodeJsReleaseSignature(
file = archiveFile,
shasumsFile = shasumsFile,
keyListResourcePath = "com/google/firebase/example/dataconnect/gradle/nodejs_release_signing_keys/keys.list",
logger
)
}

private fun verifyNodeJsReleaseSignature(file: File, shasumsFile: File, keyListResourcePath: String, logger: DataConnectGradleLogger) {
private fun verifyNodeJsReleaseSignature(
file: File,
shasumsFile: File,
keyListResourcePath: String,
logger: DataConnectGradleLogger
) {
val signatureVerifier = Sha256SignatureVerifier()
logger.info {
"Loading Node.js release signing certificates " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import com.google.firebase.example.dataconnect.gradle.util.DataConnectGradleLogg
import com.google.firebase.example.dataconnect.gradle.util.ExtractArchiveCallbacks
import com.google.firebase.example.dataconnect.gradle.util.extractArchive
import com.google.firebase.example.dataconnect.gradle.util.toFormattedString
import java.io.File
import java.util.concurrent.atomic.AtomicLong
import javax.inject.Inject
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.RegularFileProperty
Expand All @@ -33,9 +36,6 @@ import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.work.DisableCachingByDefault
import java.io.File
import java.util.concurrent.atomic.AtomicLong
import javax.inject.Inject

@DisableCachingByDefault(because = "extracting an archive is a quick operation not worth caching")
public abstract class ExtractArchiveTask : DataConnectTaskBase(LOGGER_ID_PREFIX) {
Expand Down Expand Up @@ -89,14 +89,14 @@ public abstract class ExtractArchiveTask : DataConnectTaskBase(LOGGER_ID_PREFIX)
internal abstract val providerFactory: ProviderFactory

override fun newWorker(): DataConnectTaskBase.Worker = ExtractArchiveTaskWorkerImpl(
archiveFile = archiveFile.get().asFile,
outputDirectory = outputDirectory.get().asFile,
prefixStripCount = pathPrefixComponentStripCount.orNull ?: 0,
fileSystemOperations = fileSystemOperations,
logger = dataConnectLogger
)

internal interface Worker: DataConnectTaskBase.Worker {
archiveFile = archiveFile.get().asFile,
outputDirectory = outputDirectory.get().asFile,
prefixStripCount = pathPrefixComponentStripCount.orNull ?: 0,
fileSystemOperations = fileSystemOperations,
logger = dataConnectLogger
)

internal interface Worker : DataConnectTaskBase.Worker {
val archiveFile: File
val outputDirectory: File
val prefixStripCount: Int
Expand All @@ -106,7 +106,6 @@ public abstract class ExtractArchiveTask : DataConnectTaskBase(LOGGER_ID_PREFIX)
private companion object {
const val LOGGER_ID_PREFIX = "ear"
}

}

private class ExtractArchiveTaskWorkerImpl(
Expand All @@ -127,8 +126,10 @@ private fun Worker.run() {
logger.info { "prefixStripCount: $prefixStripCount" }

if (prefixStripCount < 0) {
throw IllegalArgumentException("invalid prefixStripCount: $prefixStripCount " +
"(must be greater than or equal to zero) (error code mn8pp2b7mc)")
throw IllegalArgumentException(
"invalid prefixStripCount: $prefixStripCount " +
"(must be greater than or equal to zero) (error code mn8pp2b7mc)"
)
}

deleteDirectory(outputDirectory, fileSystemOperations)
Expand All @@ -146,8 +147,8 @@ private fun Worker.run() {
val symlinkCountStr = extractCallbacks.extractedSymlinkCount.toFormattedString()
val byteCountStr = extractCallbacks.extractedByteCount.toFormattedString()
"Extracted $fileCountStr files ($byteCountStr bytes) " +
"and $symlinkCountStr symlinks " +
"from ${archiveFile.absolutePath} to ${outputDirectory.absolutePath}"
"and $symlinkCountStr symlinks " +
"from ${archiveFile.absolutePath} to ${outputDirectory.absolutePath}"
}
}

Expand Down Expand Up @@ -189,5 +190,4 @@ private class ExtractArchiveCallbacksImpl(private val file: File, private val lo
"Ignoring failure to set ${metadataType.name} " + "on extracted file: ${file.absolutePath}"
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.google.firebase.example.dataconnect.gradle.tasks

import com.google.firebase.example.dataconnect.gradle.util.runCommand
import java.io.File
import javax.inject.Inject
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.DirectoryProperty
Expand All @@ -32,8 +34,6 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecOperations
import org.yaml.snakeyaml.Yaml
import java.io.File
import javax.inject.Inject

@CacheableTask
public abstract class GenerateDataConnectSourcesTask : DefaultTask() {
Expand Down
Loading

0 comments on commit ba411ed

Please sign in to comment.