Skip to content

Commit

Permalink
Merge pull request #29 from neva-dev/fork-cleanup-actions
Browse files Browse the repository at this point in the history
Fork cleanup actions
  • Loading branch information
Krystian Panek authored Dec 21, 2019
2 parents fdb6a9e + 8c31137 commit 1b365fa
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=4.1.0
version=4.1.1
release.useAutomaticVersion=true

kotlin.version=1.3.50
Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/com/neva/gradle/fork/config/AbstractRule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.neva.gradle.fork.config

import com.neva.gradle.fork.file.FileOperations
import com.neva.gradle.fork.file.visitAll
import org.gradle.api.file.FileTree
import org.gradle.api.file.FileVisitDetails
import java.io.File

abstract class AbstractRule(val config: Config) : Rule {

Expand Down Expand Up @@ -37,4 +39,30 @@ abstract class AbstractRule(val config: Config) : Rule {
fun visitFiles(tree: FileTree, callback: (FileHandler, FileVisitDetails) -> Unit) {
visitTree(tree, { !it.isDirectory }, callback)
}

fun removeEmptyDirs() {
val emptyDirs = mutableListOf<File>()

visitDirs(config.targetTree) { handler, _ ->
val dir = handler.file
if (FileOperations.isDirEmpty(dir)) {
emptyDirs += dir
}
}

emptyDirs.forEach { dir ->
var current = dir
while (current != config.targetDir && FileOperations.isDirEmpty(current)) {
logger.debug("Cleaning empty directory: $current")

if (current.delete()) {
current.delete()
current = current.parentFile
} else {
logger.debug("Cannot delete empty directory: $current")
break
}
}
}
}
}
16 changes: 13 additions & 3 deletions src/main/kotlin/com/neva/gradle/fork/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ abstract class Config(val fork: ForkExtension, val name: String) {
replaceTexts(mapOf(search to replace))
}

fun removeText(removal: String) = removeTexts(listOf(removal))

fun removeTexts(removals: List<String>) = replaceTexts(removals.map { it to "" }.toMap())

fun eachFiles(action: Action<in FileHandler>) {
rule(EachFilesRule(this, action))
}
Expand All @@ -247,22 +251,28 @@ abstract class Config(val fork: ForkExtension, val name: String) {
fun replaceContent(search: String, replace: String) = replaceText(search, replace)

fun eachTextFiles(action: Action<in FileHandler>) {
eachTextFiles(textFiles, textIgnoredFiles, action)
eachFiles(textFiles, textIgnoredFiles, action)
}

fun eachTextFiles(pattern: String, action: Action<in FileHandler>) = eachTextFiles(listOf(pattern), action)

fun eachTextFiles(patterns: List<String>, action: Action<in FileHandler>) {
eachTextFiles(patterns, textIgnoredFiles, action)
eachFiles(patterns, textIgnoredFiles, action)
}

fun eachTextFiles(includes: List<String>, excludes: List<String>, action: Action<in FileHandler>) {
fun eachFiles(includes: List<String>, excludes: List<String>, action: Action<in FileHandler>) {
eachFiles(action, Action {
it.filter.include(includes)
it.filter.exclude(excludes)
})
}

fun removeFile(path: String) = removeFiles(listOf(path))

fun removeFiles(includes: List<String>, excludes: List<String> = listOf()) {
eachFiles(includes, excludes, Action { it.remove() })
}

fun copyTemplateFile(templateName: String) {
copyTemplateFile(templateName, templateName)
}
Expand Down
26 changes: 25 additions & 1 deletion src/main/kotlin/com/neva/gradle/fork/config/FileHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ class FileHandler(val config: Config, val file: File) {
return this
}

fun remove(): FileHandler {
actions += action@{
if (file.exists()) {
logger.info("Removing file $file")

FileUtils.deleteQuietly(file)
}
}

return this
}

fun read(): String {
return FileOperations.read(file)
}
Expand All @@ -72,7 +84,19 @@ class FileHandler(val config: Config, val file: File) {
fun replace(search: String, replace: String): FileHandler {
val content = read()
if (content.contains(search)) {
logger.info("Replacing '$search' with '$replace' in file $file")
if (search.contains("\n") || replace.contains("\n")) {
if (replace.isEmpty()) {
logger.info("Removing from file $file content:\n$search")
} else {
logger.info("Replacing content of file $file\nSearch:\n$search\nReplace:\n$replace")
}
} else {
if (replace.isEmpty()) {
logger.info("Removing from file $file content '$search'")
} else {
logger.info("Replacing '$search' with '${replace.ifBlank { "<empty>" }}' in file $file")
}
}

val updatedContent = content.replace(search, replace)
write(updatedContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Property(
val invalid: Boolean
get() = validate().hasErrors()

var enabled: Boolean = true
var enabled: Boolean = definition.enabled

var required: Boolean = definition.required

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ open class PropertyDefinition @Inject constructor(val name: String) {

var defaultValue: String = ""

var enabled = true

var required = true

var dynamic = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CopyTemplateFilesRule(config: Config, private val files: Map<String, Strin
}

private fun copyAndExpandTemplateFiles() {
files.forEach { templateName, targetName ->
files.forEach { (templateName, targetName) ->
val templateFile = findTemplateFile(templateName)
if (templateFile != null) {
val targetFile = File(config.targetDir, targetName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MoveFilesRule(config: Config, movements: Map<String, () -> String>) : Abst

private fun moveFiles() {
visitFiles(config.targetTree) { handler, details ->
movements.forEach { searchPath, replacePath ->
movements.forEach { (searchPath, replacePath) ->
val sourcePath = details.relativePath.pathString
val targetPath = sourcePath.replace(searchPath, replacePath)
val target = File(config.targetPath, targetPath)
Expand All @@ -28,32 +28,6 @@ class MoveFilesRule(config: Config, movements: Map<String, () -> String>) : Abst
}
}

private fun removeEmptyDirs() {
val emptyDirs = mutableListOf<File>()

visitDirs(config.targetTree) { handler, _ ->
val dir = handler.file
if (FileOperations.isDirEmpty(dir)) {
emptyDirs += dir
}
}

emptyDirs.forEach { dir ->
var current = dir
while (current != config.targetDir && FileOperations.isDirEmpty(current)) {
logger.debug("Cleaning empty directory: $current")

if (current.delete()) {
current.delete()
current = current.parentFile
} else {
logger.debug("Cannot delete empty directory: $current")
break
}
}
}
}

override fun toString(): String {
return "MoveFilesRule(movements=$movements)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReplaceContentsRule(config: Config, replacements: Map<String, () -> String

override fun execute() {
visitFiles(targetTree) { fileHandler, _ ->
replacements.forEach { search, replace -> fileHandler.replace(search, replace) }
replacements.forEach { (search, replace) -> fileHandler.replace(search, replace) }
}
}

Expand Down

0 comments on commit 1b365fa

Please sign in to comment.