Skip to content

Commit

Permalink
feat: allow automatically overwriting files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
squattingmonk committed Oct 16, 2023
1 parent 5e5e0cd commit 065bb11
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/nasher/install.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/nasher/pack.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions src/nasher/utils/shared.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:<method>
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]
Expand All @@ -89,6 +94,11 @@ $#
Installation Options:
--installDir:<dir> Location for installed files (i.e., dir containing erf,
hak, modules, and tlk dirs) [default: $$NWN_HOME]
--overwriteInstalledFile:<method>
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
Expand Down

0 comments on commit 065bb11

Please sign in to comment.