Skip to content

Commit

Permalink
Remove usage of java.nio
Browse files Browse the repository at this point in the history
I have no idea what that is, and it seems to be causing issues. This code was refactored by ChatGPT.
  • Loading branch information
esotericenderman committed Sep 10, 2024
1 parent c50ee11 commit b86d91a
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import org.gradle.api.JavaVersion
import xyz.jpenilla.resourcefactory.bukkit.BukkitPluginYaml

import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import java.nio.file.*
import java.io.File
import java.io.IOException

plugins {
java
Expand Down Expand Up @@ -40,16 +37,16 @@ fun pascalcase(string: String): String {
}

fun replaceStringInFile(filePath: String, stringToReplace: String, replacementString: String) {
val file = Paths.get(filePath)
val file = File(filePath)

if (!Files.exists(file)) {
throw NoSuchFileException(filePath)
if (!file.exists()) {
throw IOException("File does not exist: $filePath")
}

val content = String(Files.readAllBytes(file))
val content = file.readText()
val updatedContent = content.replace(stringToReplace, replacementString)

Files.write(file, updatedContent.toByteArray(), StandardOpenOption.TRUNCATE_EXISTING)
file.writeText(updatedContent)
}

fun replaceStringInDirectoryFiles(directory: File, stringToReplace: String, replacementString: String) {
Expand All @@ -75,35 +72,29 @@ fun moveFilesRecursively(sourceDir: File, destDir: File) {
}

sourceDir.walkTopDown().filter { it.isFile }.forEach { sourceFile ->
val destFile = destDir.toPath().resolve(sourceFile.toPath().toString()).toFile()
val relativePath = sourceFile.relativeTo(sourceDir).path
val destFile = File(destDir, relativePath)

println("Attempting to move files")
println("src: $sourceFile")
println("dest: $destFile")
println()
println("Attempting to move file from ${sourceFile.path} to ${destFile.path}")

try {
print("Attempting to create dir ${sourceFile.parentFile}")

print("The parent of the dest files is: ${destFile.parentFile}")

if (!destFile.parentFile.mkdirs()) {
println("FAILED to create ${sourceFile.path}")

if (!destFile.canWrite()) {
println("CANNOT WRITE TO DEST FILE")
if (!destFile.parentFile.exists()) {
if (!destFile.parentFile.mkdirs()) {
println("FAILED to create directory ${destFile.parentFile.path}")
return
}

return
}

if (!sourceFile.exists()) {
println("Source file ${sourceFile.path} does not exist!")
return
}

Files.move(sourceFile.toPath(), destFile.toPath())
println("Moved file ${sourceFile.path} to $destFile")
if (!sourceFile.renameTo(destFile)) {
throw IOException("Failed to move file from ${sourceFile.path} to ${destFile.path}")
}

println("Moved file ${sourceFile.path} to ${destFile.path}")
} catch (e: Exception) {
throw RuntimeException(e)
}
Expand Down Expand Up @@ -185,8 +176,7 @@ tasks.register("renameProject") {

val settingsFilePath = projectDir.resolve("settings.gradle.kts").toString()
val buildFilePath = projectDir.resolve("build.gradle.kts").toString()
val javaSourcePath = projectDir.resolve(startPath).toPath().toFile()
val javaSourcePathString = javaSourcePath.toString()
val javaSourcePath = projectDir.resolve(startPath)

val currentProjectName = rootProject.name
val currentGroupString = project.group.toString()
Expand All @@ -195,17 +185,12 @@ tasks.register("renameProject") {
val currentMainClassName = pascalcase(currentProjectName)
val currentMainClassFileName = "$currentMainClassName.java"

val oldMainClassFilePath = projectDir.resolve(Paths.get(startPath, currentGroupPath, currentMainClassFileName).toFile())
val newMainClassFilePath = projectDir.resolve(Paths.get(startPath, currentGroupPath, newMainClassFileName).toFile())
val oldMainClassFilePath = File(startPath, currentGroupPath + File.separator + currentMainClassFileName)
val newMainClassFilePath = File(startPath, currentGroupPath + File.separator + newMainClassFileName)

println("Current main class file path: ${oldMainClassFilePath.absolutePath}")
println("New main class file path: ${newMainClassFilePath.absolutePath}")

val destinationDir = newMainClassFilePath.parentFile
if (!destinationDir.exists()) {
destinationDir.mkdirs()
}

if (oldMainClassFilePath.exists() && oldMainClassFilePath.renameTo(newMainClassFilePath)) {
println("Successfully renamed main file from ${oldMainClassFilePath.absolutePath} to ${newMainClassFilePath.absolutePath}")
} else {
Expand All @@ -219,7 +204,7 @@ tasks.register("renameProject") {
replaceStringInFile(buildFilePath, "val mainProjectAuthor = \"$mainProjectAuthor\"", "val mainProjectAuthor = \"$newAuthorName\"")
replaceStringInFile(buildFilePath, "val topLevelDomain = \"$topLevelDomain\"", "val topLevelDomain = \"$newTopLevelDomain\"")

moveFilesRecursively(Paths.get(startPath, currentGroupPath).toFile(), Paths.get(startPath, newGroupPath).toFile())
moveFilesRecursively(File(startPath, currentGroupPath), File(startPath, newGroupPath))

println("Renamed project to '$newName', author to '$newAuthorName', and top-level domain to '$newTopLevelDomain'")
}
Expand Down

0 comments on commit b86d91a

Please sign in to comment.