/// <summary> /// Try to get the package from the registered packages. /// </summary> /// <param name="packageId">Id of the package to retrieve.</param> /// <param name="package">Package to return.</param> /// <returns>Bool indicating if package was found.</returns> public static bool TryGetPackage(string packageId, out RequiredPackage package) { return _packages.TryGetValue(packageId, out package); }
/// <summary> /// Register a packaged for potential requirement. /// </summary> /// <param name="package">The package to register.</param> public static void RegisterPackage(RequiredPackage package) { _packages.TryAdd(package.PackageId, package); }
/// <summary> /// Try to get the package from the registered packages. /// </summary> /// <param name="packageId">Id of the package to retrieve.</param> /// <param name="package">Package to return.</param> /// <returns>Bool indicating if package was found.</returns> public static bool TryGetPackage(string packageId, out RequiredPackage package) { return(_packages.TryGetValue(packageId, out package)); }
/// <summary> /// Register a packaged for potential requirement. /// </summary> /// <param name="package">The package to register.</param> public static void RegisterPackage(RequiredPackage package) { _packages.TryAdd(package.PackageId, package); }
/// <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.Contains("/") || packageId.Contains("\\")) { if (!hasUri) { scriptUri = packageId; hasUri = true; } packageId = DerivePackageIdFromUri(scriptUri); 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; } var options = new ExecutionOptions { HostObjects = package.HostObjects, HostTypes = package.HostTypes }; Engine.ApplyOptions(options); if (!String.IsNullOrEmpty(package.ScriptUri)) { var compiledScript = Compiler.Compile(new IncludeScript {Uri = package.ScriptUri, PrependCode = "var " + packageId + " = {};"}); Engine.Execute(compiledScript); var outputObject = DynamicExtensions.GetProperty(Engine.Script, packageId); if (outputObject is Undefined) { //try to find pascal case if camel is not present. outputObject = DynamicExtensions.GetProperty(Engine.Script, packageId.ToPascalCase()); } if (packageCreated) { RequireManager.RegisterPackage(package); } return outputObject.exports; } if (options.HostObjects.SafeAny()) return options.HostObjects[0]; return null; }
/// <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.Contains("/") || packageId.Contains("\\")) { if (!hasUri) { scriptUri = packageId; hasUri = true; } packageId = DerivePackageIdFromUri(scriptUri); 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; } var options = new ExecutionOptions { HostObjects = package.HostObjects, HostTypes = package.HostTypes }; Engine.ApplyOptions(options); if (!String.IsNullOrEmpty(package.ScriptUri)) { var compiledScript = Compiler.Compile(new IncludeScript { Uri = package.ScriptUri, PrependCode = "var " + packageId + " = {};" }); Engine.Execute(compiledScript); var outputObject = DynamicExtensions.GetProperty(Engine.Script, packageId); if (outputObject is Undefined) { //try to find pascal case if camel is not present. outputObject = DynamicExtensions.GetProperty(Engine.Script, packageId.ToPascalCase()); } if (packageCreated) { RequireManager.RegisterPackage(package); } return(outputObject.exports); } if (options.HostObjects.SafeAny()) { return(options.HostObjects[0]); } return(null); }