Ejemplo n.º 1
0
        public void Define(object bundleName, object scriptReferences, object bundleDependencies, object bundleDescription, object run)
        {
            var strBundleName = TypeConverter.ToString(bundleName);

            if (bundleName == Undefined.Value || bundleName == Null.Value || strBundleName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "A unique bundle name or type name must be specified as the first argument.");
            }


            if ((scriptReferences == Undefined.Value || scriptReferences == null) &&
                (bundleDependencies == Undefined.Value || bundleDependencies == null) &&
                (bundleDescription == Undefined.Value || bundleDescription == null))
            {
                var strTypeName = TypeConverter.ToString(bundleName);

                var type = Type.GetType(strTypeName, false, true);
                if (type == null)
                {
                    throw new JavaScriptException(Engine, "Error", string.Format("The specified type could not be located. {0}", strTypeName));
                }

                var bundle = Activator.CreateInstance(type) as IBundle;
                if (bundle == null)
                {
                    throw new JavaScriptException(Engine, "Error", string.Format("The specified type did not implement the IBundle interface. {0}", strTypeName));
                }

                RegisterBundle(bundle);
                return;
            }

            var existingBundle = GetBundle(strBundleName, null);

            if (existingBundle != null)
            {
                throw new JavaScriptException(Engine, "Error", string.Format("Unable to define bundle: A bundle with the specified name has already been registered. {0}", strBundleName));
            }

            var scriptBundle = new ScriptBundle
            {
                BundleName = strBundleName,
            };

            if (bundleDescription != Null.Value && bundleDescription != Undefined.Value)
            {
                scriptBundle.BundleDescription = TypeConverter.ToString(bundleDescription);
            }

            //Add dependencies, first checking the dependency exists, throw if one doesn't.
            var instance = bundleDependencies as ArrayInstance;

            if (instance != null)
            {
                var arrDependencies = instance;
                var i = 0;
                foreach (var dependency in arrDependencies.ElementValues)
                {
                    var strDependency      = TypeConverter.ToString(dependency);
                    var existingDependency = GetBundle(strDependency, null);

                    if (existingDependency != null)
                    {
                        throw new JavaScriptException(Engine, "Error",
                                                      string.Format("The specified dependency does not exist: {0}", strDependency));
                    }

                    scriptBundle.AddDependency(strDependency, i.ToString(CultureInfo.InvariantCulture));
                    i++;
                }
            }
            else
            {
                var dependencies = bundleDependencies as ObjectInstance;
                if (dependencies != null)
                {
                    var objDependencies = dependencies;
                    foreach (var property in objDependencies.Properties)
                    {
                        var existingDependency = GetBundle(property.Name, null);
                        if (existingDependency == null)
                        {
                            throw new JavaScriptException(Engine, "Error",
                                                          string.Format("The specified dependency does not exist: {0}", property.Name));
                        }

                        scriptBundle.AddDependency(property.Name, TypeConverter.ToString(property.Value));
                    }
                }
                else if (bundleDependencies != Null.Value && bundleDependencies != Undefined.Value && bundleDependencies != null)
                {
                    var strDependency      = TypeConverter.ToString(bundleDependencies);
                    var existingDependency = GetBundle(strDependency, null);
                    if (existingDependency == null)
                    {
                        throw new JavaScriptException(Engine, "Error",
                                                      string.Format("The specified dependency does not exist: {0}", strDependency));
                    }

                    scriptBundle.AddDependency(strDependency, "0");
                }
            }

            //No need to check existance -- an advanced scenario might be that a script creates another script and only includes it when the bundle is required.
            var references = scriptReferences as ArrayInstance;

            if (references != null)
            {
                var arrScriptReferences = references;
                foreach (var scriptReference in arrScriptReferences.ElementValues)
                {
                    var strScriptReference = TypeConverter.ToString(scriptReference);
                    if (scriptReference == Null.Value || scriptReference == Undefined.Value || strScriptReference.IsNullOrWhiteSpace())
                    {
                        throw new JavaScriptException(Engine, "Error", string.Format("Script References must not contain empty strings. ({0})", strBundleName));
                    }

                    scriptBundle.AddScriptReference(strScriptReference);
                }
            }
            else
            {
                var function = scriptReferences as FunctionInstance;
                if (function != null)
                {
                    scriptBundle.ScriptFunction = function;
                }
                else
                {
                    var strScriptReference = TypeConverter.ToString(scriptReferences);
                    if (scriptReferences == Null.Value || scriptReferences == Undefined.Value || strScriptReference.IsNullOrWhiteSpace())
                    {
                        throw new JavaScriptException(Engine, "Error",
                                                      string.Format("At least one script reference must be defined in order to define a bundle. ({0})", strBundleName));
                    }

                    if (run != null && run != Null.Value && run != Undefined.Value && TypeConverter.ToBoolean(run))
                    {
                        var result = Engine.Evaluate("include('" + strScriptReference + "');");
                        if (result is FunctionInstance)
                        {
                            scriptBundle.ScriptFunction = result as FunctionInstance;
                        }
                        else
                        {
                            throw new JavaScriptException(Engine, "Error",
                                                          string.Format("When specifying a to run the script immediately, the eval of the specified script reference should only return a function. ({0})", strBundleName));
                        }
                    }

                    scriptBundle.AddScriptReference(strScriptReference);
                }
            }

            RegisterBundle(scriptBundle);
        }