+ # cache for `identify_file` by realpath
+ private var identified_files = new HashMap[String, nullable ModulePath]
+
+ # Identify a source file
+ # Load the associated project and groups if required
+ private fun identify_file(path: String): nullable ModulePath
+ do
+ if not path.file_exists then
+ toolcontext.error(null, "Error: `{path}` does not exists")
+ return null
+ end
+
+ # Fast track, the path is already known
+ var pn = path.basename(".nit")
+ var rp = module_absolute_path(path)
+ if identified_files.has_key(rp) then return identified_files[rp]
+
+ # Search for a group
+ var mgrouppath = path.join_path("..").simplify_path
+ var mgroup = get_mgroup(mgrouppath)
+
+ if mgroup == null then
+ # singleton project
+ var mproject = new MProject(pn, model)
+ mgroup = new MGroup(pn, mproject, null) # same name for the root group
+ mgroup.filepath = path
+ mproject.root = mgroup
+ toolcontext.info("found project `{pn}` at {path}", 2)
+ end
+
+ var res = new ModulePath(pn, path, mgroup)
+
+ identified_files[rp] = res
+ return res
+ end
+
+ # groups by path
+ private var mgroups = new HashMap[String, nullable MGroup]
+
+ # return the mgroup associated to a directory path
+ # if the directory is not a group null is returned
+ private fun get_mgroup(dirpath: String): nullable MGroup
+ do
+ var rdp = module_absolute_path(dirpath)
+ if mgroups.has_key(rdp) then
+ return mgroups[rdp]
+ end
+
+ # Hack, a dir is determined by the presence of a honomymous nit file
+ var pn = rdp.basename(".nit")
+ var mp = dirpath.join_path(pn + ".nit").simplify_path
+
+ if not mp.file_exists then return null
+
+ # check parent directory
+ var parentpath = dirpath.join_path("..").simplify_path
+ var parent = get_mgroup(parentpath)
+
+ var mgroup
+ if parent == null then
+ # no parent, thus new project
+ var mproject = new MProject(pn, model)
+ mgroup = new MGroup(pn, mproject, null) # same name for the root group
+ mproject.root = mgroup
+ toolcontext.info("found project `{mproject}` at {dirpath}", 2)
+ else
+ mgroup = new MGroup(pn, parent.mproject, parent)
+ toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
+ end
+ mgroup.filepath = dirpath
+ mgroups[rdp] = mgroup
+ return mgroup
+ end
+