toolcontext: make `nit_dir` more robust and client-definable
authorJean Privat <jean@pryen.org>
Fri, 10 Jun 2016 14:00:15 +0000 (10:00 -0400)
committerJean Privat <jean@pryen.org>
Fri, 10 Jun 2016 14:00:15 +0000 (10:00 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/loader.nit
src/toolcontext.nit

index d1626cb..8ac6ef5 100644 (file)
@@ -71,10 +71,12 @@ redef class ModelBuilder
                end
 
                var nit_dir = toolcontext.nit_dir
-               var libname = nit_dir/"lib"
-               if libname.file_exists then paths.add(libname)
-               libname = nit_dir/"contrib"
-               if libname.file_exists then paths.add(libname)
+               if nit_dir != null then
+                       var libname = nit_dir/"lib"
+                       if libname.file_exists then paths.add(libname)
+                       libname = nit_dir/"contrib"
+                       if libname.file_exists then paths.add(libname)
+               end
        end
 
        # Load a bunch of modules.
index de15aed..1ad54d9 100644 (file)
@@ -501,7 +501,7 @@ The Nit language documentation and the source code of its tools and libraries ma
                        exit 1
                end
 
-               nit_dir = compute_nit_dir
+               nit_dir = locate_nit_dir
 
                if option_context.rest.is_empty and not accept_no_arguments then
                        print tooldescription
@@ -543,7 +543,9 @@ The Nit language documentation and the source code of its tools and libraries ma
        end
 
        # The identified root directory of the Nit package
-       var nit_dir: String is noinit
+       #
+       # It is assignable but is automatically set by `process_options` with `locate_nit_dir`.
+       var nit_dir: nullable String = null is writable
 
        # Shared files directory.
        #
@@ -559,7 +561,22 @@ The Nit language documentation and the source code of its tools and libraries ma
                return sharedir
        end
 
-       private fun compute_nit_dir: String
+       # Guess a possible nit_dir.
+       #
+       # It uses, in order:
+       #
+       # * the option `opt_no_color`
+       # * the environment variable `NIT_DIR`
+       # * the runpath of the program from argv[0]
+       # * the runpath of the process from /proc
+       # * the search in PATH
+       #
+       # If there is errors (e.g. the indicated path is invalid) or if no
+       # path is found, then an error is displayed and the program exits.
+       #
+       # The result is returned without being assigned to `nit_dir`.
+       # This function is automatically called by `process_options`
+       fun locate_nit_dir: String
        do
                # the option has precedence
                var res = opt_nit_dir.value