Skip to content

Commit 287452c

Browse files
author
Krystian Panek
authored
Merge pull request #10 from Cognifide/fix-env-hosts-on-mac-when-project-in-documents-folder
#8 fix env hosts on Mac when project is in Documents folder
2 parents 3022b4a + 5b12723 commit 287452c

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

src/main/kotlin/com/cognifide/gradle/environment/hosts/HostUpdater.kt

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.cognifide.gradle.environment.hosts
22

3+
import com.cognifide.gradle.environment.EnvironmentException
34
import com.cognifide.gradle.environment.EnvironmentExtension
45
import org.gradle.internal.os.OperatingSystem
6+
import java.io.ByteArrayOutputStream
57

68
class HostUpdater(val environment: EnvironmentExtension) {
79

@@ -50,8 +52,8 @@ class HostUpdater(val environment: EnvironmentExtension) {
5052
val updaterJar = dir.resolve("hosts.jar").apply {
5153
logger.info("Providing hosts updater program: $this")
5254
outputStream().use { output ->
53-
this@HostUpdater.javaClass.getResourceAsStream("/hosts.jar").use {
54-
input -> input.copyTo(output)
55+
this@HostUpdater.javaClass.getResourceAsStream("/hosts.jar").use { input ->
56+
input.copyTo(output)
5557
}
5658
}
5759
}
@@ -64,26 +66,57 @@ class HostUpdater(val environment: EnvironmentExtension) {
6466
scriptFile.writeText("""
6567
powershell -command "Start-Process cmd -ArgumentList '/C cd %CD% && java -jar $updaterJar $sectionName $entriesFile $osFile' -Verb runas"
6668
""".trimIndent())
67-
project.exec { it.commandLine("cmd", "/C", scriptFile.toString()) }
68-
logger.lifecycle("Environment hosts successfully updated.")
69-
} else {
70-
val scriptFile = dir.resolve("hosts.sh")
71-
logger.info("Generating hosts updating script: $scriptFile")
69+
execAndHandleErrors(listOf("cmd", "/C", scriptFile.toString()))
70+
return
71+
}
72+
73+
val scriptFile = dir.resolve("hosts.sh")
74+
logger.info("Generating hosts updating script: $scriptFile")
7275

73-
if (os.isMacOsX && interactive.get()) {
74-
scriptFile.writeText("""
76+
if (os.isMacOsX && interactive.get()) {
77+
scriptFile.writeText("""
7578
#!/bin/sh
7679
osascript -e "do shell script \"java -jar $updaterJar $sectionName $entriesFile $osFile\" with prompt \"Gradle Environment Hosts\" with administrator privileges"
7780
""".trimIndent())
78-
project.exec { it.commandLine("sh", scriptFile.toString()) }
79-
logger.lifecycle("Environment hosts successfully updated.")
80-
} else {
81-
scriptFile.writeText("""
81+
execOnMacAndHandleErrors(listOf("sh", scriptFile.toString()))
82+
return
83+
}
84+
85+
scriptFile.writeText("""
8286
#!/bin/sh
8387
java -jar $updaterJar $sectionName $entriesFile $osFile
8488
""".trimIndent())
85-
logger.lifecycle("To update environment hosts, run script below as administrator/super-user:\n$scriptFile")
89+
logger.lifecycle("To update environment hosts, run script below as administrator/super-user:\n$scriptFile")
90+
}
91+
92+
private fun execOnMacAndHandleErrors(commandLine: List<String>) = execAndHandleErrors(commandLine) { errorText ->
93+
if (errorText.contains("Unable to access jarfile")) {
94+
mutableListOf<String>().apply {
95+
add("Failed to update environment hosts. Unable to access executable. Probably project source files are placed under")
96+
add("the 'Documents', 'Desktop' or 'Downloads' directory. This may cause errors related to accessing files in the future.")
97+
98+
add("Consider troubleshooting:")
99+
add("* move project files outside of 'Documents', 'Desktop' or 'Downloads' directories to avoid problems")
100+
add("* or set the host updater work directory to a path directly under your files home in your gradle.properties file as a workaround:")
101+
add(" * environment.hosts.updater.workDir=${System.getProperty("user.home")}/.gap/hosts")
102+
103+
logger.error(joinToString("\n"))
86104
}
87105
}
106+
throw EnvironmentException(errorText)
107+
}
108+
109+
private fun execAndHandleErrors(commandLine: List<String>, errorHandler: (String) -> Unit = { throw EnvironmentException(it) }) {
110+
val errorOutput = ByteArrayOutputStream()
111+
val execResult = project.exec {
112+
it.errorOutput = errorOutput
113+
it.isIgnoreExitValue = true
114+
it.commandLine(commandLine)
115+
}
116+
if (execResult.exitValue == 0) {
117+
logger.lifecycle("Environment hosts successfully updated.")
118+
return
119+
}
120+
errorHandler(errorOutput.toString("UTF8"))
88121
}
89122
}

0 commit comments

Comments
 (0)