Skip to content

Commit

Permalink
fix: make forced answers respect onMultipleSources
Browse files Browse the repository at this point in the history
Previously, `--onMultipleSources` did not override `--{yes,no,default}`.
  • Loading branch information
squattingmonk committed Oct 18, 2023
1 parent 7eadc55 commit 9e1c79b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
27 changes: 9 additions & 18 deletions src/nasher/convert.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ proc convert*(opts: Options, target: Target, updatedNss: var seq[string]): bool
if opts.get("noConvert", false):
return cmd != "convert"

let
multiSrcChoices = ["choose", "default", "error"]
multiSrcAction = opts.get("onMultipleSources", multiSrcChoices[0])
if multiSrcAction notin multiSrcChoices:
fatal("--onMultipleSources must be one of [$#]" % join(multiSrcChoices, ", "))

let
category = if cmd.endsWith('e'): cmd[0..^2] & "ing" else: cmd & "ing"
gffUtil = opts.findBin("gffUtil", "nwn_gff", "gff utility")
Expand All @@ -42,6 +36,7 @@ proc convert*(opts: Options, target: Target, updatedNss: var seq[string]): bool
tlkUtil = opts.findBin("tlkUtil", "nwn_tlk", "tlk utility")
tlkFlags = opts.get("tlkFlags")
tlkFormat = opts.get("tlkFormat", "json")
multiSrcAction = opts.get("onMultipleSources", MultiSrcAction.None)

display(category.capitalizeAscii, "target " & target.name)
var outFiles: Table[string, seq[string]]
Expand Down Expand Up @@ -92,18 +87,14 @@ proc convert*(opts: Options, target: Target, updatedNss: var seq[string]): bool
for cacheFile, inFiles in outFiles.pairs:
assert inFiles.len > 0

var srcFile = inFiles[0]
if inFiles.len > 1:
case multiSrcAction
of "error":
fatal("Multiple sources found for $1:\n$2" % [cacheFile, join(inFiles, "\n")])
of "choose":
srcFile = choose(fmt"Multiple sources found for {cacheFile}. " &
"Which one do you wish to use?", inFiles)
of "default": discard
else: assert false

let outFile = cacheDir / cacheFile
let
outFile = cacheDir / cacheFile
srcFile =
if inFiles.len > 1:
chooseFile(cacheFile, inFiles, multiSrcAction)
else:
inFiles[0]

if manifest.getFilesChanged(srcFile, outFile):
let
srcExt = srcFile.getFileExt
Expand Down
24 changes: 22 additions & 2 deletions src/nasher/utils/shared.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, times, strutils, strtabs, tables, json
import std/[os, times, strformat, strutils, strtabs, tables, json]
from sequtils import mapIt, toSeq, deduplicate
from unicode import toLower
from sugar import collect
Expand All @@ -11,6 +11,10 @@ from glob import walkGlob, defaultGlobOptions, GlobOption
import cli, target, options
export cli, target, options

type
MultiSrcAction* {.pure.} = enum
None = "", Choose = "choose", Default = "default", Error = "error"

const GlobalOpts = """
Global Options:
-h, --help Display help for nasher or one of its commands
Expand Down Expand Up @@ -38,7 +42,7 @@ const PackLoopOpts = """
--onMultipleSources:<method>
How to handle multiple sources for the same file
[choices: choose (default), default (accept the first),
error (fail)]
error (fail)]. Overrides --yes/--no if explicitly set.
--removeUnusedAreas Remove references to unused areas in module.ifo (note:
disable if you have areas present only in a hak or
override) [default: true]
Expand Down Expand Up @@ -353,3 +357,19 @@ proc outFile*(srcFile: string): string =
let (_, name, ext) = srcFile.splitFile
if ext == ".json" or ext == ".nwnt": name
else: name & ext

proc chooseFile*(file: string, choices: seq[string], action: MultiSrcAction): string =
## Presents the user with a list of possible locations for `file`, possibly
## allowing them to choose a location based on `action`. Returns the chosen
## location.
case action
of Error:
fatal("Multiple locations for $1:\n$2" % [file, choices.join("\n")])
else:
let current = getForceAnswer()
case action
of Choose: setForceAnswer(None)
of Default: setForceAnswer(Default)
else: discard
result = choose(fmt"Multiple locations available for {file}. Please choose:", choices)
setForceAnswer(current)

0 comments on commit 9e1c79b

Please sign in to comment.