Beispiel #1
0
        public static TConfig LoadConfig <TConfig>(string path)
            where TConfig : new()
        {
            if (path.StartsWith("~/"))
            {
                path = HostingEnvironment.MapPath(path);
            }

            if (File.Exists(path))
            {
                using (var sr = new StreamReader(path))
                {
                    string json = JsConfigHelper.RemovePrefix(sr.ReadToEnd()) ?? "{}";
                    return(JsonConvert.DeserializeObject <TConfig>(json, JsonSettings.Tolerant));
                }
            }

            return(new TConfig());
        }
Beispiel #2
0
        public static void Initialize()
        {
            if (isInitialized)
            {
                return;
            }

            isInitialized        = true;
            isEnabled            = false;
            bundleKeyBySourceUrl = null;
            bundleByKey          = null;
            try
            {
                var settings = JsonConvert.DeserializeObject <ScriptBundlingSettings>(
                    ConfigurationManager.AppSettings["ScriptBundling"].TrimToNull() ?? "{}", JsonSettings.Tolerant);

                if (settings == null ||
                    settings.Enabled != true)
                {
                    return;
                }

                if (settings.Bundles == null ||
                    settings.Bundles.Count == 0)
                {
                    settings.Bundles = JsConfigHelper.LoadConfig <Dictionary <string, string[]> >(
                        "~/Scripts/Site/ScriptBundles.js");

                    if (settings.Bundles == null ||
                        settings.Bundles.Count == 0)
                    {
                        return;
                    }
                }

                var  bundleKeyBySourceUrlNew = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                var  bundleByKeyNew          = new Dictionary <string, ConcatenatedScript>(StringComparer.OrdinalIgnoreCase);
                bool minimize = settings.Minimize == true;

                foreach (var pair in settings.Bundles)
                {
                    var sourceFiles = pair.Value;
                    if (sourceFiles == null ||
                        sourceFiles.Length == 0)
                    {
                        continue;
                    }

                    var bundleKey   = pair.Key;
                    var bundleName  = "Bundle." + bundleKey;
                    var bundleParts = new List <Func <string> >();

                    foreach (var sourceFile in sourceFiles)
                    {
                        if (sourceFile.IsNullOrEmpty())
                        {
                            continue;
                        }

                        string sourceUrl =
                            UrlHelper.ResolveUrl(sourceFile);
                        if (sourceUrl.IsNullOrEmpty())
                        {
                            continue;
                        }

                        bundleKeyBySourceUrlNew[sourceUrl] = bundleKey;

                        bundleParts.Add(() =>
                        {
                            if (HttpContext.Current == null)
                            {
                                return(String.Format(errorLines, "Tried to generate script while HttpContext is null"));
                            }

                            var sourcePath = HttpContext.Current.Server.MapPath(sourceUrl);
                            if (!File.Exists(sourcePath))
                            {
                                return(String.Format(errorLines, String.Format("File {0} is not found!", sourcePath)));
                            }

                            if (minimize)
                            {
                                var minPath = Path.ChangeExtension(sourcePath, ".min.js");
                                if (File.Exists(minPath))
                                {
                                    sourcePath = minPath;
                                }
                            }

                            using (StreamReader sr = new StreamReader(sourcePath))
                                return(sr.ReadToEnd());
                        });
                    }

                    var bundle = new ConcatenatedScript(bundleParts);
                    bundle.Minimize = settings.Minimize == true;
                    DynamicScriptManager.Register(bundleName, bundle);
                    bundleByKeyNew[bundleKey] = bundle;
                }

                bundleKeyBySourceUrl = bundleKeyBySourceUrlNew;
                bundleByKey          = bundleByKeyNew;
                isEnabled            = true;
            }
            catch (Exception ex)
            {
                ex.Log();
            }
        }