@@ -2,6 +2,7 @@ import com.android.build.api.variant.impl.VariantOutputImpl
22import com.codingfeline.buildkonfig.compiler.FieldSpec
33import io.github.composefluent.plugin.build.BuildConfig
44import io.github.composefluent.plugin.build.applyTargets
5+ import org.gradle.kotlin.dsl.support.uppercaseFirstChar
56import org.jetbrains.compose.desktop.application.dsl.AbstractDistributions
67import org.jetbrains.compose.desktop.application.dsl.AbstractMacOSPlatformSettings
78import org.jetbrains.compose.desktop.application.dsl.TargetFormat
@@ -235,4 +236,38 @@ fun AbstractDistributions.applyDistributions(
235236
236237fun AbstractMacOSPlatformSettings.applyCommonSetup () {
237238 iconFile.set(project.file(" icons/icon.icns" ))
239+ }
240+
241+ tasks.register(" desktopNativeRun" ) {
242+ group = " run"
243+ dependsOn(tasks.named(" runDebugExecutable${getTarget().uppercaseFirstChar()} " ))
244+ }
245+
246+ listOf (" Release" , " Debug" ).forEach { buildType ->
247+ listOf (" createDistributable" , " packageDistribution" ).forEach { name ->
248+ tasks.register(" ${name} Native${buildType.uppercaseFirstChar()} ForCurrentOS" ) {
249+ group = " compose desktop (native)"
250+ val target = getTarget()
251+ val taskName = if (name == " packageDistribution" ) {
252+ val targetFormat = compose.desktop.nativeApplication.distributions.targetFormats.first { it.isCompatibleWithCurrentOS }
253+ " package${targetFormat.name} Native${buildType}${target.uppercaseFirstChar()} "
254+ } else {
255+ " ${name} Native${buildType}${target.uppercaseFirstChar()} "
256+ }
257+ dependsOn(tasks.named(taskName))
258+ }
259+ }
260+ }
261+
262+ fun getTarget (): String {
263+ // 1. 动态确定当前平台对应的 Kotlin/Native 目标名称
264+ val os = System .getProperty(" os.name" )
265+ val arch = System .getProperty(" os.arch" )
266+ val currentTargetName = when {
267+ os.startsWith(" Mac OS X" ) -> if (arch == " aarch64" ) " macosArm64" else " macosX64"
268+ os.startsWith(" Windows" ) -> " windowsX64"
269+ os.startsWith(" Linux" ) -> " linuxX64"
270+ else -> throw GradleException (" Unsupported OS for native distribution: '$os '" )
271+ }
272+ return currentTargetName
238273}
0 commit comments