Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Aug 28, 2018
1 parent 519537a commit 8b8f445
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 39 deletions.
43 changes: 22 additions & 21 deletions compiler/cmdlinehelper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import
compiler/[options, idents, nimconf, scriptconfig, extccomp, commands, msgs, lineinfos, modulegraphs, condsyms],
std/os

type ProgBase* = ref object of RootObj
type NimProg* = ref object
name*: string
suggestMode*: bool
processCmdLine*: proc(pass: TCmdLinePass, cmd: string; config: ConfigRef)
mainCommand*: proc(graph: ModuleGraph)

method processCmdLine(self: ProgBase, pass: TCmdLinePass, cmd: string; config: ConfigRef) {.base.} =
doAssert false

method mainCommand(self: ProgBase, graph: ModuleGraph) {.base.} =
doAssert false

proc initDefinesProg*(self: ProgBase, conf: ConfigRef) =
proc initDefinesProg*(self: NimProg, conf: ConfigRef) =
condsyms.initDefines(conf.symbols)
defineSymbol conf.symbols, self.name

proc processCmdLineAndProjectPath*(self: ProgBase, conf: ConfigRef) =
proc processCmdLineAndProjectPath*(self: NimProg, conf: ConfigRef) =
self.processCmdLine(passCmd1, "", conf)
if conf.projectName == "-":
conf.projectName = "stdinfile"
Expand All @@ -39,23 +35,28 @@ proc processCmdLineAndProjectPath*(self: ProgBase, conf: ConfigRef) =
else:
conf.projectPath = canonicalizePath(conf, getCurrentDir())

proc loadConfigsAndRunMainCommand*(self: ProgBase, cache: IdentCache; conf: ConfigRef): bool =
proc loadConfigsAndRunMainCommand*(self: NimProg, cache: IdentCache; conf: ConfigRef): bool =
loadConfigs(DefaultConfig, cache, conf) # load all config files
if self.suggestMode:
conf.command = "nimsuggest"

proc runNimScriptIfExists(scriptFile: string)=
if fileExists(scriptFile):
runNimScript(cache, scriptFile, freshDefines=false, conf)
proc runNimScriptIfExists(path: string)=
if fileExists(path):
runNimScript(cache, path, freshDefines = false, conf)

# Caution: make sure this stays in sync with `loadConfigs`
if optSkipSystemConfigFile notin conf.globalOptions:
runNimScriptIfExists(getSystemConfigPath(conf, DefaultConfigNims))

if optSkipUserConfigFile notin conf.globalOptions:
runNimScriptIfExists(getUserConfigPath(DefaultConfigNims))

if optSkipParentConfigFiles notin conf.globalOptions:
for dir in parentDirs(conf.projectPath, fromRoot = true, inclusive = false):
runNimScriptIfExists(dir / DefaultConfigNims)

# TODO:
# merge this complex logic with `loadConfigs`
# check whether these should be controlled via
# optSkipConfigFile, optSkipUserConfigFile
const configNims = "config.nims"
runNimScriptIfExists(getSystemConfigPath(conf, configNims))
runNimScriptIfExists(getUserConfigPath(configNims))
runNimScriptIfExists(conf.projectPath / configNims)
if optSkipProjConfigFile notin conf.globalOptions:
runNimScriptIfExists(conf.projectPath / DefaultConfigNims)
block:
let scriptFile = conf.projectFull.changeFileExt("nims")
if not self.suggestMode:
Expand Down
2 changes: 1 addition & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
else: localError(conf, info, "invalid option for --symbolFiles: " & arg)
of "skipcfg":
expectNoArg(conf, switch, arg, pass, info)
incl(conf.globalOptions, optSkipConfigFile)
incl(conf.globalOptions, optSkipSystemConfigFile)
of "skipprojcfg":
expectNoArg(conf, switch, arg, pass, info)
incl(conf.globalOptions, optSkipProjConfigFile)
Expand Down
12 changes: 5 additions & 7 deletions compiler/nim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ proc prependCurDir(f: string): string =
else:
result = f

type ProgNim=ref object of ProgBase

method processCmdLine(self: ProgNim, pass: TCmdLinePass, cmd: string; config: ConfigRef) =
proc processCmdLine(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
var p = parseopt.initOptParser(cmd)
var argsCount = 0
while true:
Expand All @@ -58,11 +56,11 @@ method processCmdLine(self: ProgNim, pass: TCmdLinePass, cmd: string; config: Co
if optRun notin config.globalOptions and config.arguments.len > 0 and config.command.normalize != "run":
rawMessage(config, errGenerated, errArgsNeedRunOption)

method mainCommand(self: ProgNim, graph: ModuleGraph)=
mainCommand(graph)

proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
let self = ProgNim(name: "nim_compiler")
let self = NimProg(
name: "nim_compiler",
processCmdLine: processCmdLine,
mainCommand: mainCommand)
self.initDefinesProg(conf)
if paramCount() == 0:
writeCommandLineUsage(conf, conf.helpWritten)
Expand Down
3 changes: 2 additions & 1 deletion compiler/nimconf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ proc loadConfigs*(cfg: string; cache: IdentCache; conf: ConfigRef) =
if readConfigFile(configPath, cache, conf):
add(configFiles, configPath)

if optSkipConfigFile notin conf.globalOptions:
if optSkipSystemConfigFile notin conf.globalOptions:
readConfigFile(getSystemConfigPath(conf, cfg))

if optSkipUserConfigFile notin conf.globalOptions:
Expand All @@ -263,4 +263,5 @@ proc loadConfigs*(cfg: string; cache: IdentCache; conf: ConfigRef) =
readConfigFile(projectConfig)

for filename in configFiles:
# delayed to here so that `hintConf` is honored
rawMessage(conf, hintConf, filename)
9 changes: 5 additions & 4 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ type # please make sure we have under 32 options
optGenMapping, # generate a mapping file
optRun, # run the compiled project
optCheckNep1, # check that the names adhere to NEP-1
optSkipConfigFile, # skip the general config file
optSkipProjConfigFile, # skip the project's config file
optSkipUserConfigFile, # skip the users's config file
optSkipParentConfigFiles, # skip parent dir's config files
optSkipSystemConfigFile, # skip the system's cfg/nims config file
optSkipProjConfigFile, # skip the project's cfg/nims config file
optSkipUserConfigFile, # skip the users's cfg/nims config file
optSkipParentConfigFiles, # skip parent dir's cfg/nims config files
optNoMain, # do not generate a "main" proc
optUseColors, # use colors for hints, warnings, and errors
optThreads, # support for multi-threading
Expand Down Expand Up @@ -391,6 +391,7 @@ const
TexExt* = "tex"
IniExt* = "ini"
DefaultConfig* = "nim.cfg"
DefaultConfigNims* = "config.nims"
DocConfig* = "nimdoc.cfg"
DocTexConfig* = "nimdoc.tex.cfg"

Expand Down
2 changes: 1 addition & 1 deletion compiler/scriptconfig.nim
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ proc runNimScript*(cache: IdentCache; scriptName: string;
incl(m.flags, sfMainModule)
graph.vm = setupVM(m, cache, scriptName, graph)

graph.compileSystemModule()
graph.compileSystemModule() # TODO: see why this unsets hintConf in conf.notes
discard graph.processModule(m, llStreamOpen(scriptName, fmRead))

# ensure we load 'system.nim' again for the real non-config stuff!
Expand Down
10 changes: 6 additions & 4 deletions nimsuggest/nimsuggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,7 @@ proc mainThread(graph: ModuleGraph) =
var
inputThread: Thread[ThreadParams]

type ProgNimsuggest=ref object of ProgBase

method mainCommand(self: ProgNimsuggest, graph: ModuleGraph) =
proc mainCommand(graph: ModuleGraph) =
let conf = graph.config
clearPasses(graph)
registerPass graph, verbosePass
Expand Down Expand Up @@ -584,7 +582,11 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string; conf: ConfigRef) =
# if processArgument(pass, p, argsCount): break

proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
let self = ProgNimsuggest(suggestMode: true, name: "nimsuggest")
let self = NimProg(
suggestMode: true,
name: "nimsuggest",
processCmdLine: processCmdLine,
mainCommand: mainCommand)
self.initDefinesProg(conf)

if paramCount() == 0:
Expand Down

0 comments on commit 8b8f445

Please sign in to comment.