예제 #1
0
        /// <summary>
        /// Load all themes
        /// </summary>
        /// <param name="loadOnlyInstalledModules"></param>
        /// <returns></returns>
        public virtual IList <ThemeInfo> LoadThemes()
        {
            if (_themes == null)
            {
                using (new WriteLockDisposable(_locker))
                {
                    //load all theme descriptors
                    _fileProvider.CreateDirectory(_fileProvider.MapPath(ThemesPath));
                    var themeFolder = new DirectoryInfo(_fileProvider.MapPath(ThemesPath));
                    _themes = new List <ThemeInfo>();
                    foreach (var themeInfoFile in themeFolder.GetFiles(ThemeInfoFileName, SearchOption.AllDirectories))
                    {
                        var text = File.ReadAllText(themeInfoFile.FullName);
                        if (string.IsNullOrEmpty(text))
                        {
                            continue;
                        }

                        //deserialize module information file to ModuleInfo object
                        var themeInfo = JsonConvert.DeserializeObject <ThemeInfo>(File.ReadAllText(themeInfoFile.FullName));

                        //validation
                        if (string.IsNullOrEmpty(themeInfo?.ThemeName))
                        {
                            throw new Exception($"A theme info '{themeInfoFile.FullName}' has no system name");
                        }

                        _themes.Add(themeInfo);
                    }
                }
            }

            return(_themes);
        }
예제 #2
0
 /// <summary>
 /// Try to write web.config file
 /// </summary>
 /// <returns></returns>
 protected virtual bool TryWriteWebConfig()
 {
     try
     {
         File.SetLastWriteTimeUtc(_fileProvider.MapPath("~/web.config"), DateTime.UtcNow);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #3
0
        /// <summary>
        /// Generate all script parts
        /// </summary>
        /// <param name="urlHelper">URL Helper</param>
        /// <param name="location">A location of the script element</param>
        /// <param name="bundleFiles">A value indicating whether to bundle script elements</param>
        /// <returns>Generated string</returns>
        public virtual string RenderScripts(IUrlHelper urlHelper, ResourceLocation location)
        {
            if (!_scriptParts.ContainsKey(location) || _scriptParts[location] == null)
            {
                return("");
            }

            if (!_scriptParts.Any())
            {
                return("");
            }

            if (_seoSettings.EnableJsBundling)
            {
                var partsToBundle = _scriptParts[location]
                                    .Where(x => !x.ExcludeFromBundle)
                                    .Distinct()
                                    .ToArray();

                var partsToDontBundle = _scriptParts[location]
                                        .Where(x => x.ExcludeFromBundle)
                                        .Distinct()
                                        .ToArray();

                var result = new StringBuilder();

                //parts to  bundle
                if (partsToBundle.Any())
                {
                    //ensure \bundles directory exists
                    _fileProvider.CreateDirectory(_fileProvider.GetAbsolutePath("bundles"));

                    var bundle = new Bundle();
                    foreach (var item in partsToBundle)
                    {
                        new PathString(urlHelper.Content(item.Src))
                        .StartsWithSegments(urlHelper.ActionContext.HttpContext.Request.PathBase, out PathString path);
                        var src = path.Value.TrimStart('/');

                        //check whether this file exists, if not it should be stored into /wwwroot directory
                        if (!_fileProvider.FileExists(_fileProvider.MapPath(path)))
                        {
                            src = $"wwwroot/{src}";
                        }

                        bundle.InputFiles.Add(src);
                    }
                    //output file
                    var outputFileName = GetBundleFileName(partsToBundle.Select(x => x.Src).ToArray());
                    bundle.OutputFileName = "wwwroot/bundles/" + outputFileName + ".js";
                    //save
                    var configFilePath = _hostingEnvironment.ContentRootPath + "\\" + outputFileName + ".json";
                    bundle.FileName = configFilePath;
                    lock (s_lock)
                    {
                        //performance optimization. do not bundle and minify for each HTTP request
                        //we periodically re-check already bundles file
                        //so if we have minification enabled, it could take up to several minutes to see changes in updated resource files (or just reset the cache or restart the site)
                        var cacheKey      = $"minification.shouldrebuild.scripts-{outputFileName}";
                        var shouldRebuild = _cacheManager.Get(cacheKey, RecheckBundledFilesPeriod, () => true);
                        if (shouldRebuild)
                        {
                            //process
                            _processor.Process(configFilePath, new List <Bundle> {
                                bundle
                            });
                            _cacheManager.Set(cacheKey, false, RecheckBundledFilesPeriod);
                        }
                    }

                    //render
                    result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content("~/bundles/" + outputFileName + ".min.js"));
                    result.Append(Environment.NewLine);
                }

                //parts to not bundle
                foreach (var item in partsToDontBundle)
                {
                    result.AppendFormat("<script {1}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : "");
                    result.Append(Environment.NewLine);
                }

                return(result.ToString());
            }
            else
            {
                //bundling is disabled
                var result = new StringBuilder();
                foreach (var item in _scriptParts[location].Distinct())
                {
                    result.AppendFormat("<script {1}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : "");
                    result.Append(Environment.NewLine);
                }

                return(result.ToString());
            }
        }