-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Cognifide/hosts-editing
Hosts editing more automated
- Loading branch information
Showing
9 changed files
with
212 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
version=1.0.9 | ||
version=1.1.0 | ||
release.useAutomaticVersion=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
plugins { | ||
kotlin("jvm") | ||
application | ||
java | ||
} | ||
|
||
description = "Hosts Editor" | ||
version = "" | ||
|
||
repositories { | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | ||
} | ||
|
||
application { | ||
mainClass.set("MainKt") | ||
} | ||
|
||
tasks { | ||
jar { | ||
manifest { | ||
attributes["Implementation-Title"] = project.description | ||
attributes["Main-Class"] = "MainKt" | ||
} | ||
from(configurations.runtimeClasspath.get().files.map { if (it.isDirectory) it else zipTree(it) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import java.io.File | ||
|
||
fun main(args: Array<String>) = try { | ||
val sourceName = args.getOrNull(0) ?: "mysite" | ||
val sourceFile = File(args.getOrNull(1) ?: ".gradle/environment/hosts.txt") | ||
val sourceSection = Section(sourceName, sourceFile.readText().lines().map { it.trim() }) | ||
val targetFile = File(args.getOrNull(2) ?: "/etc/hosts") | ||
|
||
val text = targetFile.readText() | ||
val sections = Section.parseAll(text) | ||
|
||
val targetSection = sections.find { it.name == sourceSection.name } | ||
|
||
if (targetSection != null) { | ||
targetFile.writeText(text.replace(targetSection.render(), sourceSection.render())) | ||
} else { | ||
targetFile.appendText("${System.lineSeparator()}${sourceSection.render()}") | ||
} | ||
} catch (e: Exception) { | ||
println("Cannot update hosts file!") | ||
println("Ensure using administrator/super-user privileges.") | ||
println("Error details: ${e.message}") | ||
} | ||
|
||
data class Section(val name: String, val entries: List<String>) { | ||
fun render() = mutableListOf<String>().apply { | ||
add("#environment-start") | ||
add("#name=$name") | ||
addAll(entries) | ||
add("#environment-end") | ||
}.joinToString(System.lineSeparator()) | ||
|
||
companion object { | ||
fun parseAll(text: String): List<Section> { | ||
val sections = mutableListOf<Section>() | ||
|
||
var section = false | ||
var sectionName = "" | ||
val sectionLines = mutableListOf<String>() | ||
|
||
text.lineSequence().forEach { line -> | ||
when (val l = line.trim()) { | ||
"#environment-start" -> { | ||
section = true | ||
} | ||
"#environment-end" -> { | ||
sections.add(Section(sectionName, sectionLines.toList())) | ||
section = false | ||
sectionLines.clear() | ||
} | ||
else -> { | ||
if (section) { | ||
if (l.startsWith("#name=")) { | ||
sectionName = l.substringAfter("#name=") | ||
} else { | ||
sectionLines.add(l) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return sections | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
rootProject.name = "environment-plugin" | ||
|
||
include(":hosts") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/kotlin/com/cognifide/gradle/environment/hosts/HostUpdater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.cognifide.gradle.environment.hosts | ||
|
||
import com.cognifide.gradle.environment.EnvironmentExtension | ||
import org.gradle.internal.os.OperatingSystem | ||
|
||
class HostUpdater(val environment: EnvironmentExtension) { | ||
|
||
private val project = environment.project | ||
|
||
private val logger = project.logger | ||
|
||
private val common = environment.common | ||
|
||
val interactive = common.obj.boolean { | ||
convention(true) | ||
common.prop.boolean("environment.hosts.updater.interactive")?.let { set(it) } | ||
} | ||
|
||
val workDir = common.obj.dir { | ||
convention(environment.rootDir.dir("hosts")) | ||
common.prop.file("environment.hosts.updater.workDir")?.let { set(it) } | ||
} | ||
|
||
val targetFile = common.obj.file { | ||
fileProvider(common.obj.provider { | ||
project.file(when { | ||
OperatingSystem.current().isWindows -> """C:\Windows\System32\drivers\etc\hosts""" | ||
else -> "/etc/hosts" | ||
}) | ||
}) | ||
common.prop.file("environment.hosts.updater.targetFile")?.let { set(it) } | ||
} | ||
|
||
val section = common.obj.string { | ||
convention(environment.docker.stack.internalName) | ||
common.prop.string("environment.hosts.updater.section")?.let { set(it) } | ||
} | ||
|
||
@Suppress("MaxLineLength") | ||
fun update() { | ||
val os = OperatingSystem.current() | ||
val osFile = targetFile.get() | ||
|
||
val dir = workDir.get().asFile.apply { mkdirs() } | ||
|
||
val entriesFile = dir.resolve("hosts.txt").apply { | ||
logger.info("Generating hosts entries file: $this") | ||
writeText(environment.hosts.defined.get().joinToString(System.lineSeparator()) { it.text }) | ||
} | ||
val updaterJar = dir.resolve("hosts.jar").apply { | ||
logger.info("Providing hosts updater program: $this") | ||
outputStream().use { output -> | ||
this@HostUpdater.javaClass.getResourceAsStream("/hosts.jar").use { | ||
input -> input.copyTo(output) | ||
} | ||
} | ||
} | ||
val sectionName = section.get() | ||
|
||
if (os.isWindows && interactive.get()) { | ||
val scriptFile = dir.resolve("hosts.bat") | ||
logger.info("Generating hosts updating script: $scriptFile") | ||
|
||
scriptFile.writeText(""" | ||
powershell -command "Start-Process cmd -ArgumentList '/C cd %CD% && java -jar $updaterJar $sectionName $entriesFile $osFile' -Verb runas" | ||
""".trimIndent()) | ||
project.exec { it.commandLine("cmd", "/C", scriptFile.toString()) } | ||
logger.lifecycle("Environment hosts successfully updated.") | ||
} else { | ||
val scriptFile = dir.resolve("hosts.sh") | ||
logger.info("Generating hosts updating script: $scriptFile") | ||
|
||
if (os.isMacOsX && interactive.get()) { | ||
scriptFile.writeText(""" | ||
#!/bin/sh | ||
osascript -e "do shell script \"java -jar $updaterJar $sectionName $entriesFile $osFile\" with prompt \"Gradle Environment Hosts\" with administrator privileges" | ||
""".trimIndent()) | ||
project.exec { it.commandLine("sh", scriptFile.toString()) } | ||
logger.lifecycle("Environment hosts successfully updated.") | ||
} else { | ||
scriptFile.writeText(""" | ||
#!/bin/sh | ||
java -jar $updaterJar $sectionName $entriesFile $osFile | ||
""".trimIndent()) | ||
logger.lifecycle("To update environment hosts, run script below as administrator/super-user:\n$scriptFile") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters