Skip to content

Commit 0e97740

Browse files
committed
refactor(gradle-plugin): moved import map output directory
`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`.
1 parent 4a7373c commit 0e97740

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

gradle-plugin/src/main/kotlin/io/github/manriif/supabase/functions/Constants.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ internal const val COROUTINES_VERSION_GRADLE_PROPERTY = "supabase.functions.coro
3131
internal const val DENO_KOTLIN_BRIDGE_FUNCTION_NAME = "denoKotlinBridge"
3232
internal const val KOTLIN_MAIN_FUNCTION_NAME = "serve"
3333

34-
internal const val IMPORT_MAPS_DIRECTORY_NAME = "importMaps"
3534
internal const val GITIGNORE_FILE_NAME = ".gitignore"
3635
internal const val IMPORT_MAP_TEMPLATE_FILE_NAME = "import_map_template.json"
3736
internal const val IMPORT_MAP_FILE_NAME = "import_map.json"

gradle-plugin/src/main/kotlin/io/github/manriif/supabase/functions/task/SupabaseFunctionAggregateImportMapTask.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import io.github.manriif.supabase.functions.IMPORT_MAP_TEMPLATE_FILE_NAME
2828
import io.github.manriif.supabase.functions.error.SupabaseFunctionImportMapTemplateException
2929
import io.github.manriif.supabase.functions.supabase.supabaseAllFunctionsDirFile
3030
import org.gradle.api.DefaultTask
31+
import org.gradle.api.file.ConfigurableFileCollection
3132
import org.gradle.api.file.DirectoryProperty
3233
import org.gradle.api.file.RegularFileProperty
3334
import org.gradle.api.tasks.CacheableTask
34-
import org.gradle.api.tasks.IgnoreEmptyDirectories
35-
import org.gradle.api.tasks.InputDirectory
3635
import org.gradle.api.tasks.InputFile
36+
import org.gradle.api.tasks.InputFiles
3737
import org.gradle.api.tasks.Internal
3838
import org.gradle.api.tasks.Optional
3939
import org.gradle.api.tasks.OutputFile
@@ -51,10 +51,9 @@ import java.io.File
5151
@CacheableTask
5252
abstract class SupabaseFunctionAggregateImportMapTask : DefaultTask() {
5353

54-
@get:InputDirectory
55-
@get:IgnoreEmptyDirectories
54+
@get:InputFiles
5655
@get:PathSensitive(PathSensitivity.RELATIVE)
57-
internal abstract val importMapsDir: DirectoryProperty
56+
internal abstract val importMapDirs: ConfigurableFileCollection
5857

5958
@get:Internal
6059
internal abstract val supabaseDir: DirectoryProperty
@@ -100,7 +99,7 @@ abstract class SupabaseFunctionAggregateImportMapTask : DefaultTask() {
10099
val imports = importMap.getAsJsonObject(IMPORT_MAP_JSON_IMPORTS)
101100
val scopes = importMap.getAsJsonObject(IMPORT_MAP_JSON_SCOPES)
102101

103-
importMapsDir.get().asFileTree
102+
importMapDirs.asFileTree
104103
.matching {
105104
include { file ->
106105
!file.isDirectory && file.name.endsWith(".json")

gradle-plugin/src/main/kotlin/io/github/manriif/supabase/functions/task/SupabaseFunctionCopyKotlinTask.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ abstract class SupabaseFunctionCopyKotlinTask : DefaultTask() {
5353
internal abstract val supabaseDir: DirectoryProperty
5454

5555
@get:InputDirectory
56-
@get:Optional
5756
@get:PathSensitive(PathSensitivity.RELATIVE)
5857
internal abstract val compiledSourceDir: DirectoryProperty
5958

gradle-plugin/src/main/kotlin/io/github/manriif/supabase/functions/task/Tasks.kt

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
*/
2222
package io.github.manriif.supabase.functions.task
2323

24-
import io.github.manriif.supabase.functions.IMPORT_MAPS_DIRECTORY_NAME
2524
import io.github.manriif.supabase.functions.IMPORT_MAP_TEMPLATE_FILE_NAME
2625
import io.github.manriif.supabase.functions.KOTLIN_MAIN_FUNCTION_NAME
2726
import io.github.manriif.supabase.functions.REQUEST_CONFIG_FILE_NAME
@@ -99,10 +98,6 @@ private fun Project.registerAggregateImportMapTask(extension: SupabaseFunctionEx
9998

10099
supabaseDir.convention(extension.supabaseDir)
101100

102-
importMapsDir.convention(
103-
layout.buildDirectory.dir("${SUPABASE_FUNCTION_OUTPUT_DIR}/$IMPORT_MAPS_DIRECTORY_NAME")
104-
)
105-
106101
importMapTemplateFile.convention(
107102
extension.supabaseDir.file("functions/$IMPORT_MAP_TEMPLATE_FILE_NAME").orNone()
108103
)
@@ -163,6 +158,19 @@ private fun Project.registerCopyJsTask(
163158
}
164159
}
165160

161+
private fun missingJsCompileTaskError(compileTaskName: String): Nothing {
162+
error(
163+
"""
164+
Task `$compileTaskName` was not found during project sync, common reasons for this error are:
165+
166+
- 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)
167+
- The kotlin multiplatform plugin was not applied on this project
168+
- JS target was not initialized on this project
169+
- JS target is missing `binaries.library()`
170+
""".trimIndent()
171+
)
172+
}
173+
166174
private fun Project.registerCopyKotlinTask(
167175
extension: SupabaseFunctionExtension,
168176
environment: String,
@@ -173,30 +181,27 @@ private fun Project.registerCopyKotlinTask(
173181

174182
tasks.register<SupabaseFunctionCopyKotlinTask>(taskName) {
175183
group = SUPABASE_FUNCTION_TASK_GROUP
176-
description = "Copy Kotlin generated sources into supabase function directory."
177-
178-
compiledSourceDir.convention(
179-
layout.buildDirectory.dir("compileSync/js/main/${environment}Library/kotlin").orNone()
180-
)
184+
description = "Copy Kotlin generated $environment sources into supabase function directory."
181185

182186
supabaseDir.convention(extension.supabaseDir)
183187
functionName.convention(extension.functionName)
184188

185-
if (tasks.names.none { it == compileSyncTaskName }) {
189+
val compileTaskFound = tasks.names.any { it == compileSyncTaskName }
190+
191+
if (compileTaskFound) {
192+
compiledSourceDir.convention(
193+
layout.buildDirectory.dir("compileSync/js/main/${environment}Library/kotlin")
194+
)
195+
196+
dependsOn(compileSyncTaskName)
197+
} else {
198+
compiledSourceDir.convention(provider {
199+
missingJsCompileTaskError(compileSyncTaskName)
200+
})
201+
186202
doFirst {
187-
error(
188-
"""
189-
Task `$compileSyncTaskName` was not found during project sync, common reasons for this error are:
190-
191-
- 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)
192-
- The kotlin multiplatform plugin was not applied on this project
193-
- JS target was not initialized on this project
194-
- JS target is missing `binaries.library()`
195-
""".trimIndent()
196-
)
203+
missingJsCompileTaskError(compileSyncTaskName)
197204
}
198-
} else {
199-
dependsOn(compileSyncTaskName)
200205
}
201206

202207
dependsOn(TASK_COPY_JS)
@@ -267,21 +272,20 @@ private fun Project.registerGenerateImportMapTask(
267272
extension: SupabaseFunctionExtension,
268273
jsDependenciesProvider: Provider<Collection<JsDependency>>
269274
) {
275+
val importMapDir = layout.buildDirectory
276+
.dir("generated/${SUPABASE_FUNCTION_OUTPUT_DIR}/importMap")
277+
270278
val generateTaskProvider = tasks.register<SupabaseFunctionGenerateImportMapTask>(
271279
name = TASK_GENERATE_IMPORT_MAP
272280
) {
273281
group = SUPABASE_FUNCTION_TASK_GROUP
274282
description = "Generate import map."
275283

276-
importMapsDir.convention(
277-
rootProject.layout.buildDirectory
278-
.dir("${SUPABASE_FUNCTION_OUTPUT_DIR}/$IMPORT_MAPS_DIRECTORY_NAME")
279-
)
280-
281284
packageJsonFile.convention(
282285
layout.buildDirectory.file("tmp/jsPublicPackageJson/package.json").orNone()
283286
)
284287

288+
importMapsDir.convention(importMapDir)
285289
functionName.convention(extension.functionName)
286290
jsDependencies.convention(jsDependenciesProvider)
287291

@@ -291,6 +295,7 @@ private fun Project.registerGenerateImportMapTask(
291295
}
292296

293297
aggregateTaskProvider.configure {
298+
importMapDirs.from(importMapDir)
294299
dependsOn(generateTaskProvider)
295300
}
296301
}

0 commit comments

Comments
 (0)