From 065bb113bc8ffdabfa6cf69ee24ce939d1238527 Mon Sep 17 00:00:00 2001 From: "Michael A. Sinclair" Date: Mon, 16 Oct 2023 00:20:13 -0400 Subject: [PATCH] feat: allow automatically overwriting files This adds two new flags, `--overwritePackedFile` and `--overwriteInstalledFile`, which can be used to automatically answer the "Are you sure you wish to overwrite?" prompt when an existing packed or installed file of the same name is found. Valid values include "ask", "default", "always", and "never". Like other nasher flags, these can be set with `nasher config` so you don't have to pass them every time. --- src/nasher/install.nim | 16 ++++++++++++++++ src/nasher/pack.nim | 17 +++++++++++++++++ src/nasher/utils/shared.nim | 10 ++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/nasher/install.nim b/src/nasher/install.nim index f7d4479..5cda15e 100644 --- a/src/nasher/install.nim +++ b/src/nasher/install.nim @@ -57,12 +57,28 @@ proc install*(opts: Options, target: Target): bool = let installedTime = installed.getLastModificationTime timeDiff = getTimeDiff(fileTime, installedTime) + currentAnswer = getForceAnswer() defaultAnswer = if timeDiff >= 0: Yes else: No + # Here we temporarily override the user's forced answer. We do this so if + # the user passed both --yes / --no and --overwriteInstalledFile=ask, we can + # ask the user for input. After we ask, we set the forced answer back so any + # other prompts will be answered as the user intended. + case opts.get("overwriteInstalledFile", "") + of "ask": setForceAnswer(None) + of "default": setForceAnswer(Default) + of "always": setForceAnswer(Yes) + of "never": setForceAnswer(No) + of "": discard + else: + fatal("--overwriteInstalledFile must be one of [ask, default, always, never]") + hint(getTimeDiffHint("The file to be installed", timeDiff)) if not askIf(fmt"{installed} already exists. Overwrite?", defaultAnswer): + setForceAnswer(currentAnswer) return ext == ".mod" and cmd != "install" and askIf(fmt"Do you still wish to {cmd} {filename}?") + setForceAnswer(currentAnswer) copyFile(file, installed) setLastModificationTime(installed, fileTime) diff --git a/src/nasher/pack.nim b/src/nasher/pack.nim index 775e133..c30cbd5 100644 --- a/src/nasher/pack.nim +++ b/src/nasher/pack.nim @@ -62,11 +62,28 @@ proc pack*(opts: Options, target: Target): bool = let packTime = file.getLastModificationTime timeDiff = getTimeDiff(fileTime, packTime) + currentAnswer = getForceAnswer() defaultAnswer = if timeDiff >= 0: Yes else: No + # Here we temporarily override the user's forced answer. We do this so if + # the user passed both --yes / --no and --overwritePackedFile=ask, we can + # ask the user for input. After we ask, we set the forced answer back so any + # other prompts will be answered as the user intended. + case opts.get("overwritePackedFile", "") + of "ask": setForceAnswer(None) + of "default": setForceAnswer(Default) + of "always": setForceAnswer(Yes) + of "never": setForceAnswer(No) + of "": discard + else: + fatal("--overwritePackedFile must be one of [ask, default, always, never]") + hint(getTimeDiffHint("The file to be packed", timeDiff)) if not askIf(fmt"{file} already exists. Overwrite?", defaultAnswer): + setForceAnswer(currentAnswer) return cmd != "pack" and askIf(fmt"Continue installing {file}?") + setForceAnswer(currentAnswer) + else: file.parentDir.createDir diff --git a/src/nasher/utils/shared.nim b/src/nasher/utils/shared.nim index 787ff34..2d073b1 100644 --- a/src/nasher/utils/shared.nim +++ b/src/nasher/utils/shared.nim @@ -79,6 +79,11 @@ const PackOpts* = """ --noCompile Do not re-compile scripts before packing --noConvert Do not re-convert gff files before compiling --packUnchanged Pack a target even if the source files are unchanged + --overwritePackedFile: + Whether to overwrite existing packed files of the same + name in the output directory [choices: ask (default), + default (overwrite if existing file is older, skip if + newer), never, always] $# $# $#""" % [PackLoopOpts, UtilOpts, CompilerOpts] @@ -89,6 +94,11 @@ $# Installation Options: --installDir: Location for installed files (i.e., dir containing erf, hak, modules, and tlk dirs) [default: $$NWN_HOME] + --overwriteInstalledFile: + Whether to overwrite existing files of the same name in + the install directory [choices: ask (default), default + (overwrite if existing file is older, skip if newer), + never, always (will lose any changes from the toolset)] --useModuleFolder Treat modules in $$installDir/modules as folders instead of .mod files (note: EE only) [default: true] """ % PackOpts