Example #1
0
        private IEnumerable <MutableString> /*!*/ GetPathsToTestLoaded(string /*!*/ path, string fullPath, LoadFlags flags, string[] /*!*/ sourceFileExtensions)
        {
            List <MutableString> paths = new List <MutableString>();

            paths.Add(_context.EncodePath(path));

            if (fullPath != null)
            {
                paths.Add(_context.EncodePath(path));
            }

            if ((flags & LoadFlags.AppendExtensions) != 0 && RubyUtils.GetExtension(path).Length == 0)
            {
                foreach (var extension in sourceFileExtensions)
                {
                    paths.Add(_context.EncodePath(path + extension));
                }
                foreach (var extension in _LibraryExtensions)
                {
                    paths.Add(_context.EncodePath(path + extension));
                }
            }

            return(paths);
        }
Example #2
0
        /// <summary>
        /// Searches file in load directories and then appends extensions.
        /// </summary>
        private ResolvedFile FindFile(string /*!*/ path, bool appendExtensions, string[] sourceFileExtensions)
        {
            Assert.NotNull(path);
            bool isAbsolutePath;

#if SILVERLIGHT
            {
#else
            if (path.StartsWith("~/", StringComparison.Ordinal) || path.StartsWith("~\\", StringComparison.Ordinal))
            {
                path           = RubyUtils.ExpandPath(_context.Platform, path);
                isAbsolutePath = true;
            }
            else
            {
#endif
                try {
                    isAbsolutePath = Platform.IsAbsolutePath(path);
                } catch (ArgumentException e) {
                    throw RubyExceptions.CreateLoadError(e);
                }
            }

            string extension = RubyUtils.GetExtension(path);

            // Absolute path -> load paths not consulted.
            if (isAbsolutePath)
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            string[] loadPaths = GetLoadPathStrings();

            if (loadPaths.Length == 0)
            {
                return(null);
            }

            // If load paths are non-empty and the path starts with .\ or ..\ then MRI also ignores the load paths.
            if (path.StartsWith("./", StringComparison.Ordinal) ||
                path.StartsWith("../", StringComparison.Ordinal) ||
                path.StartsWith(".\\", StringComparison.Ordinal) ||
                path.StartsWith("..\\", StringComparison.Ordinal))
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            foreach (var dir in loadPaths)
            {
                ResolvedFile result = ResolveFile(RubyUtils.CombinePaths(dir, path), extension, appendExtensions, sourceFileExtensions);
                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
Example #3
0
        private bool AlreadyLoaded(string /*!*/ path, string fullPath, LoadFlags flags, string[] /*!*/ sourceFileExtensions)
        {
            Debug.Assert(fullPath == null || RubyUtils.GetExtension(path) == RubyUtils.GetExtension(fullPath));

            IEnumerable <string> appendExtensions = (flags & LoadFlags.AppendExtensions) != 0 && RubyUtils.GetExtension(path).Length == 0 ?
                                                    sourceFileExtensions.Concat(_LibraryExtensions) : null;

            return((flags & LoadFlags.LoadOnce) != 0 && IsFileLoaded(path, fullPath, appendExtensions));
        }
Example #4
0
        /// <summary>
        /// Appends extensions if applicable.
        /// </summary>
        private ResolvedFile ResolveFile(string /*!*/ path, string /*!*/ extension, bool appendExtensions, string[] /*!*/ knownExtensions)
        {
            Debug.Assert(RubyUtils.GetExtension(path) == extension);

            string expandedPath = RubyUtils.ExpandPath(_context.Platform, path);

            // MRI doesn't load file w/o .rb extension:
            if (IsKnownExtension(extension, knownExtensions))
            {
                return(GetSourceUnit(path, expandedPath, extension, false));
            }

            if (_LibraryExtensions.IndexOf(extension, DlrConfiguration.FileExtensionComparer) != -1)
            {
                if (Platform.FileExists(expandedPath))
                {
                    return(new ResolvedFile(expandedPath, null));
                }
            }
            else if (!appendExtensions)
            {
                return(GetSourceUnit(path, expandedPath, extension, false));
            }

            if (appendExtensions)
            {
                List <string> matchingExtensions = GetExtensionsOfExistingFiles(expandedPath, knownExtensions);

                if (matchingExtensions.Count == 1)
                {
                    return(GetSourceUnit(path + matchingExtensions[0], expandedPath + matchingExtensions[0], matchingExtensions[0], true));
                }
                else if (matchingExtensions.Count > 1)
                {
                    Exception e = new AmbiguousFileNameException(expandedPath + matchingExtensions[0], expandedPath + matchingExtensions[1]);
                    throw RubyExceptions.CreateLoadError(e);
                }

                foreach (string libExtension in _LibraryExtensions)
                {
                    if (Platform.FileExists(expandedPath + libExtension))
                    {
                        return(new ResolvedFile(expandedPath + libExtension, libExtension));
                    }
                }
            }

            return(null);
        }
Example #5
0
 private bool AlreadyLoaded(string /*!*/ path, string fullPath, LoadFlags flags, string[] /*!*/ sourceFileExtensions)
 {
     Debug.Assert(fullPath == null || RubyUtils.GetExtension(path) == RubyUtils.GetExtension(fullPath));
     return((flags & LoadFlags.LoadOnce) != 0 && AnyFileLoaded(GetPathsToTestLoaded(path, fullPath, flags, sourceFileExtensions)));
 }
Example #6
0
        private ResolvedFile FindFile(string /*!*/ path, bool appendExtensions, string[] sourceFileExtensions)
        {
            Assert.NotNull(path);
            bool   isAbsolutePath;
            string extension;
            string home = null;

#if !SILVERLIGHT
            if (path.StartsWith("~/", StringComparison.Ordinal) || path.StartsWith("~\\", StringComparison.Ordinal))
            {
                try {
                    home = Environment.GetEnvironmentVariable("HOME");
                } catch (SecurityException) {
                    home = null;
                }

                if (home == null)
                {
                    throw RubyExceptions.CreateArgumentError(String.Format("couldn't find HOME environment -- expanding `{0}'", path));
                }
            }
#endif
            try {
                if (home != null)
                {
                    path = RubyUtils.CombinePaths(home, path.Substring(2));
                }

                isAbsolutePath = Platform.IsAbsolutePath(path);
                extension      = RubyUtils.GetExtension(path);
            } catch (ArgumentException e) {
                throw RubyExceptions.CreateLoadError(e);
            }

            // Absolute path -> load paths not consulted.
            if (isAbsolutePath)
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            string[] loadPaths = GetLoadPathStrings();

            if (loadPaths.Length == 0)
            {
                return(null);
            }

            // If load paths are non-empty and the path starts with .\ or ..\ then MRI also ignores the load paths.
            if (path.StartsWith("./", StringComparison.Ordinal) ||
                path.StartsWith("../", StringComparison.Ordinal) ||
                path.StartsWith(".\\", StringComparison.Ordinal) ||
                path.StartsWith("..\\", StringComparison.Ordinal))
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            foreach (var dir in loadPaths)
            {
                try {
                    ResolvedFile result = ResolveFile(RubyUtils.CombinePaths(dir, path), extension, appendExtensions, sourceFileExtensions);
                    if (result != null)
                    {
                        return(result);
                    }
                } catch (ArgumentException) {
                    // invalid characters in path
                }
            }

            return(null);
        }