Skip to content

Commit

Permalink
KTOR-3420 Fix compilation and tests (#2)
Browse files Browse the repository at this point in the history
* KTOR-3420 Fix compilation and tests

* KTOR-3922 Add support for macOs M1 target in CLI generator

* KTOR-3929 Support colored logs for errors and success msgs

* Rename run to start

* Fix test
  • Loading branch information
Ololoshechkin authored Jun 2, 2022
1 parent 0969e9f commit bee37c6
Show file tree
Hide file tree
Showing 24 changed files with 196 additions and 55 deletions.
8 changes: 5 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet

val ktor_version: String by project
val kotlinx_cli_version: String by project
val mordant_version: String by project

plugins {
kotlin("multiplatform") version "1.5.31"
Expand All @@ -26,12 +25,13 @@ kotlin {
compilations["main"].enableEndorsedLibs = true
}
mingwX64 {
compilations["main"].enableEndorsedLibs = true

binaries {
executable {
entryPoint = "main"
}
}
compilations["main"].enableEndorsedLibs = true
}
macosX64("macosX64") {
binaries {
Expand All @@ -41,6 +41,7 @@ kotlin {
}
compilations["main"].enableEndorsedLibs = true
}

sourceSets {
val nativeMain by creating {
dependencies {
Expand All @@ -50,6 +51,7 @@ kotlin {
implementation("com.squareup.okio:okio:3.0.0")
implementation("io.ktor:ktor-client-curl:$ktor_version")
implementation("org.jetbrains.kotlinx:kotlinx-cli:$kotlinx_cli_version")
implementation("com.github.ajalt.mordant:mordant:$mordant_version")
}
}
val nativeTest by creating {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
ktor_version=1.6.4
kotlinx_cli_version=0.3.3
mordant_version=2.0.0-beta4
kotlin.mpp.enableCInteropCommonization=true
kotlin.internal.mpp.hierarchicalStructureByDefault=true
10 changes: 8 additions & 2 deletions src/linuxX64Main/kotlin/io/ktor/generator/cli/installer/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ actual val jdkArchiveName: String = "openjdk-11.tar.gz"
actual fun unpackJdk(archive: File, outputDir: Directory) {
val tempPath = Directory.current().path
runProcess("tar -xvf ${archive.path} -C $tempPath")
FileSystem.SYSTEM.atomicMove("$tempPath/jdk-11.jdk".toPath(), outputDir.path.toPath())
FileSystem.SYSTEM.atomicMove("$tempPath/jdk-11".toPath(), outputDir.path.toPath())
}

actual fun isGradleWrapper(file: File): Boolean = file.name.contains("gradlew")
actual fun isGradleWrapper(file: File): Boolean = file.name.contains("gradlew")

actual fun setEnv(varName: String, value: String) {
platform.posix.setenv(varName, value, 1)
}

actual fun getJdkContentsHome(directory: Directory?): Directory? = directory
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.generator.cli.installer.*
import kotlinx.cinterop.pointed
import kotlinx.cinterop.toKString
import kotlinx.cinterop.*
import platform.posix.getcwd
import platform.posix.getpwuid
import platform.posix.getuid
import platform.posix.realpath
import kotlin.text.*
import kotlinx.cinterop.allocArray

actual val FS_DELIMETER: String = "/"

Expand All @@ -21,4 +23,10 @@ actual fun homePath(): String =

actual fun addExecutablePermissions(file: File) {
runProcess("chmod +x ${file.path}")
}
}

actual fun realPath(path: String, buffer: CPointer<ByteVar>): String? {
return realpath(path, buffer)?.toKString()
}

actual fun getCwd(buffer: CPointer<ByteVar>, size: Int) = getcwd(buffer, size.toULong())
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.ktor.generator.cli.utils

import platform.posix.pclose
import platform.posix.popen
import kotlinx.cinterop.CPointer
import platform.posix.FILE

actual fun openPipe(command: String, access: String): CPointer<FILE>? = popen(command, access)
actual fun closePipe(filePtr: CPointer<FILE>): Int = pclose(filePtr)
40 changes: 38 additions & 2 deletions src/macosX64Main/kotlin/io/ktor/generator/cli/installer/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package io.ktor.generator.cli.installer

import io.ktor.generator.cli.installer.Architecture.*
import io.ktor.generator.cli.utils.*
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.toKString
import okio.FileSystem
import okio.Path.Companion.toPath
import platform.posix.uname
import platform.posix.utsname

actual val rootKtorDirName: String = ".ktor"
actual val jdkDownloadUrl: String = "https://download.java.net/java/ga/jdk11/openjdk-11_osx-x64_bin.tar.gz"
actual val jdkDownloadUrl: String
get() = when (getArchitecture()) {
X86_64 -> "https://download.java.net/java/ga/jdk11/openjdk-11_osx-x64_bin.tar.gz"
ARM_64 -> "https://cdn.azul.com/zulu/bin/zulu11.54.25-ca-jdk11.0.14.1-macosx_aarch64.tar.gz"
}
actual val jdkArchiveName: String = "openjdk-11.tar.gz"

actual fun unpackJdk(archive: File, outputDir: Directory) {
Expand All @@ -14,4 +25,29 @@ actual fun unpackJdk(archive: File, outputDir: Directory) {
FileSystem.SYSTEM.atomicMove("$tempPath/jdk-11.jdk".toPath(), outputDir.path.toPath())
}

actual fun isGradleWrapper(file: File): Boolean = file.name.contains("gradlew")
actual fun isGradleWrapper(file: File): Boolean = file.name.contains("gradlew")

actual fun setEnv(varName: String, value: String) {
platform.posix.setenv(varName, value, 1)
}

actual fun getJdkContentsHome(directory: Directory?): Directory? =
directory
?.subdir(KtorInstaller.JAVA_CONTENTS)
?.subdir(KtorInstaller.JAVA_CONTENTS_HOME)

private enum class Architecture {
X86_64, ARM_64
}

private fun getArchitecture(): Architecture {
val architectureName = memScoped {
val systemInfo = alloc<utsname>()
uname(systemInfo.ptr)
systemInfo.machine.toKString()
}
return when (architectureName) {
"x86_64" -> X86_64
else -> ARM_64
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.ktor.generator.cli.utils

import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.pointed
import kotlinx.cinterop.toKString
import platform.posix.getpwuid
import platform.posix.getuid
import platform.posix.realpath
import kotlinx.cinterop.allocArray
import platform.posix.getcwd

actual val FS_DELIMETER: String = "/"

Expand All @@ -16,4 +21,10 @@ actual fun homePath(): String =

actual fun addExecutablePermissions(file: File) {
runProcess("chmod +x ${file.path}")
}
}

actual fun realPath(path: String, buffer: CPointer<ByteVar>): String? {
return realpath(path, buffer)?.toKString()
}

actual fun getCwd(buffer: CPointer<ByteVar>, size: Int) = getcwd(buffer, size.toULong())
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.ktor.generator.cli.utils

import platform.posix.pclose
import platform.posix.popen
import kotlinx.cinterop.CPointer
import platform.posix.FILE

actual fun openPipe(command: String, access: String): CPointer<FILE>? = popen(command, access)
actual fun closePipe(filePtr: CPointer<FILE>): Int = pclose(filePtr)
6 changes: 0 additions & 6 deletions src/mingwX64Main/c_interop/WinApi.def

This file was deleted.

9 changes: 9 additions & 0 deletions src/mingwX64Main/interop/libcurl.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package = libcurl
headers = curl.h
headerFilter = *
linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl -L/opt/homebrew/opt/curl/include/curl
compilerOpts.osx = -I/opt/local/include/curl -I/usr/bin/curl -I/usr/local/include/curl -I/usr/include/curl -I/usr/local/Cellar/curl/7.81.0/include/curl -I/usr/local/Cellar/curl/7.80.0_1/include/curl -I/usr/local/Cellar/curl/7.80.0/include/curl -I/usr/local/Cellar/curl/7.62.0/include/curl -I/usr/local/Cellar/curl/7.63.0/include/curl -I/usr/local/Cellar/curl/7.65.3/include/curl -I/usr/local/Cellar/curl/7.66.0/include/curl -I/opt/homebrew/opt/curl/include/curl
linkerOpts.linux = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lcurl -L/opt/homebrew/opt/curl/include/curl
compilerOpts.linux = -I/usr/include/curl -I/usr/include/x86_64-linux-gnu/curl -I/opt/homebrew/opt/curl/include/curl
linkerOpts.mingw_x64 = -LC:/msys64/mingw64/lib -LC:/Tools/msys64/mingw64/lib -LC:/Tools/msys2/mingw64/lib -lcurl -L/opt/homebrew/opt/curl/include/curl
compilerOpts.mingw_x64 = -I/usr/include/curl -I/usr/include/x86_64-linux-gnu/curl -I/opt/homebrew/opt/curl/include/curl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.ktor.generator.cli.installer
import io.ktor.generator.cli.utils.*
import okio.FileSystem
import okio.Path.Companion.toPath
import platform.windows.*

actual val rootKtorDirName: String = ".ktor."
actual val jdkDownloadUrl: String = "https://download.java.net/java/ga/jdk11/openjdk-11_windows-x64_bin.zip"
Expand All @@ -14,4 +15,10 @@ actual fun unpackJdk(archive: File, outputDir: Directory) {
FileSystem.SYSTEM.atomicMove("${Directory.current().path}\\jdk-11".toPath(), outputDir.path.toPath())
}

actual fun isGradleWrapper(file: File): Boolean = file.name.contains("gradlew")
actual fun isGradleWrapper(file: File): Boolean = file.name.contains("gradlew")

actual fun setEnv(varName: String, value: String) {
SetEnvironmentVariableA(varName, value)
}

actual fun getJdkContentsHome(directory: Directory?): Directory? = directory
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package io.ktor.generator.cli.utils

import io.ktor.generator.cli.installer.*
import io.ktor.utils.io.core.*
import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.allocArray
import kotlinx.cinterop.toKString
import platform.posix.*

actual val FS_DELIMETER: String = "\\"

Expand All @@ -11,4 +17,10 @@ actual fun unzip(zipFile: File, outputDir: Directory) {

actual fun homePath(): String = getEnv("USERPROFILE") ?: throw Exception("Couldn't locate user home path")

actual fun addExecutablePermissions(file: File) {}
actual fun addExecutablePermissions(file: File) {}

actual fun realPath(path: String, buffer: CPointer<ByteVar>): String? {
return _fullpath(buffer, path, PATH_MAX)?.toKString()
}

actual fun getCwd(buffer: CPointer<ByteVar>, size: Int) = _getcwd(buffer, size)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.ktor.generator.cli.utils

import platform.posix._pclose
import platform.posix._popen
import kotlinx.cinterop.CPointer
import platform.posix.FILE

actual fun openPipe(command: String, access: String): CPointer<FILE>? = _popen(command, access)
actual fun closePipe(filePtr: CPointer<FILE>): Int = _pclose(filePtr)
4 changes: 2 additions & 2 deletions src/nativeMain/kotlin/CliGeneratorMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class GenerateProject(client: HttpClient) : KtorCommand(
}

class RunProject(client: HttpClient) : KtorCommand(
"run", description = PropertiesBundle.message("run.command.description"), client = client
"start", description = PropertiesBundle.message("run.command.description"), client = client
) {
private val args: List<String> by argument(
ArgType.String, fullName = "args", description = PropertiesBundle.message("run.arguments.description")
).vararg()
).optional().vararg()

override fun execute() {
ktorInstaller.runKtorProject(projectName, args)
Expand Down
16 changes: 14 additions & 2 deletions src/nativeMain/kotlin/io/ktor/generator/bundle/PropertiesBundle.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.ktor.generator.bundle

import com.github.ajalt.mordant.rendering.TextColors.green
import com.github.ajalt.mordant.rendering.TextColors.red
import com.github.ajalt.mordant.terminal.Terminal
import kotlin.test.assertNotNull

// TODO: figure out how to read properties from resources and deploy with project in Kotlin/Native
Expand All @@ -18,8 +21,9 @@ object PropertiesBundle {
"project.already.exists" to "Project with name {0} already exists",
"generating.project" to "Generating your ktor project",
"project.downloaded" to "Project \"{0}\" was downloaded. Running gradle setup...",
"project.generated" to "Project \"{0}\" was successfully generated.\nYou can execute `ktor run {0}` to run it",
"project.not.exists" to "Project {0} does not exist"
"project.generated" to "Project \"{0}\" was successfully generated.\nYou can execute `ktor start {0}` to start it",
"project.not.exists" to "Project {0} does not exist",
"project.not.have.gradlew" to "Invalid project. Project \"{0}\" does not have gradlew file"
)

private val argumentRegex = "\\{\\d+}".toRegex()
Expand All @@ -44,4 +48,12 @@ object PropertiesBundle {
}

fun writeMessage(property: String, vararg args: String) = println(message(property, *args))
fun writeErrorMessage(property: String, vararg args: String) {
println()
Terminal().println(red(message(property, *args)))
}
fun writeSuccessMessage(property: String, vararg args: String) {
println()
Terminal().println(green(message(property, *args)))
}
}
Loading

0 comments on commit bee37c6

Please sign in to comment.