cmdlinehelper.nim 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, eg: nim, nimsuggest, nimfix
  10. import
  11. options, idents, nimconf, extccomp, commands, msgs,
  12. lineinfos, modulegraphs, condsyms, os, pathutils, parseopt
  13. from strutils import normalize
  14. proc prependCurDir*(f: AbsoluteFile): AbsoluteFile =
  15. when defined(unix):
  16. if os.isAbsolute(f.string): result = f
  17. else: result = AbsoluteFile("./" & f.string)
  18. else:
  19. result = f
  20. proc addCmdPrefix*(result: var string, kind: CmdLineKind) =
  21. # consider moving this to std/parseopt
  22. case kind
  23. of cmdLongOption: result.add "--"
  24. of cmdShortOption: result.add "-"
  25. of cmdArgument, cmdEnd: discard
  26. type
  27. NimProg* = ref object
  28. suggestMode*: bool
  29. supportsStdinFile*: bool
  30. processCmdLine*: proc(pass: TCmdLinePass, cmd: string; config: ConfigRef)
  31. proc initDefinesProg*(self: NimProg, conf: ConfigRef, name: string) =
  32. condsyms.initDefines(conf.symbols)
  33. defineSymbol conf.symbols, name
  34. proc processCmdLineAndProjectPath*(self: NimProg, conf: ConfigRef) =
  35. self.processCmdLine(passCmd1, "", conf)
  36. if self.supportsStdinFile and conf.projectName == "-":
  37. handleStdinInput(conf)
  38. elif conf.projectName != "":
  39. try:
  40. conf.projectFull = canonicalizePath(conf, AbsoluteFile conf.projectName)
  41. except OSError:
  42. conf.projectFull = AbsoluteFile conf.projectName
  43. let p = splitFile(conf.projectFull)
  44. let dir = if p.dir.isEmpty: AbsoluteDir getCurrentDir() else: p.dir
  45. conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile dir)
  46. conf.projectName = p.name
  47. else:
  48. conf.projectPath = AbsoluteDir canonicalizePath(conf, AbsoluteFile getCurrentDir())
  49. proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef;
  50. graph: ModuleGraph): bool =
  51. if self.suggestMode:
  52. conf.command = "nimsuggest"
  53. loadConfigs(DefaultConfig, cache, conf) # load all config files
  54. if not self.suggestMode:
  55. let scriptFile = conf.projectFull.changeFileExt("nims")
  56. # 'nim foo.nims' means to just run the NimScript file and do nothing more:
  57. if fileExists(scriptFile) and scriptFile == conf.projectFull:
  58. if conf.command == "":
  59. conf.command = "e"
  60. return false
  61. elif conf.command.normalize == "e":
  62. return false
  63. # now process command line arguments again, because some options in the
  64. # command line can overwrite the config file's settings
  65. extccomp.initVars(conf)
  66. self.processCmdLine(passCmd2, "", conf)
  67. if conf.command == "":
  68. rawMessage(conf, errGenerated, "command missing")
  69. graph.suggestMode = self.suggestMode
  70. return true