nimfix.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Nimfix is a tool that helps to convert old-style Nimrod code to Nim code.
  10. import strutils, os, parseopt
  11. import compiler/[options, commands, modules, sem,
  12. passes, passaux, nimfix/pretty,
  13. msgs, nimconf,
  14. extccomp, condsyms,
  15. modulegraphs, idents]
  16. const Usage = """
  17. Nimfix - Tool to patch Nim code
  18. Usage:
  19. nimfix [options] projectfile.nim
  20. Options:
  21. --overwriteFiles:on|off overwrite the original nim files.
  22. DEFAULT is ON!
  23. --wholeProject overwrite every processed file.
  24. --checkExtern:on|off style check also extern names
  25. --styleCheck:on|off|auto performs style checking for identifiers
  26. and suggests an alternative spelling;
  27. 'auto' corrects the spelling.
  28. --bestEffort try to fix the code even when there
  29. are errors.
  30. In addition, all command line options of Nim are supported.
  31. """
  32. proc mainCommand =
  33. registerPass verbosePass
  34. registerPass semPass
  35. gCmd = cmdPretty
  36. searchPaths.add options.libpath
  37. if gProjectFull.len != 0:
  38. # current path is always looked first for modules
  39. searchPaths.insert(gProjectPath, 0)
  40. compileProject(newModuleGraph(), newIdentCache())
  41. pretty.overwriteFiles()
  42. proc processCmdLine*(pass: TCmdLinePass, cmd: string) =
  43. var p = parseopt.initOptParser(cmd)
  44. var argsCount = 0
  45. gOnlyMainfile = true
  46. while true:
  47. parseopt.next(p)
  48. case p.kind
  49. of cmdEnd: break
  50. of cmdLongoption, cmdShortOption:
  51. case p.key.normalize
  52. of "overwritefiles":
  53. case p.val.normalize
  54. of "on": gOverWrite = true
  55. of "off": gOverWrite = false
  56. else: localError(gCmdLineInfo, errOnOrOffExpected)
  57. of "checkextern":
  58. case p.val.normalize
  59. of "on": gCheckExtern = true
  60. of "off": gCheckExtern = false
  61. else: localError(gCmdLineInfo, errOnOrOffExpected)
  62. of "stylecheck":
  63. case p.val.normalize
  64. of "off": gStyleCheck = StyleCheck.None
  65. of "on": gStyleCheck = StyleCheck.Warn
  66. of "auto": gStyleCheck = StyleCheck.Auto
  67. else: localError(gCmdLineInfo, errOnOrOffExpected)
  68. of "wholeproject": gOnlyMainfile = false
  69. of "besteffort": msgs.gErrorMax = high(int) # don't stop after first error
  70. else:
  71. processSwitch(pass, p)
  72. of cmdArgument:
  73. options.gProjectName = unixToNativePath(p.key)
  74. # if processArgument(pass, p, argsCount): break
  75. proc handleCmdLine() =
  76. if paramCount() == 0:
  77. stdout.writeLine(Usage)
  78. else:
  79. processCmdLine(passCmd1, "")
  80. if gProjectName != "":
  81. try:
  82. gProjectFull = canonicalizePath(gProjectName)
  83. except OSError:
  84. gProjectFull = gProjectName
  85. var p = splitFile(gProjectFull)
  86. gProjectPath = p.dir
  87. gProjectName = p.name
  88. else:
  89. gProjectPath = getCurrentDir()
  90. loadConfigs(DefaultConfig) # load all config files
  91. # now process command line arguments again, because some options in the
  92. # command line can overwite the config file's settings
  93. extccomp.initVars()
  94. processCmdLine(passCmd2, "")
  95. mainCommand()
  96. when compileOption("gc", "v2") or compileOption("gc", "refc"):
  97. GC_disableMarkAndSweep()
  98. condsyms.initDefines()
  99. defineSymbol "nimfix"
  100. handleCmdline()