Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / loader.nit
index 924f5c8..169d52e 100644 (file)
@@ -39,7 +39,7 @@ module loader
 
 import modelbuilder_base
 import ini
-import picnit_shared
+import nitpm_shared
 
 redef class ToolContext
        # Option --path
@@ -66,9 +66,9 @@ redef class ModelBuilder
                # Setup the paths value
                paths.append(toolcontext.opt_path.value)
 
-               # Packages managed by picnit, only use when not testing with tests.sh
+               # Packages managed by nitpm, only use when not testing with tests.sh
                if "NIT_TESTING_TESTS_SH".environ != "true" then
-                       paths.add picnit_lib_dir
+                       paths.add nitpm_lib_dir
                end
 
                var path_env = "NIT_PATH".environ
@@ -255,6 +255,11 @@ redef class ModelBuilder
                        end
                end
 
+               if mgroup != null then
+                       var alias = mgroup.mpackage.import_alias(name)
+                       if alias != null then name = alias
+               end
+
                var loc = null
                if anode != null then loc = anode.hot_location
                var candidate = search_module_in_paths(loc, name, lookpaths)
@@ -287,6 +292,11 @@ redef class ModelBuilder
        # If found, the module is returned.
        private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable MModule
        do
+               var name_no_version
+               if name.has('=') then
+                       name_no_version = name.split('=').first
+               else name_no_version = name
+
                var res = new ArraySet[MModule]
                for dirname in lookpaths do
                        # Try a single module file
@@ -296,7 +306,7 @@ redef class ModelBuilder
                        var g = identify_group((dirname/name).simplify_path)
                        if g != null then
                                scan_group(g)
-                               res.add_all g.mmodules_by_name(name)
+                               res.add_all g.mmodules_by_name(name_no_version)
                        end
                end
                if res.is_empty then return null
@@ -467,7 +477,7 @@ redef class ModelBuilder
                        # Attach homonymous `ini` file to the package
                        var inipath = path.dirname / "{pn}.ini"
                        if inipath.file_exists then
-                               var ini = new ConfigTree(inipath)
+                               var ini = new IniFile.from_file(inipath)
                                mpackage.ini = ini
                        end
                end
@@ -533,7 +543,7 @@ redef class ModelBuilder
                var parent = null
                var inipath = dirpath / "package.ini"
                if inipath.file_exists then
-                       ini = new ConfigTree(inipath)
+                       ini = new IniFile.from_file(inipath)
                end
 
                if ini == null then
@@ -860,6 +870,13 @@ redef class ModelBuilder
                # If no module yet, then assume that the first element of the path
                # Is to be searched in the path.
                var root_name = n_name.n_path.first.text
+
+               # Search for an alias in required external packages
+               if mgroup != null then
+                       var alias = mgroup.mpackage.import_alias(root_name)
+                       if alias != null then root_name = alias
+               end
+
                var roots = search_group_in_paths(root_name, paths)
                if roots.is_empty then
                        error(n_name, "Error: cannot find `{root_name}`. Tried: {paths.join(", ")}.")
@@ -1161,7 +1178,7 @@ redef class MPackage
        # The `ini` file is given as is and might contain invalid or missing information.
        #
        # Some packages, like stand-alone packages or virtual packages have no `ini` file associated.
-       var ini: nullable ConfigTree = null
+       var ini: nullable IniFile = null
 
        # Array of relative source paths excluded according to the `source.exclude` key of the `ini`
        var excludes: nullable Array[String] is lazy do
@@ -1183,6 +1200,31 @@ redef class MPackage
                end
                return true
        end
+
+       # Get the name to search for, for a `root_name` declared as `import` in `ini`
+       fun import_alias(root_name: String): nullable String
+       do
+               var map = import_aliases_parsed
+               if map == null then return null
+
+               var val = map.get_or_null(root_name)
+               if val == null then return null
+
+               return val.dir_name
+       end
+
+       private var import_aliases_parsed: nullable Map[String, ExternalPackage] is lazy do
+               var ini = ini
+               if ini == null then return null
+
+               var import_line = ini["package.import"]
+               if import_line == null then return null
+
+               var map = import_line.parse_import
+               if map.is_empty then return null
+
+               return map
+       end
 end
 
 redef class MGroup