Skip to content

Commit

Permalink
refactor(gradle-plugin): moved import map output directory
Browse files Browse the repository at this point in the history
`import_map.json` files are now generated under the target project build directory instead of the root project build directory.

Only projects registered to the SupabaseFunctionAggregateImportMapTask are now taken into account when merging the final `import_map.json`.
  • Loading branch information
manriif committed Jun 29, 2024
1 parent 4a7373c commit 0e97740
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ internal const val COROUTINES_VERSION_GRADLE_PROPERTY = "supabase.functions.coro
internal const val DENO_KOTLIN_BRIDGE_FUNCTION_NAME = "denoKotlinBridge"
internal const val KOTLIN_MAIN_FUNCTION_NAME = "serve"

internal const val IMPORT_MAPS_DIRECTORY_NAME = "importMaps"
internal const val GITIGNORE_FILE_NAME = ".gitignore"
internal const val IMPORT_MAP_TEMPLATE_FILE_NAME = "import_map_template.json"
internal const val IMPORT_MAP_FILE_NAME = "import_map.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import io.github.manriif.supabase.functions.IMPORT_MAP_TEMPLATE_FILE_NAME
import io.github.manriif.supabase.functions.error.SupabaseFunctionImportMapTemplateException
import io.github.manriif.supabase.functions.supabase.supabaseAllFunctionsDirFile
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
Expand All @@ -51,10 +51,9 @@ import java.io.File
@CacheableTask
abstract class SupabaseFunctionAggregateImportMapTask : DefaultTask() {

@get:InputDirectory
@get:IgnoreEmptyDirectories
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
internal abstract val importMapsDir: DirectoryProperty
internal abstract val importMapDirs: ConfigurableFileCollection

@get:Internal
internal abstract val supabaseDir: DirectoryProperty
Expand Down Expand Up @@ -100,7 +99,7 @@ abstract class SupabaseFunctionAggregateImportMapTask : DefaultTask() {
val imports = importMap.getAsJsonObject(IMPORT_MAP_JSON_IMPORTS)
val scopes = importMap.getAsJsonObject(IMPORT_MAP_JSON_SCOPES)

importMapsDir.get().asFileTree
importMapDirs.asFileTree
.matching {
include { file ->
!file.isDirectory && file.name.endsWith(".json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ abstract class SupabaseFunctionCopyKotlinTask : DefaultTask() {
internal abstract val supabaseDir: DirectoryProperty

@get:InputDirectory
@get:Optional
@get:PathSensitive(PathSensitivity.RELATIVE)
internal abstract val compiledSourceDir: DirectoryProperty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
package io.github.manriif.supabase.functions.task

import io.github.manriif.supabase.functions.IMPORT_MAPS_DIRECTORY_NAME
import io.github.manriif.supabase.functions.IMPORT_MAP_TEMPLATE_FILE_NAME
import io.github.manriif.supabase.functions.KOTLIN_MAIN_FUNCTION_NAME
import io.github.manriif.supabase.functions.REQUEST_CONFIG_FILE_NAME
Expand Down Expand Up @@ -99,10 +98,6 @@ private fun Project.registerAggregateImportMapTask(extension: SupabaseFunctionEx

supabaseDir.convention(extension.supabaseDir)

importMapsDir.convention(
layout.buildDirectory.dir("${SUPABASE_FUNCTION_OUTPUT_DIR}/$IMPORT_MAPS_DIRECTORY_NAME")
)

importMapTemplateFile.convention(
extension.supabaseDir.file("functions/$IMPORT_MAP_TEMPLATE_FILE_NAME").orNone()
)
Expand Down Expand Up @@ -163,6 +158,19 @@ private fun Project.registerCopyJsTask(
}
}

private fun missingJsCompileTaskError(compileTaskName: String): Nothing {
error(
"""
Task `$compileTaskName` was not found during project sync, common reasons for this error are:
- The `$SUPABASE_FUNCTION_PLUGIN_NAME` plugin was applied on a build script where the kotlin multiplatform plugin was not applied (e.g., root build script)
- The kotlin multiplatform plugin was not applied on this project
- JS target was not initialized on this project
- JS target is missing `binaries.library()`
""".trimIndent()
)
}

private fun Project.registerCopyKotlinTask(
extension: SupabaseFunctionExtension,
environment: String,
Expand All @@ -173,30 +181,27 @@ private fun Project.registerCopyKotlinTask(

tasks.register<SupabaseFunctionCopyKotlinTask>(taskName) {
group = SUPABASE_FUNCTION_TASK_GROUP
description = "Copy Kotlin generated sources into supabase function directory."

compiledSourceDir.convention(
layout.buildDirectory.dir("compileSync/js/main/${environment}Library/kotlin").orNone()
)
description = "Copy Kotlin generated $environment sources into supabase function directory."

supabaseDir.convention(extension.supabaseDir)
functionName.convention(extension.functionName)

if (tasks.names.none { it == compileSyncTaskName }) {
val compileTaskFound = tasks.names.any { it == compileSyncTaskName }

if (compileTaskFound) {
compiledSourceDir.convention(
layout.buildDirectory.dir("compileSync/js/main/${environment}Library/kotlin")
)

dependsOn(compileSyncTaskName)
} else {
compiledSourceDir.convention(provider {
missingJsCompileTaskError(compileSyncTaskName)
})

doFirst {
error(
"""
Task `$compileSyncTaskName` was not found during project sync, common reasons for this error are:
- The `$SUPABASE_FUNCTION_PLUGIN_NAME` plugin was applied on a build script where the kotlin multiplatform plugin was not applied (e.g., root build script)
- The kotlin multiplatform plugin was not applied on this project
- JS target was not initialized on this project
- JS target is missing `binaries.library()`
""".trimIndent()
)
missingJsCompileTaskError(compileSyncTaskName)
}
} else {
dependsOn(compileSyncTaskName)
}

dependsOn(TASK_COPY_JS)
Expand Down Expand Up @@ -267,21 +272,20 @@ private fun Project.registerGenerateImportMapTask(
extension: SupabaseFunctionExtension,
jsDependenciesProvider: Provider<Collection<JsDependency>>
) {
val importMapDir = layout.buildDirectory
.dir("generated/${SUPABASE_FUNCTION_OUTPUT_DIR}/importMap")

val generateTaskProvider = tasks.register<SupabaseFunctionGenerateImportMapTask>(
name = TASK_GENERATE_IMPORT_MAP
) {
group = SUPABASE_FUNCTION_TASK_GROUP
description = "Generate import map."

importMapsDir.convention(
rootProject.layout.buildDirectory
.dir("${SUPABASE_FUNCTION_OUTPUT_DIR}/$IMPORT_MAPS_DIRECTORY_NAME")
)

packageJsonFile.convention(
layout.buildDirectory.file("tmp/jsPublicPackageJson/package.json").orNone()
)

importMapsDir.convention(importMapDir)
functionName.convention(extension.functionName)
jsDependencies.convention(jsDependenciesProvider)

Expand All @@ -291,6 +295,7 @@ private fun Project.registerGenerateImportMapTask(
}

aggregateTaskProvider.configure {
importMapDirs.from(importMapDir)
dependsOn(generateTaskProvider)
}
}

0 comments on commit 0e97740

Please sign in to comment.