From b86d91a287c0f66e21da69c8d3127b1e64fa7902 Mon Sep 17 00:00:00 2001 From: Esoteric Slime <90862990+EsotericSlime@users.noreply.github.com> Date: Tue, 10 Sep 2024 23:41:13 +0100 Subject: [PATCH] Remove usage of `java.nio` I have no idea what that is, and it seems to be causing issues. This code was refactored by ChatGPT. --- build.gradle.kts | 61 ++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a9975468..fe41757e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 @@ -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) { @@ -75,26 +72,17 @@ 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()) { @@ -102,8 +90,11 @@ fun moveFilesRecursively(sourceDir: File, destDir: File) { 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) } @@ -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() @@ -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 { @@ -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'") }