|
| 1 | +/* |
| 2 | + * Abysner - Dive planner |
| 3 | + * Copyright (C) 2026 Neotech |
| 4 | + * |
| 5 | + * Abysner is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License version 3, |
| 7 | + * as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * You should have received a copy of the GNU Affero General Public License |
| 10 | + * along with this program. If not, see https://www.gnu.org/licenses/. |
| 11 | + */ |
| 12 | + |
| 13 | +package org.neotech.plugin |
| 14 | + |
| 15 | +import org.gradle.api.DefaultTask |
| 16 | +import org.gradle.api.GradleException |
| 17 | +import org.gradle.api.file.DirectoryProperty |
| 18 | +import org.gradle.api.provider.Property |
| 19 | +import org.gradle.api.tasks.Input |
| 20 | +import org.gradle.api.tasks.InputDirectory |
| 21 | +import org.gradle.api.tasks.Optional |
| 22 | +import org.gradle.api.tasks.OutputDirectory |
| 23 | +import org.gradle.api.tasks.TaskAction |
| 24 | + |
| 25 | +/** |
| 26 | + * Builds an iOS .xcarchive using xcodebuild. This task must run on macOS with Xcode installed. |
| 27 | + * Use [IosExportTask] to export the archive as an IPA for App Store upload. |
| 28 | + */ |
| 29 | +abstract class IosArchiveTask : DefaultTask() { |
| 30 | + |
| 31 | + @get:InputDirectory |
| 32 | + abstract val xcodeProjectDirectory: DirectoryProperty |
| 33 | + |
| 34 | + @get:Input |
| 35 | + abstract val scheme: Property<String> |
| 36 | + |
| 37 | + @get:Input |
| 38 | + @get:Optional |
| 39 | + abstract val configuration: Property<String> |
| 40 | + |
| 41 | + @get:InputDirectory |
| 42 | + @get:Optional |
| 43 | + abstract val xcodeAppPath: DirectoryProperty |
| 44 | + |
| 45 | + @get:OutputDirectory |
| 46 | + abstract val outputDirectory: DirectoryProperty |
| 47 | + |
| 48 | + init { |
| 49 | + group = "release" |
| 50 | + description = "Archives the iOS app into an .xcarchive bundle." |
| 51 | + } |
| 52 | + |
| 53 | + @TaskAction |
| 54 | + fun execute() { |
| 55 | + val projectDirectory = xcodeProjectDirectory.get().asFile |
| 56 | + val schemeName = scheme.get() |
| 57 | + val config = configuration.getOrElse("Release") |
| 58 | + val outputDir = outputDirectory.get().asFile |
| 59 | + val archivePath = outputDir.resolve("$schemeName.xcarchive") |
| 60 | + |
| 61 | + outputDir.mkdirs() |
| 62 | + |
| 63 | + logger.lifecycle("Archiving iOS app (scheme=$schemeName, configuration=$config)...") |
| 64 | + |
| 65 | + val archiveResult = runCommand( |
| 66 | + listOf( |
| 67 | + "xcodebuild", |
| 68 | + "-project", projectDirectory.resolve("iosApp.xcodeproj").absolutePath, |
| 69 | + "-scheme", schemeName, |
| 70 | + "-configuration", config, |
| 71 | + "-archivePath", archivePath.absolutePath, |
| 72 | + "-destination", "generic/platform=iOS", |
| 73 | + "archive", |
| 74 | + ), |
| 75 | + workingDirectory = projectDirectory, |
| 76 | + ) |
| 77 | + |
| 78 | + if (archiveResult != 0) { |
| 79 | + throw GradleException("xcodebuild archive failed with exit code $archiveResult") |
| 80 | + } |
| 81 | + |
| 82 | + logger.lifecycle("Archive created at: ${archivePath.absolutePath}") |
| 83 | + } |
| 84 | + |
| 85 | + private fun runCommand(command: List<String>, workingDirectory: java.io.File): Int { |
| 86 | + logger.info("Running: ${command.joinToString(" ")}") |
| 87 | + val process = ProcessBuilder(command) |
| 88 | + .directory(workingDirectory) |
| 89 | + .redirectErrorStream(true) |
| 90 | + .also { builder -> |
| 91 | + if (xcodeAppPath.isPresent) { |
| 92 | + val developerDir = xcodeAppPath.get().asFile.resolve("Contents/Developer") |
| 93 | + logger.info("Using Xcode at: ${xcodeAppPath.get().asFile.absolutePath}") |
| 94 | + builder.environment()["DEVELOPER_DIR"] = developerDir.absolutePath |
| 95 | + } |
| 96 | + } |
| 97 | + .start() |
| 98 | + |
| 99 | + process.inputStream.bufferedReader().forEachLine { line -> |
| 100 | + logger.info(" $line") |
| 101 | + } |
| 102 | + |
| 103 | + return process.waitFor() |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments