11package com.cognifide.gradle.environment.hosts
22
3+ import com.cognifide.gradle.environment.EnvironmentException
34import com.cognifide.gradle.environment.EnvironmentExtension
45import org.gradle.internal.os.OperatingSystem
6+ import java.io.ByteArrayOutputStream
57
68class 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