Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace kotlinx.cli with Clikt and make stuff suspending. #119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "co.touchlab"
version = "2.1.0"
version = "2.2.0"

kotlin {
listOf(macosX64(), macosArm64()).forEach {
Expand All @@ -31,9 +31,9 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation(libs.kotlinx.cli)
implementation(libs.clikt)
implementation(libs.mordant)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kermit)
implementation(libs.kotlinx.coroutines.core)
}
}
Expand Down Expand Up @@ -63,7 +63,6 @@ kotlin {
}

all {
languageSettings.optIn("kotlinx.cli.ExperimentalCli")
languageSettings.optIn("kotlinx.cinterop.BetaInteropApi")
languageSettings.optIn("kotlin.experimental.ExperimentalNativeApi")
}
Expand Down
8 changes: 5 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
[versions]
kotlin = "2.0.0"
kotlin = "2.1.10"
kotlinx-coroutines = "1.7.3"
kotlinx-cli = "0.3.6"
kotlinx-serialization = "1.6.0"
kermit = "1.2.2"
gradle-doctor = "0.9.2"
clikt = "5.0.3"
mordant = "3.0.1"

[libraries]
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-cli = { module = "org.jetbrains.kotlinx:kotlinx-cli", version.ref = "kotlinx-cli" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
clikt = { module = "com.github.ajalt.clikt:clikt", version.ref = "clikt" }
mordant = { module = "com.github.ajalt.mordant:mordant", version.ref = "mordant" }

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
27 changes: 0 additions & 27 deletions src/macosMain/kotlin/co/touchlab/xcode/cli/EchoWriter.kt

This file was deleted.

52 changes: 24 additions & 28 deletions src/macosMain/kotlin/co/touchlab/xcode/cli/InstallationFacade.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package co.touchlab.xcode.cli

import co.touchlab.kermit.Logger
import co.touchlab.xcode.cli.command.Install
import co.touchlab.xcode.cli.util.Console

object InstallationFacade {
private val logger = Logger.withTag("InstallationFacade")

fun installAll(xcodeInstallations: List<XcodeHelper.XcodeInstallation>, fixXcode15: Boolean) {
suspend fun installAll(xcodeInstallations: List<XcodeHelper.XcodeInstallation>, fixXcode15: Boolean) {
XcodeHelper.ensureXcodeNotRunning()

val bundledVersion = PluginManager.bundledVersion
logger.v { "Bundled plugin version = $bundledVersion" }
Console.muted("Bundled plugin version = $bundledVersion")
val installedVersion = PluginManager.installedVersion
logger.v { "Installed plugin version = ${installedVersion ?: "N/A"}" }
Console.muted("Installed plugin version = ${installedVersion ?: "N/A"}")

if (installedVersion != null) {
val (confirmation, notification) = when {
bundledVersion > installedVersion -> {
"Do you want to update from $installedVersion to $bundledVersion? y/n: " to "Updating to $bundledVersion"
"Do you want to update from $installedVersion to $bundledVersion?" to "Updating to $bundledVersion"
}
bundledVersion == installedVersion -> {
"Do you want to reinstall version $installedVersion? y/n: " to "Reinstalling $installedVersion"
"Do you want to reinstall version $installedVersion?" to "Reinstalling $installedVersion"
}
bundledVersion < installedVersion -> {
"Do you want to downgrade from $installedVersion to $bundledVersion? y/n: " to "Downgrading to $bundledVersion"
"Do you want to downgrade from $installedVersion to $bundledVersion?" to "Downgrading to $bundledVersion"
}
else -> error("Unhandled comparison possibility!")
}
Expand All @@ -33,11 +29,11 @@ object InstallationFacade {
return
}

logger.v { "Installation confirmed." }
logger.i { notification }
Console.muted("Installation confirmed.")
Console.info(notification)
uninstallAll()
} else {
logger.i { "Installing $bundledVersion." }
Console.info("Installing $bundledVersion.")
}

PluginManager.install()
Expand All @@ -50,36 +46,36 @@ object InstallationFacade {
LLDBInitManager.install()
PluginManager.enable(bundledVersion, xcodeInstallations)

logger.i { "Installation complete." }
Console.info("Installation complete.")
}

fun enable(xcodeInstallations: List<XcodeHelper.XcodeInstallation>) {
suspend fun enable(xcodeInstallations: List<XcodeHelper.XcodeInstallation>) {
XcodeHelper.ensureXcodeNotRunning()

val installedVersion = PluginManager.installedVersion ?: run {
Console.echo("Plugin not installed, nothing to enable.")
Console.warning("Plugin not installed, nothing to enable.")
return
}

PluginManager.enable(installedVersion, xcodeInstallations)

logger.i { "Plugin enabled." }
Console.info("Plugin enabled.")
}

fun disable(xcodeInstallations: List<XcodeHelper.XcodeInstallation>) {
suspend fun disable(xcodeInstallations: List<XcodeHelper.XcodeInstallation>) {
XcodeHelper.ensureXcodeNotRunning()

val installedVersion = PluginManager.installedVersion ?: run {
Console.echo("Plugin not installed, nothing to disable.")
Console.warning("Plugin not installed, nothing to disable.")
return
}

PluginManager.disable(installedVersion, xcodeInstallations)

logger.i { "Plugin disabled." }
Console.info("Plugin disabled.")
}

fun fixXcode15(xcodeInstallations: List<XcodeHelper.XcodeInstallation>) {
suspend fun fixXcode15(xcodeInstallations: List<XcodeHelper.XcodeInstallation>) {
XcodeHelper.ensureXcodeNotRunning()

val installedVersion = PluginManager.installedVersion
Expand All @@ -95,14 +91,14 @@ object InstallationFacade {
}
}

logger.i { "Xcode 15 fix applied." }
Console.info("Xcode 15 fix applied.")
}

fun sync(xcodeInstallations: List<XcodeHelper.XcodeInstallation>, fixXcode15: Boolean) {
suspend fun sync(xcodeInstallations: List<XcodeHelper.XcodeInstallation>, fixXcode15: Boolean) {
XcodeHelper.ensureXcodeNotRunning()

val installedVersion = PluginManager.installedVersion ?: run {
Console.echo("Plugin not installed, nothing to synchronize.")
Console.warning("Plugin not installed, nothing to synchronize.")
return
}

Expand All @@ -113,16 +109,16 @@ object InstallationFacade {
}
PluginManager.enable(installedVersion, xcodeInstallations)

logger.i { "Synchronization complete." }
Console.info("Synchronization complete.")
}

fun uninstallAll() {
logger.v { "Will uninstall all plugin components." }
suspend fun uninstallAll() {
Console.muted("Will uninstall all plugin components.")
XcodeHelper.ensureXcodeNotRunning()
PluginManager.uninstall()
LangSpecManager.uninstall()
LLDBInitManager.uninstall()

logger.i { "Uninstallation complete." }
Console.info("Uninstallation complete.")
}
}
17 changes: 7 additions & 10 deletions src/macosMain/kotlin/co/touchlab/xcode/cli/LLDBInitManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package co.touchlab.xcode.cli

import co.touchlab.kermit.Logger
import co.touchlab.xcode.cli.util.Console
import co.touchlab.xcode.cli.util.File
import co.touchlab.xcode.cli.util.Path
Expand All @@ -15,7 +14,6 @@ object LLDBInitManager {
private val sourceMainLlvmInit = "command source ~/.lldbinit"
private val lldbInitFile = File(Path.home / ".lldbinit")
private val lldbInitXcodeFile = File(Path.home / ".lldbinit-Xcode")
private val logger = Logger.withTag("LLDBInitManager")

val isInstalled: Boolean
get() {
Expand All @@ -38,16 +36,16 @@ object LLDBInitManager {

val oldContents = when {
lldbInitXcodeFile.exists() -> {
logger.v { "${lldbInitXcodeFile.path} exists, will append LLDB init into it." }
Console.muted("${lldbInitXcodeFile.path} exists, will append LLDB init into it.")
lldbInitXcodeFile.stringContents().kt
}
lldbInitFile.exists() -> {
Console.echo("""
This installer will create a new file at '~/.lldbinit-Xcode'. This file takes precedence over the '~/.lldbinit' file.
To keep using configuration from '~/.lldbinit' file, it needs to be sourced by the newly created '~/.lldbinit-Xcode' file.
""".trimIndent())
if (Console.confirm("Do you want to source the '~/.lldbinit' file? y/n: ")) {
logger.v { "Will source ~/.lldbinit." }
if (Console.confirm("Do you want to source the '~/.lldbinit' file?")) {
Console.muted("Will source ~/.lldbinit.")
sourceMainLlvmInit
} else {
""
Expand All @@ -60,13 +58,13 @@ object LLDBInitManager {

val oldAndNewContentsSeparator = if (!oldContents.endsWith("\n")) "\n" else ""
val newContents = oldContents + oldAndNewContentsSeparator + sourcePluginInit
logger.v { "Saving new LLDB init to ${lldbInitXcodeFile.path}." }
Console.muted("Saving new LLDB init to ${lldbInitXcodeFile.path}.")
lldbInitXcodeFile.write(newContents.objc)
}

fun uninstall() {
if (isInstalled) {
logger.v { "LLDB init script found, removing." }
Console.muted("LLDB init script found, removing.")
val oldContents = lldbInitXcodeFile.stringContents()
val newContents = oldContents.stringByReplacingOccurrencesOfString(target = sourcePluginInit, withString = "")
lldbInitXcodeFile.write(newContents.objc)
Expand All @@ -75,7 +73,6 @@ object LLDBInitManager {
}

object Legacy {
private val logger = Logger.withTag("LLDBInitManager")
private val legacyImport = """
command script import ~/Library/Developer/Xcode/Plug-ins/Kotlin.ideplugin/Contents/Resources/konan_lldb_config.py
command script import ~/Library/Developer/Xcode/Plug-ins/Kotlin.ideplugin/Contents/Resources/konan_lldb.py
Expand All @@ -91,11 +88,11 @@ object LLDBInitManager {

fun uninstall() {
if (isInstalled) {
logger.v { "Legacy LLDB script initialization found, removing." }
Console.muted("Legacy LLDB script initialization found, removing.")
val oldContents = lldbInitXcodeFile.stringContents()
val newContents = oldContents.stringByReplacingOccurrencesOfString(target = legacyImport, withString = "")
lldbInitXcodeFile.write(newContents.objc)
}
}
}
}
}
11 changes: 5 additions & 6 deletions src/macosMain/kotlin/co/touchlab/xcode/cli/LangSpecManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.touchlab.xcode.cli

import co.touchlab.kermit.Logger
import co.touchlab.xcode.cli.util.Console
import co.touchlab.xcode.cli.util.File
import co.touchlab.xcode.cli.util.Path

Expand All @@ -9,21 +9,20 @@ object LangSpecManager {
private val specSourceFile = File(Path.dataDir / specName)
private val specsDirectory = File(XcodeHelper.xcodeLibraryPath / "Specifications")
private val specTargetFile = File(specsDirectory.path / specName)
private val logger = Logger.withTag("LangSpecManager")

val isInstalled: Boolean
get() = specTargetFile.exists()

fun install() {
check(!specTargetFile.exists()) { "Language spec file exists at path ${specTargetFile.path}! Delete it first." }
logger.v { "Ensuring language specification directory exists at ${specsDirectory.path}" }
Console.muted("Ensuring language specification directory exists at ${specsDirectory.path}")
specsDirectory.mkdirs()
logger.v { "Copying language specification to target path ${specTargetFile.path}" }
Console.muted("Copying language specification to target path ${specTargetFile.path}")
specSourceFile.copy(specTargetFile.path)
}

fun uninstall() {
logger.v { "Deleting language specification from ${specTargetFile.path}." }
Console.muted("Deleting language specification from ${specTargetFile.path}.")
specTargetFile.delete()
}
}
}
Loading