Skip to content

Commit

Permalink
Refactor tasks for better readability.
Browse files Browse the repository at this point in the history
Add test coverage (wip).
  • Loading branch information
buijs-dev committed Aug 25, 2022
1 parent 4867734 commit 3a9a2a3
Show file tree
Hide file tree
Showing 18 changed files with 1,333 additions and 364 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Dec 09 14:59:52 CET 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,104 @@ package dev.buijs.klutter.tasks

import dev.buijs.klutter.kore.KlutterException
import dev.buijs.klutter.kore.KlutterTask
import dev.buijs.klutter.kore.project.Project
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

/**
* Task to build a Klutter plugin project.
*
* Executes the following steps:
* - clean platform module
* - build platform module
* - create XCFramework
* - klutterCopyAarFile
* - klutterCopyFramework
*
*/
class BuildKlutterPluginProjectTask(
private val project: Project,
private val executor: CliExecutor = CliExecutor(),
) : KlutterTask {

override fun run() {
executor.execute(
command = """./gradlew clean build assemblePlatformReleaseXCFramework -p platform""",
runFrom = project.root.folder,
)

executor.execute(
command = """./gradlew klutterCopyAarFile""",
runFrom = project.root.folder,
timeout = 30
)

executor.execute(
command = """./gradlew klutterCopyFramework""",
runFrom = project.root.folder,
timeout = 30
)

}

}

/**
* Task to build debug .apk for Android and Runner.app for IOS.
*/
sealed class ArtifactBuildTask(
class BuildAndroidAndIosWithFlutterTask(
private val pathToFlutterApp: File,
private val pathToTestFolder: File,
) : KlutterTask {
override fun run() {
buildAndroid(pathToTestFolder, pathToFlutterApp)
buildIos(pathToTestFolder, pathToFlutterApp)
}
}

/**
* Task to build debug .apk Android artifact.
*/
class BuildAndroidWithFlutterTask(
private val pathToTestFolder: File,
private val pathToFlutterApp: File,
) : KlutterTask {
override fun run() = buildAndroid(pathToTestFolder, pathToFlutterApp)
}

/**
* Task to build Runner.app IOS artifact.
*/
class BuildIosWithFlutterTask(
private val pathToTestFolder: File,
private val pathToFlutterApp: File,
) : KlutterTask {
override fun run() = buildIos(pathToTestFolder, pathToFlutterApp)
}

private fun buildIos(
pathToTestFolder: File,
pathToToFlutterApp: File,
) = IosArtifactBuildTask(
pathToFlutterApp = pathToToFlutterApp,
pathToOutput = pathToTestFolder.resolve("src/test/resources"),
).run()

private fun buildAndroid(
pathToTestFolder: File,
pathToToFlutterApp: File,
) = AndroidArtifactBuildTask(
pathToFlutterApp = pathToToFlutterApp,
pathToOutput = pathToTestFolder.resolve("src/test/resources"),
).run()

/**
*
*/
private sealed class ArtifactBuildTask(

/**
* Path to the Flutter frontend folder.
Expand All @@ -46,14 +134,14 @@ sealed class ArtifactBuildTask(

) : KlutterTask {

internal fun pathToFlutterApp(): File {
fun pathToFlutterApp(): File {
if (!pathToFlutterApp.exists()) {
throw KlutterException("Missing directory: $pathToFlutterApp.")
}
return pathToFlutterApp
}

internal fun pathToOutputFolder(): File {
fun pathToOutputFolder(): File {
if (!pathToOutput.exists()) {
throw KlutterException("Missing output directory: $pathToOutput.")
}
Expand All @@ -62,16 +150,20 @@ sealed class ArtifactBuildTask(
}

/**
*
* Task to build debug app with flutter.
*/
class AndroidArtifactBuildTask(
private class AndroidArtifactBuildTask(
pathToFlutterApp: File,
pathToOutput: File,
private val executor: CliExecutor = CliExecutor(),
): ArtifactBuildTask(pathToFlutterApp, pathToOutput) {

override fun run() {
// Build the artifact using Flutter.
"""flutter build apk --debug""".execute(pathToFlutterApp())
executor.execute(
command = """flutter build apk --debug""",
runFrom = pathToFlutterApp(),
)

// Check if artifact exists and fail if not.
val artifact = pathToFlutterApp().resolve("build/app/outputs/flutter-apk/app-debug.apk").also {
Expand All @@ -88,16 +180,20 @@ class AndroidArtifactBuildTask(
}

/**
*
* Task to build Runner.app with Flutter.
*/
class IosArtifactBuildTask(
private class IosArtifactBuildTask(
pathToFlutterApp: File,
pathToOutput: File,
private val executor: CliExecutor = CliExecutor(),
): ArtifactBuildTask(pathToFlutterApp, pathToOutput) {

override fun run() {
// Build the artifact using Flutter.
"""flutter build ios --no-codesign --debug""".execute(pathToFlutterApp())
executor.execute(
command = """flutter build ios --no-codesign --debug""",
runFrom = pathToFlutterApp(),
)

// Check if artifact exists and fail if not.
val artifact = pathToFlutterApp().resolve("build/ios/iphonesimulator/Runner.app").also {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.buijs.klutter.tasks

import dev.buijs.klutter.kore.KlutterException
import java.io.File
import java.util.concurrent.TimeUnit

/**
* Execute a CLI command.
*
*/
open class CliExecutor {

/**
* Execute a CLI command.
*/
fun execute(
/**
* Folder from where to execute this command.
*/
runFrom: File,

/**
* Maximum time in seconds to wait for the command to be executed.
*/
timeout: Long? = null,

/**
* The command to be executed.
*/
command: String,
): String = command.execute(
runFrom = runFrom,
timeout = timeout,
)

open fun String.execute(
/**
* Folder from where to execute this command.
*/
runFrom: File,

/**
* Maximum time in seconds to wait for the command to be executed.
*/
timeout: Long? = null,
): String {

val process = ProcessBuilder()
.command(split(" "))
.directory(runFrom)
.start()

if(timeout == null) {
process.waitFor()
} else {
process.waitFor(timeout, TimeUnit.SECONDS)
}

if(process.exitValue() != 0) {
throw KlutterException(
"Failed to execute command: \n${
process.errorStream.reader().readText()
}"
)
}

return process.inputStream.readBytes().decodeToString()

}

}
Loading

0 comments on commit 3a9a2a3

Please sign in to comment.