nitc :: CmdMainCompile :: defaultinit
# Cmd that finds the nitc command related to an `mentity`
class CmdMainCompile
super CmdEntity
var file: nullable SourceFile = null
var command: nullable String is lazy do
var path = test_path(file)
if path == null then return null
return "nitc {path}"
end
redef fun init_command do
var res = super
if not res isa CmdSuccess then return res
var mentity = self.mentity.as(not null)
if mentity isa MModule then
file = mmodule_main(mentity)
if file == null then return new WarningNoMain(mentity)
else
return new WarningNoMain(mentity)
end
return res
end
# Does `mmodule` has a `main` method?
private fun mmodule_main(mmodule: MModule): nullable SourceFile do
for mclassdef in mmodule.mclassdefs do
if mclassdef.name != "Sys" then continue
for mpropdef in mclassdef.mpropdefs do
if mpropdef.name == "main" then return mmodule.location.file
end
end
return null
end
# Return the sourcefile path
#
# This method exists for the only purpose to be redefined by nitunit tests
# to avoid path diffs.
private fun test_path(file: nullable SourceFile): nullable String do
if file == null then return null
var base_path = mentity.as(MModule).mpackage.as(not null).location.file.as(not null).filename
return file.filename.replace(base_path, "")
end
end
src/doc/commands/commands_main.nit:73,1--119,3