Skip to content

Commit

Permalink
chore: handle File.createNewFile() result in Main.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellet committed Jun 26, 2024
1 parent 8b35553 commit cc2924e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 13 additions & 3 deletions sync-script/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ suspend fun main(args: Array<String>) {
}

// TODO: Plan if we should implement this in non GUI mode
val isPreferencesConfiguredFile = SyncScriptDotMinecraftFiles.SyncScriptData.IsPreferencesConfigured.file
if (GuiState.isGuiEnabled &&
!SyncScriptDotMinecraftFiles.SyncScriptData.IsPreferencesConfigured.file
!isPreferencesConfiguredFile
.exists()
) {
val newScriptConfig = QuickPreferencesDialog().showDialog()
Expand All @@ -177,8 +178,17 @@ suspend fun main(args: Array<String>) {
GuiState.updateIsGuiEnabled()

withContext(Dispatchers.IO) {
SyncScriptDotMinecraftFiles.SyncScriptData.IsPreferencesConfigured.file
.createNewFile()
val wasFileCreated =
isPreferencesConfiguredFile
.createNewFile()
if (!wasFileCreated) {
showErrorMessageAndTerminate(
title = "📄 File Already Exists",
message =
"⚠️ The file '${isPreferencesConfiguredFile.name}' already exists. We're unable to create it. " +
"This might be a bug, a workaround is to delete '${isPreferencesConfiguredFile.path}'.",
)
}
}
}

Expand Down
7 changes: 3 additions & 4 deletions sync-script/src/main/kotlin/utils/FileDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ class FileDownloader(
if (!tempFile.exists()) {
tempFile.parentFile.mkdirs()
}
val wasFileDoesNotExist = tempFile.createNewFile()
// If the file doesn't exist, it means it created successfully
if (!wasFileDoesNotExist) {
val wasFileCreated = tempFile.createNewFile()
if (!wasFileCreated) {
showErrorMessageAndTerminate(
title = "📄 File Already Exists",
message =
"⚠️ The temporary file '${tempFile.name}' already exists. We are unable to create it. " +
"⚠️ The temporary file '${tempFile.name}' already exists. We're unable to create it. " +
"This might be a bug," +
" delete the file: ${targetFile.path} as a workaround.",
)
Expand Down

0 comments on commit cc2924e

Please sign in to comment.