cmdlinehelper.nim 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Helpers for binaries that use compiler passes, e.g.: nim, nimsuggest, nimfix
  10. import
  11. options, idents, nimconf, extccomp, commands, msgs,
  12. lineinfos, modulegraphs, condsyms, os, pathutils, parseopt
  13. proc prependCurDir*(f: AbsoluteFile): AbsoluteFile =
  14. when defined(unix):
  15. if os.isAbsolute(f.string): result = f
  16. else: result = AbsoluteFile("./" & f.string)
  17. else:
  18. result = f
  19. proc addCmdPrefix*(result: var string, kind: CmdLineKind) =
  20. # consider moving this to std/parseopt
  21. case kind
  22. of cmdLongOption: result.add "--"
  23. of cmdShortOption: result.add "-"
  24. of cmdArgument, cmdEnd: discard
  25. type
  26. NimProg* = ref object
  27. suggestMode*: bool
  28. supportsStdinFile*: bool
  29. processCmdLine*: proc(pass: TCmdLinePass, cmd: string; config: ConfigRef)
  30. proc initDefinesProg*(self: NimProg, conf: ConfigRef, name: string) =
  31. condsyms.initDefines(conf.symbols)
  32. defineSymbol conf.symbols, name
  33. proc processCmdLineAndProjectPath*(self: NimProg, conf: ConfigRef) =
  34. self.processCmdLine(passCmd1, "", conf)
  35. if conf.projectIsCmd and conf.projectName in ["-", ""]:
  36. handleCmdInput(conf)
  37. elif self.supportsStdinFile and conf.projectName == "-":
  38. handleStdinInput(conf)
  39. elif conf.projectName != "":
  40. setFromProjectName(conf, conf.projectName)
  41. else:
  42. conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile getCurrentDir())
  43. proc loadConfigsAndProcessCmdLine*(self: NimProg, cache: IdentCache; conf: ConfigRef;
  44. graph: ModuleGraph): bool =
  45. if self.suggestMode:
  46. conf.setCmd cmdIdeTools
  47. if conf.cmd == cmdNimscript:
  48. incl(conf.globalOptions, optWasNimscript)
  49. loadConfigs(DefaultConfig, cache, conf, graph.idgen) # load all config files
  50. if not self.suggestMode:
  51. let scriptFile = conf.projectFull.changeFileExt("nims")
  52. # 'nim foo.nims' means to just run the NimScript file and do nothing more:
  53. if fileExists(scriptFile) and scriptFile == conf.projectFull:
  54. if conf.cmd == cmdNone: conf.setCmd cmdNimscript
  55. if conf.cmd == cmdNimscript: return false
  56. # now process command line arguments again, because some options in the
  57. # command line can overwrite the config file's settings
  58. if conf.backend != backendJs: # bug #19059
  59. extccomp.initVars(conf)
  60. self.processCmdLine(passCmd2, "", conf)
  61. if conf.cmd == cmdNone:
  62. rawMessage(conf, errGenerated, "command missing")
  63. graph.suggestMode = self.suggestMode
  64. return true
  65. proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef; graph: ModuleGraph): bool =
  66. ## Alias for loadConfigsAndProcessCmdLine, here for backwards compatibility
  67. loadConfigsAndProcessCmdLine(self, cache, conf, graph)