Example #1
0
        /// <summary>
        /// Loads the module of the given name using the host provided semantics.
        ///
        /// The default semantics are to search the host path for a file of the specified
        /// name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns>A valid SourceUnit or null no module could be found.</returns>
        /// <exception cref="System.InvalidOperationException">An ambigious module match has occured</exception>
        public virtual SourceUnit ResolveSourceFileUnit(string name)
        {
            Contract.RequiresNotNull(name, "name");

            SourceUnit result = null;

            foreach (string directory in SourceUnitResolutionPath)
            {
                string finalPath = null;

                foreach (string extension in _environment.GetRegisteredFileExtensions())
                {
                    string fullPath = Path.Combine(directory, name + extension);

                    if (ScriptDomainManager.CurrentManager.PAL.FileExists(fullPath))
                    {
                        if (result != null)
                        {
                            throw new InvalidOperationException(String.Format(Resources.AmbigiousModule, fullPath, finalPath));
                        }

                        LanguageProvider provider;
                        if (!ScriptDomainManager.CurrentManager.TryGetLanguageProviderByFileExtension(extension, out provider))
                        {
                            // provider may have been unregistered, let's pick another one:
                            continue;
                        }

                        result    = SourceUnit.CreateFileUnit(provider.GetEngine(), NormalizePath(fullPath), Encoding.Default);
                        finalPath = fullPath;
                    }
                }
            }

            return(result);
        }