public void LoadCode(IncludeScript script)
 {
     using (var httpClient = new HttpClient())
     {
         script.Code = httpClient.GetStringAsync(script.Uri).Result;
     }
 }
Esempio n. 2
0
        public bool ShouldUse(IncludeScript script)
        {
            try
            {
                if (!string.IsNullOrEmpty(script.Code) || script.RequiredPackage == null ||
                    script.RequiredPackage.RequiredPackageType != RequiredPackageType.EmbeddedFile)
                {
                    return(false);
                }

                if (!script.RequiredPackage.ScriptUri.Equals(script.Uri))
                {
                    return(false);
                }

                if (script.Uri.Contains("/") || script.Uri.Contains("\\"))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public bool ShouldUse(IncludeScript script)
        {
            try
            {
                if (!string.IsNullOrEmpty(script.Code))
                {
                    return(false);
                }

                if (Uri.IsWellFormedUriString(script.Uri, UriKind.RelativeOrAbsolute))
                {
                    var uri = new Uri(script.Uri);

                    if (!uri.IsFile)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
 public void LoadCode(IncludeScript script)
 {
     using (var reader = File.OpenText(script.Uri))
     {
         script.Code = reader.ReadToEnd();
     }
 }
        public bool ShouldUse(IncludeScript script)
        {
            try
            {
                if (!string.IsNullOrEmpty(script.Code) || string.IsNullOrEmpty(script.Uri))
                {
                    return(false);
                }

                if (script.RequiredPackage != null &&
                    !script.RequiredPackage.RequiredPackageType.Equals(RequiredPackageType.Default))
                {
                    return(false);
                }

                if (!File.Exists(script.Uri))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
 public void LoadCode(IncludeScript script)
 {
     using (var httpClient = new HttpClient())
     {
         script.Code = httpClient.GetStringAsync(script.Uri).Result;
     }
 }
 public void LoadCode(IncludeScript script)
 {
     using (var reader = File.OpenText(script.Uri))
     {
         script.Code = reader.ReadToEnd();
     }
 }
Esempio n. 8
0
 public void LoadCode(IncludeScript script)
 {
     script.Code = script.RequiredPackage.GetEmbeddedAsset(script.RequiredPackage.ScriptUri);
     if (string.IsNullOrEmpty(script.Code))
     {
         throw new FileNotFoundException("Embedded Resource not found or content is empty : " + script.RequiredPackage.ScriptUri);
     }
 }
        public bool ShouldUse(IncludeScript script)
        {
            if (!string.IsNullOrEmpty(script.Code))
                return false;

            if (Uri.IsWellFormedUriString(script.Uri, UriKind.RelativeOrAbsolute))
            {
                var uri = new Uri(script.Uri);
                if (!uri.IsFile)
                    return true;
            }

            return false;
        }
Esempio n. 10
0
        public bool ShouldUse(IncludeScript script)
        {
            try
            {
                if (!string.IsNullOrEmpty(script.Code) || string.IsNullOrEmpty(script.Uri))
                {
                    return(false);
                }

                if (!script.Uri.ToLower().EndsWith(".dll"))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Uses the manager to load a script.
        /// </summary>
        /// <param name="script">The script to load.</param>
        public static void LoadScript(this IncludeScript script)
        {
            foreach (var scriptLoader in _loaders)
            {
                if (scriptLoader.ShouldUse(script))
                {
                    try {
                        scriptLoader.LoadCode(script);
                        return;
                    }
                    catch (Exception ex)
                    {
                        throw new ArgumentException(string.Format("{0} failed to load script with Script ID:{1} and Script Uri:{2}. Check InnerException for more info.",
                                                                  scriptLoader.Name, script.ScriptId, script.Uri), ex);
                    }
                }
            }

            throw new ArgumentException(
                      string.Format("The provided script could not be loaded with any of the available script loaders. Script ID:{0}  Script Uri:{1}.", script.ScriptId, script.Uri));
        }
Esempio n. 12
0
        public bool ShouldUse(IncludeScript script)
        {
            if (!string.IsNullOrEmpty(script.Code))
            {
                return(false);
            }

            if (!script.Uri.ToLower().StartsWith("http://") && !script.Uri.ToLower().StartsWith("https://"))
            {
                return(false);
            }

            if (Uri.IsWellFormedUriString(script.Uri, UriKind.RelativeOrAbsolute))
            {
                var uri = new Uri(script.Uri);
                if (!uri.IsFile)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 13
0
        public void LoadCode(IncludeScript script)
        {
            var asm = Assembly.LoadFrom(script.Uri);

            script.Exports = new HostTypeCollection(asm);
        }
Esempio n. 14
0
        /// <summary>
        /// Called via a javascript to require and return the requested package.
        /// </summary>
        /// <param name="packageId">ID of the RequirePackage to require.</param>
        /// <param name="scriptUri">A script uri.  This is only needed if the packageId doesn't meet the script convention name and the package is not a registered package.</param>
        /// <returns>The return object to use for the require. Either the export from the require script or the returned HostObject if not script is present.</returns>
        public object Require(string packageId, string scriptUri)
        {
            RequiredPackage package;
            bool            hasUri         = !String.IsNullOrEmpty(scriptUri);
            bool            packageCreated = false;

            if (packageId.StartsWith("/"))
            {
                packageId = "." + packageId;
            }
            if (packageId.Contains("/") || packageId.Contains("\\"))
            {
                //http 和 file 类型的都会进来
                if (!hasUri)
                {
                    scriptUri = packageId;
                    hasUri    = true;
                }

                //拿到真正的地址
                var real = GetRealFilePath(scriptUri);

                packageId = real.PackageId;
                scriptUri = real.NativeRequirePath;


                if (packageId.Length == 0)
                {
                    throw new ArgumentException(
                              "The provided packageId is not a valid package name. The packageId must be a valid file path or uri if path characters are contained in the name.");
                }
            }

            if (!RequireManager.TryGetPackage(packageId, out package))
            {
                if (!hasUri)
                {
                    throw new KeyNotFoundException(String.Format("The package with ID {0} was not found, did you register this package?", packageId));
                }

                package = new RequiredPackage {
                    PackageId = packageId, ScriptUri = scriptUri
                };
                packageCreated = true;
            }

            if (package.Exports != null)
            {
                return(package.Exports);
            }

            var options = new ExecutionOptions
            {
                HostObjects = package.HostObjects,
                HostTypes   = package.HostTypes
            };

            Engine.ApplyOptions(options);

            if (!String.IsNullOrEmpty(package.ScriptUri))
            {
                var includScript = new IncludeScript
                {
                    Uri             = package.ScriptUri,
                    PrependCode     = "var " + package.PackageId + " = {};",
                    RequiredPackage = package
                };

                var compiledScript = Compiler.Compile(includScript);

                if (compiledScript == null && includScript.Exports != null)//DLL场景
                {
                    if (packageCreated)
                    {
                        package.Exports = includScript.Exports;
                        RequireManager.RegisterPackage(package);
                    }
                    _LastRequireDic.TryRemove(_lastRequireIdex, out string _aa);
                    _lastRequireIdex--;
                    return(package.Exports);
                }

                Engine.Execute(compiledScript);

                var outputObject = DynamicExtensions.GetProperty(Engine.Script, package.PackageId);

                if (outputObject is Undefined)
                {
                    //try to find pascal case if camel is not present.
                    outputObject = DynamicExtensions.GetProperty(Engine.Script, package.PackageId.ToPascalCase());
                }

                if (packageCreated)
                {
                    RequireManager.RegisterPackage(package);
                }
                if (_lastRequireIdex > 0)
                {
                    _LastRequireDic.TryRemove(_lastRequireIdex, out string _a);
                    _lastRequireIdex--;
                }

                return(outputObject.exports);
            }

            if (options.HostObjects.SafeAny())
            {
                return(options.HostObjects[0].Target);
            }

            return(null);
        }