GRiMe2D / redtamarin

Automatically exported from code.google.com/p/redtamarin
Other
1 stars 0 forks source link

we need a load() function to load external ABC files #48

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
it could look something like that

    /* note:
       load an external library into memory

       A valid library has the extension ".abc"
       if you provide "test" (a name without extension), we will search automatically for "test.abc"
       if you provide any other extension a warning message will display and the loading stop

       When looking for a library a certain order is followed
       if you provide an absolute path like "/var/lib/mystuff/test.abc"
       we test for the existence fo the file and if it exists we load it
       if it does not exists, we display a warning and the loading stop

       if you provide a relative path like "test.abc"
       - first we search "test.abc" in the current directory
       - then we search "test.abc" in the startup directory
       - then we search "test.abc" in the REDTAMARINHOME/lib directory
       - if REDTAMARINPATH exists we search in each directories contained in REDTAMARINPATH

    */
    public function loadLibrary( name:String, domain:Domain = null  ):void
    {
        var ext:String = FileSystem.getExtension( name );

        if( ext != "abc" )
        {
            trace( "\""+name+"\" is not a valid library, extension should be 'abc' not '"+ext+"'" );
            return;
        }

        if( ext == "" )
        {
            name += ".abc"; //we automatically add the extension if not found
        }

        if( !FileSystem.isAbsolutePath( name ) )
        {
            if( !FileSystem.exists( name ) )
            {
                trace( "\""+name+"\" could not be found on the file system." );
                return;
            }
        }
        else
        {
            var i:uint;
            var paths:Array = [];
                paths.push( name );
                paths.push( FileSystem.absolutePath(name) );
                paths.push( FileSystem.ensureEndsWithSeparator(System.workingDirectory) + name );
                paths.push( FileSystem.ensureEndsWithSeparator(System.startupDirectory) + name );

            var home:String = getenv( "REDTAMARINHOME" );
            if( home != "" )
            {
                home = FileSystem.ensureEndsWithSeparator(home) + "lib";
                paths.push( FileSystem.ensureEndsWithSeparator(home) + name );
            }

            var path:String = getenv( "REDTAMARINPATH" );
            if( path != "" )
            {
                var pathsep:String = FileSystem.pathSeparator;
                var morepaths:Array = path.split( pathsep );
                for( i=0; i<morepaths.length; i++ )
                {
                    paths.push( FileSystem.ensureEndsWithSeparator( morepaths[i] ) + name );
                }
            }

            trace( "DEBUG - all search path for library" );
            trace( paths.join( "\n" ) );
            for( i=0; i<paths.length; i++ )
            {
                name = paths[i];
                trace( "DEBUG - test path: " + name );
                if( FileSystem.exists( name ) )
                {
                    break; //found a valid path
                }
            }
        }

        if( !domain )
        {
            domain = Domain.currentDomain;
        }

        trace( "DEBUG - loading library from \"" + name + "\"" )
        domain.load( name );
    }

why ?

when you use redtamarin server side (as CGI or HTTP server or other)
if you execute script files it would be a nicea feature to be able to load
libraries "on the fly"

but the problem is more complicated then that, we need to get some inspiration
on how to deal with environment variables like what Python does

http://docs.python.org/using/cmdline.html

ideally a loadLibrary() should work like an import statement
but be smart enough to be able to search in different directories to find this 
library

for ex:
loadLibrary( "cgilib.abc" )

if your script is run from
/cgi-bin/test.as3

the script should look first in
/cgi-bin/lib/cgilib.abc

if not found, then in
/usr/local/redtamarin/lib/cgilib.abc
or
/usr/share/redtamarin-0.3.2/lib/cgilib.abc

if not found, then in
/home/vhosts/redtamarin/lib/cgilib.abc

etc. etc.

for this to work we need to define env var like
REDTAMARINHOME and REDTAMARINPATH (maybe others ?)

and need to decide who is responsible to define those variables,
the user ? an install program ? other ?

Original issue reported on code.google.com by zwetan on 25 Sep 2011 at 1:31

GoogleCodeExporter commented 9 years ago
keep it simple, let's name it load()

eg.

import - to use a package
load - to load an external library

Original comment by zwetan on 7 Dec 2013 at 5:47