Skip to content

Commit 363d274

Browse files
authored
🔀 Merge pull request #36 from DrUlysses/main
Added quotation for registry in windows, like edge does
2 parents 7618c8d + edab634 commit 363d274

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

‎auto-launch/src/commonMain/kotlin/io/github/vinceglb/autolaunch/AutoLaunch.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AutoLaunch(
3939
*/
4040
fun isStartedViaAutostart(): Boolean {
4141
val inputArguments = System.getProperty("sun.java.command")?.split(" ") ?: emptyList()
42-
println("Arguments fournis: $inputArguments")
42+
logger.d { "Starting via AutoLaunch with inputArguments: $inputArguments" }
4343
return inputArguments.contains("--autostart=true")
4444
}
4545

‎auto-launch/src/commonMain/kotlin/io/github/vinceglb/autolaunch/PlatformAutoLaunchLinux.kt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.vinceglb.autolaunch
22

3+
import co.touchlab.kermit.Logger
34
import kotlin.io.path.*
45

56
internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : PlatformAutoLaunch {
7+
private val logger = Logger.withTag("PlatformAutoLaunchLinux")
68
// Checks if the application is installed
79
// by looking for a corresponding .desktop file in /usr/share/applications and /opt
810
private fun isInstalled(): Boolean {
@@ -19,7 +21,7 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
1921
}
2022

2123
val isInstalled = desktopFile != null || optFile != null
22-
println("Checking if app is installed: $isInstalled (package: $appPackageName)")
24+
logger.d { "Checking if app is installed: $isInstalled (package: $appPackageName)" }
2325
return isInstalled
2426
}
2527

@@ -31,10 +33,10 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
3133
it.name.endsWith("${appPackageName.replace(" ", "_")}.desktop")
3234
}
3335
return if (desktopFile != null && desktopFile.exists()) {
34-
println("Reading desktop file content from: $desktopFile")
36+
logger.d { "Reading desktop file content from: $desktopFile" }
3537
desktopFile.readText()
3638
} else {
37-
println("Desktop file not found for package: $appPackageName")
39+
logger.d { "Desktop file not found for package: $appPackageName" }
3840
null
3941
}
4042
}
@@ -56,10 +58,10 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
5658
val key = entry.substringBefore("=")
5759
val existingIndex = updatedContent.indexOfFirst { it.startsWith(key) }
5860
if (existingIndex != -1) {
59-
println("Updating existing entry: $key")
61+
logger.d { "Updating existing entry: $key" }
6062
updatedContent[existingIndex] = entry
6163
} else {
62-
println("Adding new entry: $entry")
64+
logger.d { "Adding new entry: $entry" }
6365
updatedContent.add(entry)
6466
}
6567
}
@@ -69,12 +71,12 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
6971
if (execIndex != -1) {
7072
val execLine = updatedContent[execIndex]
7173
if (!execLine.contains("--autostart=true")) {
72-
println("Adding --autostart=true to the Exec line")
74+
logger.d { "Adding --autostart=true to the Exec line" }
7375
updatedContent[execIndex] = "$execLine --autostart=true"
7476
}
7577
} else {
7678
// If Exec line does not exist, create a new one
77-
println("Adding new Exec line with --autostart=true")
79+
logger.d { "Adding new Exec line with --autostart=true" }
7880
updatedContent.add("Exec=${config.appPath} --autostart=true")
7981
}
8082

@@ -86,11 +88,11 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
8688
private fun writeAutostartDesktopFile(content: String) {
8789
val autostartDirectory = Path(System.getProperty("user.home"), ".config/autostart")
8890
if (!autostartDirectory.exists()) {
89-
println("Creating autostart directory at: $autostartDirectory")
91+
logger.d { "Creating autostart directory at: $autostartDirectory" }
9092
autostartDirectory.createDirectories()
9193
}
9294
val autostartFile = autostartDirectory.resolve("${config.appPackageName}.desktop")
93-
println("Writing autostart desktop file to: $autostartFile")
95+
logger.d { "Writing autostart desktop file to: $autostartFile" }
9496
autostartFile.writeText(content)
9597
}
9698

@@ -113,10 +115,10 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
113115
|WantedBy=default.target
114116
""".trimMargin()
115117
if (servicePath.exists()) {
116-
println("Service $appPath already exists at $servicePath")
118+
logger.d { "Service $appPath already exists at $servicePath" }
117119

118120
if (servicePath.readText() == serviceContent) {
119-
println("Service $appPath is already up to date")
121+
logger.d { "Service $appPath is already up to date" }
120122
return
121123
}
122124
servicePath.deleteExisting()
@@ -132,9 +134,9 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
132134
servicePath.writeText(serviceContent)
133135
}
134136
enableSystemdService(appPath)
135-
println("Service $servicePath has been updated")
137+
logger.d { "Service $servicePath has been updated" }
136138
} catch (e: Exception) {
137-
println("Failed to enable auto start as systemd service: $e")
139+
logger.d { "Failed to enable auto start as systemd service: $e" }
138140
}
139141
}
140142

@@ -171,7 +173,7 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
171173
val appPackageName = config.appPackageName
172174
val autostartFile = Path(System.getProperty("user.home"), ".config/autostart/$appPackageName.desktop")
173175
val isEnabledDesktop = autostartFile.exists()
174-
println("Checking if autostart is enabled: $isEnabledDesktop (path: $autostartFile)")
176+
logger.d { "Checking if autostart is enabled: $isEnabledDesktop (path: $autostartFile)" }
175177

176178
val appPath = appPackageName.replace(" ", "-").lowercase()
177179
val statusProcess = ProcessBuilder("systemctl", "--user", "status", "$appPath.service")
@@ -180,40 +182,40 @@ internal class PlatformAutoLaunchLinux(private val config: AutoLaunchConfig) : P
180182
val statusOutput = statusProcess.inputStream.bufferedReader().readText()
181183
statusProcess.waitFor()
182184
val isEnabledSystemd = statusOutput.contains("Active: active (running)")
183-
println("Checking if systemd is enabled: $isEnabledSystemd (path: $appPath.service)")
185+
logger.d {"Checking if systemd is enabled: $isEnabledSystemd (path: $appPath.service)" }
184186

185187
val isEnabled = isEnabledDesktop || isEnabledSystemd
186188
return isEnabled
187189
}
188190

189191
// Enables autostart by copying and modifying the application's .desktop file
190192
override suspend fun enable() {
191-
println("Enabling autostart for app: ${config.appPackageName}")
193+
logger.d { "Enabling autostart for app: ${config.appPackageName}" }
192194
if (isInstalled()) {
193195
val desktopFileContent = getDesktopFileContent()
194196
if (desktopFileContent != null) {
195197
val modifiedContent = modifyDesktopFileContent(desktopFileContent)
196198
writeAutostartDesktopFile(modifiedContent)
197199
} else {
198-
println("Desktop file content is null. Trying to enable autostart via systemd service.")
200+
logger.d { "Desktop file content is null. Trying to enable autostart via systemd service." }
199201
writeSystemdService()
200202
}
201203
} else {
202-
println("Failed to enable autostart: app is not installed")
204+
logger.d { "Failed to enable autostart: app is not installed" }
203205
}
204206
}
205207

206208
// Disables autostart by deleting the .desktop file in ~/.config/autostart
207209
override suspend fun disable() {
208210
val appPackageName = config.appPackageName
209-
println("Disabling autostart for app: $appPackageName")
211+
logger.d { "Disabling autostart for app: $appPackageName" }
210212
val autostartFile = Path(System.getProperty("user.home"), ".config/autostart/$appPackageName.desktop")
211213
if (autostartFile.exists()) {
212-
println("Deleting autostart desktop file at: $autostartFile")
214+
logger.d { "Deleting autostart desktop file at: $autostartFile" }
213215
autostartFile.deleteIfExists()
214216
} else {
215-
println("Autostart desktop file not found at: $autostartFile")
216-
println("Disabling systemd for app: $appPackageName")
217+
logger.d { "Autostart desktop file not found at: $autostartFile" }
218+
logger.d { "Disabling systemd for app: $appPackageName" }
217219
disableSystemdService(appPackageName.replace(" ", "-").lowercase())
218220
}
219221
}

‎auto-launch/src/commonMain/kotlin/io/github/vinceglb/autolaunch/PlatformAutoLaunchWindows.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class PlatformAutoLaunchWindows(
1414
/* key = */ REGISTRY_KEY,
1515
/* value = */ config.appPackageName
1616
)
17-
value == "${config.appPath} --autostart=true"
17+
value == "cmd /c cd /d \"${Path(config.appPath).parent}\" && \"${config.appPath}\" --autostart=true"
1818
} catch (e: Win32Exception) {
1919
if (e.errorCode == 2) { // ERROR_FILE_NOT_FOUND
2020
false
@@ -44,7 +44,7 @@ internal class PlatformAutoLaunchWindows(
4444
/* root = */ WinReg.HKEY_CURRENT_USER,
4545
/* keyPath = */ REGISTRY_KEY,
4646
/* name = */ config.appPackageName,
47-
/* value = */ "${config.appPath} --autostart=true"
47+
/* value = */ "cmd /c cd /d \"${Path(config.appPath).parent}\" && \"${config.appPath}\" --autostart=true"
4848
)
4949
}
5050

0 commit comments

Comments
 (0)