diff options
author | Alexander Wolfe <61166+memoryhole@users.noreply.github.com> | 2020-07-12 07:07:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-12 16:07:31 +0200 |
commit | 124feb2af87dcbb44e25e7dd4303098ae5e9225d (patch) | |
tree | be8c94f4fceaaec49c11048a8e80fa0fc48e4b1d /nimpretty/nimpretty.nim | |
parent | 9ec9a31f46697a615630fce3614e68e57d390a74 (diff) | |
download | Nim-124feb2af87dcbb44e25e7dd4303098ae5e9225d.tar.gz |
nimpretty support for multiple files (#14890)
Diffstat (limited to 'nimpretty/nimpretty.nim')
-rw-r--r-- | nimpretty/nimpretty.nim | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/nimpretty/nimpretty.nim b/nimpretty/nimpretty.nim index 9b5cf556b..bedf9f248 100644 --- a/nimpretty/nimpretty.nim +++ b/nimpretty/nimpretty.nim @@ -14,7 +14,7 @@ when not defined(nimpretty): import ../compiler / [idents, msgs, syntaxes, options, pathutils, layouter] -import parseopt, strutils, os +import parseopt, strutils, os, sequtils const Version = "0.2" @@ -22,9 +22,10 @@ const (c) 2017 Andreas Rumpf Usage: - nimpretty [options] file.nim + nimpretty [options] nimfiles... Options: --out:file set the output file (default: overwrite the input file) + --outDir:dir set the output dir (default: overwrite the input files) --indent:N[=0] set the number of spaces that is used for indentation --indent:0 means autodetection (default behaviour) --maxLineLen:N set the desired maximum line length (default: 80) @@ -61,37 +62,61 @@ proc prettyPrint(infile, outfile: string, opt: PrettyOptions) = closeParsers(p) proc main = - var infile, outfile: string + var outfile, outdir: string + + var infiles = newSeq[string]() + var outfiles = newSeq[string]() + var backup = false # when `on`, create a backup file of input in case # `prettyPrint` could over-write it (note that the backup may happen even # if input is not actually over-written, when nimpretty is a noop). # --backup was un-documented (rely on git instead). var opt = PrettyOptions(indWidth: 0, maxLineLen: 80) + + for kind, key, val in getopt(): case kind of cmdArgument: - infile = key.addFileExt(".nim") + infiles.add(key.addFileExt(".nim")) of cmdLongOption, cmdShortOption: case normalize(key) of "help", "h": writeHelp() of "version", "v": writeVersion() of "backup": backup = parseBool(val) of "output", "o", "out": outfile = val + of "outDir", "outdir": outdir = val of "indent": opt.indWidth = parseInt(val) of "maxlinelen": opt.maxLineLen = parseInt(val) else: writeHelp() of cmdEnd: assert(false) # cannot happen - if infile.len == 0: + if infiles.len == 0: quit "[Error] no input file." - if outfile.len == 0: - outfile = infile - if not fileExists(outfile) or not sameFile(infile, outfile): - backup = false # no backup needed since won't be over-written - if backup: - let infileBackup = infile & ".backup" # works with .nim or .nims - echo "writing backup " & infile & " > " & infileBackup - os.copyFile(source = infile, dest = infileBackup) - prettyPrint(infile, outfile, opt) + + if outfile.len != 0 and outdir.len != 0: + quit "[Error] out and outDir cannot both be specified" + + if outfile.len == 0 and outdir.len == 0: + outfiles = infiles + elif outfile.len != 0 and infiles.len > 1: + # Take the last file to maintain backwards compatibility + let infile = infiles[^1] + infiles = @[infile] + outfiles = @[outfile] + elif outfile.len != 0: + outfiles = @[outfile] + elif outdir.len != 0: + outfiles = infiles.mapIt($(joinPath(outdir, it))) + + for (infile, outfile) in zip(infiles, outfiles): + let (dir, _, _) = splitFile(outfile) + createDir(dir) + if not fileExists(outfile) or not sameFile(infile, outfile): + backup = false # no backup needed since won't be over-written + if backup: + let infileBackup = infile & ".backup" # works with .nim or .nims + echo "writing backup " & infile & " > " & infileBackup + os.copyFile(source = infile, dest = infileBackup) + prettyPrint(infile, outfile, opt) main() |