/// <summary>
        /// Registers a style sheet file.
        /// <seealso cref="IResourceUrlFactory"/>
        /// </summary>
        /// <remarks>
        ///   <para>
        ///     All calls to <see cref="RegisterStylesheetLink(string, string)"/> must be completed before
        ///     <see cref="SetAppended"/> is called. (Typically during the <c>Render</c> phase.)
        ///   </para><para>
        ///     Registeres the javascript file with a default priority of Page.
        ///   </para>
        /// </remarks>
        /// <param name="key">
        ///   The unique key identifying the stylesheet file in the headers collection. Must not be <see langword="null"/> or empty.
        /// </param>
        /// <param name="url">
        /// The url of the stylesheet file. Must not be <see langword="null"/>.
        /// </param>
        /// <exception cref="InvalidOperationException">
        ///   Thrown if method is called after <see cref="SetAppended"/> has executed.
        /// </exception>
        public void RegisterStylesheetLink(string key, IResourceUrl url)
        {
            ArgumentUtility.CheckNotNullOrEmpty("key", key);
            ArgumentUtility.CheckNotNull("url", url);

            RegisterStylesheetLink(key, url, Priority.Page);
        }
        /// <summary>
        /// Registers a javascript file.
        /// <seealso cref="IResourceUrlFactory"/>
        /// </summary>
        /// <remarks>
        ///   <para>
        ///     All calls to <see cref="RegisterJavaScriptInclude(string,Remotion.Web.IResourceUrl)"/> must be completed before
        ///     <see cref="SetAppended"/> is called. (Typically during the <c>Render</c> phase.)
        ///   </para><para>
        ///     Registeres the javascript file with a default priority of Page.
        ///   </para>
        /// </remarks>
        /// <param name="key">
        ///   The unique key identifying the javascript file in the headers collection. Must not be <see langword="null"/> or empty.
        /// </param>
        /// <param name="url">
        ///   The url of the javascript file. Must not be <see langword="null"/>.
        /// </param>
        /// <exception cref="InvalidOperationException">
        ///   Thrown if method is called after <see cref="SetAppended"/> has executed.
        /// </exception>
        public void RegisterJavaScriptInclude(string key, IResourceUrl url)
        {
            ArgumentUtility.CheckNotNullOrEmpty("key", key);
            ArgumentUtility.CheckNotNull("url", url);

            RegisterHeadElement(key, new JavaScriptInclude(url), Priority.Script);
        }
        /// <summary>
        /// Registers a style sheet file.
        /// <seealso cref="IResourceUrlFactory"/>
        /// </summary>
        /// <remarks>
        ///   All calls to <see cref="RegisterStylesheetLink(string, string, Priority)"/> must be completed before
        ///   <see cref="SetAppended"/> is called. (Typically during the <c>Render</c> phase.)
        /// </remarks>
        /// <param name="key">
        ///   The unique key identifying the stylesheet file in the headers collection. Must not be <see langword="null"/> or empty.
        /// </param>
        /// <param name="url"> The url of the stylesheet file. Must not be <see langword="null"/>. </param>
        /// <param name="priority">
        ///   The priority level of the head element. Elements are rendered in the following order:
        ///   Library, UserControl, Page.
        /// </param>
        /// <exception cref="InvalidOperationException">
        ///   Thrown if method is called after <see cref="SetAppended"/> has executed.
        /// </exception>
        public void RegisterStylesheetLink(string key, IResourceUrl url, Priority priority)
        {
            ArgumentUtility.CheckNotNullOrEmpty("key", key);
            ArgumentUtility.CheckNotNull("url", url);

            RegisterHeadElement(key, new StyleSheetImportRule(url), priority);
        }
        public void AddResourcesDic(string key, IResourceUrl resourceUrl, bool isAlias)
        {
            var resource = ResourcesDic;

            resourceUrl.IsAlias = isAlias;
            resource.AddOrUpdate(key, resourceUrl, (tKey, existingVal) => resourceUrl);
        }
 public System.IO.Stream GetManifestResourceStream(IResourceUrl resourceUrl)
 {
     using (var wacth = new CodeWatch("GetManifestResourceStream", 1000, new Action<string, LoggerStrategyBase, int?, long>((tag, currentLog, wcount, execms) => currentLog.LogFormat(LoggerLevels.Warn, "\t{0}:资源({3})请求时间为({1})ms.已超过阀值({2})ms.", tag, execms, wcount, resourceUrl.ManifestResourceName))))
     {
         if (!PlusAssemblysList.Any(p => p.Assembly.FullName == resourceUrl.AssemblyFullName))
             throw new Exception("此插件已移除或者不存在。");
         var ass = PlusAssemblysList.First(p => p.Assembly.FullName == resourceUrl.AssemblyFullName);
         return ass.Assembly.GetManifestResourceStream(resourceUrl.ManifestResourceName);
     }
 }
        public void Render()
        {
            IResourceUrl resourceUrl = MockRepository.GenerateStub <IResourceUrl>();

            resourceUrl.Stub(stub => stub.GetUrl()).Return("myStylesheetUrl.js");

            var javaScriptInclude = new StyleSheetImportRule(resourceUrl);

            javaScriptInclude.Render(_htmlHelper.Writer);

            Assert.That(_htmlHelper.GetDocumentText(), Is.EqualTo("@import url(\"myStylesheetUrl.js\");\r\n"));
        }
 public System.IO.Stream GetManifestResourceStream(IResourceUrl resourceUrl)
 {
     using (var wacth = new CodeWatch("GetManifestResourceStream", 1000, new Action <string, LoggerStrategyBase, int?, long>((tag, currentLog, wcount, execms) => currentLog.LogFormat(LoggerLevels.Warn, "\t{0}:资源({3})请求时间为({1})ms.已超过阀值({2})ms.", tag, execms, wcount, resourceUrl.ManifestResourceName))))
     {
         if (!PlusAssemblysList.Any(p => p.Assembly.FullName == resourceUrl.AssemblyFullName))
         {
             throw new Exception("此插件已移除或者不存在。");
         }
         var ass = PlusAssemblysList.First(p => p.Assembly.FullName == resourceUrl.AssemblyFullName);
         return(ass.Assembly.GetManifestResourceStream(resourceUrl.ManifestResourceName));
     }
 }
Exemple #8
0
        public void Render()
        {
            IResourceUrl resourceUrl = MockRepository.GenerateStub <IResourceUrl>();

            resourceUrl.Stub(stub => stub.GetUrl()).Return("myScriptUrl.js");

            var javaScriptInclude = new JavaScriptInclude(resourceUrl);

            javaScriptInclude.Render(_htmlHelper.Writer);

            var document = _htmlHelper.GetResultDocument();
            var element  = _htmlHelper.GetAssertedChildElement(document, "script", 0);

            _htmlHelper.AssertAttribute(element, "type", "text/javascript");
            _htmlHelper.AssertAttribute(element, "src", "myScriptUrl.js");
        }
        /// <summary>
        /// 检查是否已将资源写入站点目录
        /// </summary>
        /// <param name="assDirPath"></param>
        /// <param name="fwAss"></param>
        /// <param name="resourceUrl"></param>
        /// <returns></returns>
        public bool CheckHasWrittenToSiteDir(string assDirPath, IPlusAssemblyInfo fwAss, ref IResourceUrl resourceUrl)
        {
            var childPath = resourceUrl.ManifestResourceName.Replace(fwAss.Name, string.Empty).Trim('.');
            var childDirs = childPath.Split('.');
            var fileName  = string.Empty;

            if (childDirs.Length >= 2)
            {
                childPath = childDirs.Where((str, index) => index != childDirs.Length - 1)
                            .Aggregate(assDirPath, Path.Combine);
                childPath.CreateDirectoryIfNotExist();
                fileName = string.Format("{0}.{1}", childDirs[childDirs.Length - 2], childDirs[childDirs.Length - 1]);
            }
            //如果没有父级命名空间
            if (childDirs.Length == 1)
            {
                fileName = childPath;
            }
            if (!fileName.IsNotEmpty())
            {
                return(true);
            }
            var filePath = Path.Combine(childPath, fileName);

            if (!File.Exists(filePath))
            {
                return(false);
            }
            resourceUrl.FileWriteTicks      = AssemblyManager.GetFileWriteTicks(filePath);
            resourceUrl.HasWrittenToSiteDir = true;
            resourceUrl.SiteRelativeUrl     = filePath.Replace(GlobalApplicationObject.Current.ApplicationContext.SitePaths.SiteRootDirPath, string.Empty)
                                              .Replace("\\", "/");
            return(false);
        }
        /// <summary>
        /// 将文件输出到站点目录
        /// </summary>
        /// <param name="assDirPath"></param>
        /// <param name="pluAssembly"></param>
        /// <param name="resourceUrl"></param>
        /// <param name="fwAss"></param>
        public void WriteResourceToSiteDir(string assDirPath, Assembly pluAssembly, ref IResourceUrl resourceUrl, IPlusAssemblyInfo fwAss)
        {
            assDirPath.CreateDirectoryIfNotExist();
            if (resourceUrl.ManifestResourceName.EndsWith(".config"))
            {
                resourceUrl.HasWrittenToSiteDir = false;
                return;
            }
            #region 根据命名空间创建目录
            var childPath = resourceUrl.ManifestResourceName.Replace(fwAss.Name, string.Empty).Trim('.');
            var childDirs = childPath.Split('.');
            var fileName  = string.Empty;
            if (childDirs.Length >= 2)
            {
                childPath = childDirs
                            .Where((str, index) =>
                                   index < childDirs.Length - 2)
                            .Aggregate(assDirPath, Path.Combine);
                childPath.CreateDirectoryIfNotExist();
                fileName = string.Format("{0}.{1}", childDirs[childDirs.Length - 2], childDirs[childDirs.Length - 1]);
            }
            #endregion

            //如果没有父级命名空间
            if (childDirs.Length == 1)
            {
                fileName = childPath;
            }
            if (!fileName.IsNotEmpty())
            {
                return;
            }
            var filePath = Path.Combine(childPath, fileName);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            //获取相对路径
            resourceUrl.SiteRelativeUrl = filePath.Replace(GlobalApplicationObject.Current.ApplicationContext.SitePaths.SiteRootDirPath, string.Empty)
                                          .Replace("\\", "/");

            #region 压缩Js和Css
            var ext = Path.GetExtension(fileName);
            if ((ext == ".js" || ext == ".css"))
            {
                var content = ManifestResourceManager.GetWebResourceAsString(pluAssembly, resourceUrl.ManifestResourceName);
                if (!resourceUrl.ManifestResourceName.ToLower().Contains(".min."))
                {
                    #region 判断是否压缩脚本或者样式文件
                    var configManager = GlobalApplicationObject.Current.ApplicationContext.ConfigManager;
                    var sysConfig     = configManager.GetConfig <SystemConfigInfo>();
                    if (ext == ".js" && sysConfig.IsMinJs)
                    {
                        content = MinHelper.MinJs(content);
                    }
                    else if (ext == ".css" && sysConfig.IsMinCss)
                    {
                        content = MinHelper.MinCss(content);
                    }
                    #endregion
                }
                //写文件
                File.WriteAllText(filePath, content, Encoding.UTF8);
            }
            else
            {
                using (var stream = ManifestResourceManager.GetManifestResourceStream(pluAssembly, resourceUrl.ManifestResourceName))
                {
                    var buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);  //将流的内容读到缓冲区
                    using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        fs.Write(buffer, 0, buffer.Length);
                        fs.Flush();
                    }
                }
            }
            #endregion
            resourceUrl.FileWriteTicks = AssemblyManager.GetFileWriteTicks(filePath);
        }
 public System.Reflection.Assembly GetAssemblyByResourceUrl(IResourceUrl resourceUrl)
 {
     return PlusAssemblysList.First(p => p.Assembly.FullName == resourceUrl.AssemblyFullName).Assembly;
 }
 public StyleSheetImportRule(IResourceUrl resourceUrl)
 {
     ArgumentUtility.CheckNotNull("resourceUrl", resourceUrl);
     _resourceUrl = resourceUrl;
 }
Exemple #13
0
        /// <summary>
        /// 将文件输出到站点目录
        /// </summary>
        /// <param name="assDirPath"></param>
        /// <param name="pluAssembly"></param>
        /// <param name="resourceUrl"></param>
        /// <param name="fwAss"></param>
        public void WriteResourceToSiteDir(string assDirPath, Assembly pluAssembly, ref IResourceUrl resourceUrl, IPlusAssemblyInfo fwAss)
        {
            assDirPath.CreateDirectoryIfNotExist();
            if (resourceUrl.ManifestResourceName.EndsWith(".config"))
            {
                resourceUrl.HasWrittenToSiteDir = false;
                return;
            }
            #region 根据命名空间创建目录
            var childPath = resourceUrl.ManifestResourceName.Replace(fwAss.Name, string.Empty).Trim('.');
            var childDirs = childPath.Split('.');
            var fileName = string.Empty;
            if (childDirs.Length >= 2)
            {
                childPath = childDirs
                    .Where((str, index) =>
                        index < childDirs.Length - 2)
                    .Aggregate(assDirPath, Path.Combine);
                childPath.CreateDirectoryIfNotExist();
                fileName = string.Format("{0}.{1}", childDirs[childDirs.Length - 2], childDirs[childDirs.Length - 1]);
            }
            #endregion

            //如果没有父级命名空间
            if (childDirs.Length == 1)
                fileName = childPath;
            if (!fileName.IsNotEmpty()) return;
            var filePath = Path.Combine(childPath, fileName);
            if (File.Exists(filePath)) File.Delete(filePath);
            //获取相对路径
            resourceUrl.SiteRelativeUrl = filePath.Replace(GlobalApplicationObject.Current.ApplicationContext.SitePaths.SiteRootDirPath, string.Empty)
                    .Replace("\\", "/");

            #region 压缩Js和Css
            var ext = Path.GetExtension(fileName);
            if ((ext == ".js" || ext == ".css"))
            {
                var content = ManifestResourceManager.GetWebResourceAsString(pluAssembly, resourceUrl.ManifestResourceName);
                if (!resourceUrl.ManifestResourceName.ToLower().Contains(".min."))
                {
                    #region 判断是否压缩脚本或者样式文件
                    var configManager = GlobalApplicationObject.Current.ApplicationContext.ConfigManager;
                    var sysConfig = configManager.GetConfig<SystemConfigInfo>();
                    if (ext == ".js" && sysConfig.IsMinJs)
                        content = MinHelper.MinJs(content);
                    else if (ext == ".css" && sysConfig.IsMinCss)
                        content = MinHelper.MinCss(content);
                    #endregion
                  
                }
                //写文件
                File.WriteAllText(filePath, content, Encoding.UTF8);
            }
            else
            {
                using (var stream = ManifestResourceManager.GetManifestResourceStream(pluAssembly, resourceUrl.ManifestResourceName))
                {
                    var buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);  //将流的内容读到缓冲区
                    using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        fs.Write(buffer, 0, buffer.Length);
                        fs.Flush();
                    }
                }
            }
            #endregion
            resourceUrl.FileWriteTicks = AssemblyManager.GetFileWriteTicks(filePath);
        }
Exemple #14
0
 /// <summary>
 /// 检查是否已将资源写入站点目录
 /// </summary>
 /// <param name="assDirPath"></param>
 /// <param name="fwAss"></param>
 /// <param name="resourceUrl"></param>
 /// <returns></returns>
 public bool CheckHasWrittenToSiteDir(string assDirPath, IPlusAssemblyInfo fwAss, ref IResourceUrl resourceUrl)
 {
     var childPath = resourceUrl.ManifestResourceName.Replace(fwAss.Name, string.Empty).Trim('.');
     var childDirs = childPath.Split('.');
     var fileName = string.Empty;
     if (childDirs.Length >= 2)
     {
         childPath = childDirs.Where((str, index) => index != childDirs.Length - 1)
             .Aggregate(assDirPath, Path.Combine);
         childPath.CreateDirectoryIfNotExist();
         fileName = string.Format("{0}.{1}", childDirs[childDirs.Length - 2], childDirs[childDirs.Length - 1]);
     }
     //如果没有父级命名空间
     if (childDirs.Length == 1)
         fileName = childPath;
     if (!fileName.IsNotEmpty()) return true;
     var filePath = Path.Combine(childPath, fileName);
     if (!File.Exists(filePath)) return false;
     resourceUrl.FileWriteTicks = AssemblyManager.GetFileWriteTicks(filePath);
     resourceUrl.HasWrittenToSiteDir = true;
     resourceUrl.SiteRelativeUrl = filePath.Replace(GlobalApplicationObject.Current.ApplicationContext.SitePaths.SiteRootDirPath, string.Empty)
             .Replace("\\", "/");
     return false;
 }
 public System.Reflection.Assembly GetAssemblyByResourceUrl(IResourceUrl resourceUrl)
 {
     return(PlusAssemblysList.First(p => p.Assembly.FullName == resourceUrl.AssemblyFullName).Assembly);
 }
        public JavaScriptInclude(IResourceUrl resourceUrl)
        {
            ArgumentUtility.CheckNotNull("resourceUrl", resourceUrl);

            _resourceUrl = resourceUrl;
        }
 public void AddResourcesDic(string key, IResourceUrl resourceUrl, bool isAlias)
 {
     var resource = ResourcesDic;
     resourceUrl.IsAlias = isAlias;
     resource.AddOrUpdate(key, resourceUrl, (tKey, existingVal) => resourceUrl);
 }