From 4deae439af2c87cb2bd9cc7723a03cf61cb0e350 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 2 Mar 2020 09:09:29 +0100 Subject: [PATCH] Convert base scripts and styles to embedded resources and move to VAR.WebForms.Common. --- VAR.Focus.Web/VAR.Focus.Web.csproj | 5 -- VAR.WebForms.Common/Code/Bundler.cs | 60 +++++++++++++++---- VAR.WebForms.Common/Code/ScriptsBundler.cs | 8 ++- VAR.WebForms.Common/Code/StylesBundler.cs | 8 ++- .../Scripts/01. Base.js | 0 .../Scripts/02. Ajax.js | 0 .../Scripts/03. Controls.js | 0 .../Styles/00. Reset.css | 0 .../Styles/01. base.css | 0 .../VAR.WebForms.Common.csproj | 7 +++ 10 files changed, 69 insertions(+), 19 deletions(-) rename {VAR.Focus.Web => VAR.WebForms.Common}/Scripts/01. Base.js (100%) rename {VAR.Focus.Web => VAR.WebForms.Common}/Scripts/02. Ajax.js (100%) rename {VAR.Focus.Web => VAR.WebForms.Common}/Scripts/03. Controls.js (100%) rename {VAR.Focus.Web => VAR.WebForms.Common}/Styles/00. Reset.css (100%) rename {VAR.Focus.Web => VAR.WebForms.Common}/Styles/01. base.css (100%) diff --git a/VAR.Focus.Web/VAR.Focus.Web.csproj b/VAR.Focus.Web/VAR.Focus.Web.csproj index 3524fd5..002b95d 100644 --- a/VAR.Focus.Web/VAR.Focus.Web.csproj +++ b/VAR.Focus.Web/VAR.Focus.Web.csproj @@ -52,7 +52,6 @@ - @@ -67,12 +66,8 @@ - - - - diff --git a/VAR.WebForms.Common/Code/Bundler.cs b/VAR.WebForms.Common/Code/Bundler.cs index 9ef558b..bf18b1b 100644 --- a/VAR.WebForms.Common/Code/Bundler.cs +++ b/VAR.WebForms.Common/Code/Bundler.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Web; @@ -10,23 +11,47 @@ namespace VAR.WebForms.Common.Code { #region Declarations - private string _path = null; - private List _files = null; + private Assembly _assembly = null; + private string _assemblyNamespace = null; + private List _assemblyFiles = null; + private string _absolutePath = null; + private List _absoluteFiles = null; #endregion Declarations #region Properties - private List Files + private List AssemblyFiles { get { - if (_files != null) { return _files; } + if (_assemblyFiles != null) { return _assemblyFiles; } + if (_assembly == null || string.IsNullOrEmpty(_assemblyNamespace)) + { + _assemblyFiles = new List(); + return _assemblyFiles; + } + string assemblyPath = string.Concat(_assembly.GetName().Name, ".", _assemblyNamespace, "."); + _assemblyFiles = _assembly.GetManifestResourceNames().Where(r => r.StartsWith(assemblyPath)).ToList(); + return _assemblyFiles; + } + } - DirectoryInfo dir = new DirectoryInfo(_path); + private List AbsoluteFiles + { + get + { + if (_absoluteFiles != null) { return _absoluteFiles; } + + if (string.IsNullOrEmpty(_absolutePath)) + { + _absoluteFiles = new List(); + return _absoluteFiles; + } + DirectoryInfo dir = new DirectoryInfo(_absolutePath); FileInfo[] files = dir.GetFiles(); - _files = files.OrderBy(file => file.FullName).Select(file2 => file2.FullName).ToList(); - return _files; + _absoluteFiles = files.OrderBy(file => file.FullName).Select(file2 => file2.FullName).ToList(); + return _absoluteFiles; } } @@ -34,9 +59,11 @@ namespace VAR.WebForms.Common.Code #region Creator - public Bundler(string path) + public Bundler(Assembly assembly = null, string assemblyNamespace = null, string absolutePath = null) { - _path = path; + _assembly = assembly; + _assemblyNamespace = assemblyNamespace; + _absolutePath = absolutePath; } #endregion Creator @@ -46,7 +73,20 @@ namespace VAR.WebForms.Common.Code public void WriteResponse(HttpResponse response, string contentType) { response.ContentType = contentType; - foreach (string fileName in Files) + foreach (string fileName in AssemblyFiles) + { + Stream resourceStream = _assembly.GetManifestResourceStream(fileName); + string fileContent = new StreamReader(resourceStream).ReadToEnd(); + byte[] byteArray = Encoding.UTF8.GetBytes(fileContent); + if (byteArray.Length > 0) + { + response.OutputStream.Write(byteArray, 0, byteArray.Length); + + byteArray = Encoding.UTF8.GetBytes("\n\n"); + response.OutputStream.Write(byteArray, 0, byteArray.Length); + } + } + foreach (string fileName in AbsoluteFiles) { string fileContent = File.ReadAllText(fileName); byte[] byteArray = Encoding.UTF8.GetBytes(fileContent); diff --git a/VAR.WebForms.Common/Code/ScriptsBundler.cs b/VAR.WebForms.Common/Code/ScriptsBundler.cs index ef59d73..f99f79e 100644 --- a/VAR.WebForms.Common/Code/ScriptsBundler.cs +++ b/VAR.WebForms.Common/Code/ScriptsBundler.cs @@ -1,4 +1,5 @@ -using System.Web; +using System.Reflection; +using System.Web; namespace VAR.WebForms.Common.Code { @@ -10,7 +11,10 @@ namespace VAR.WebForms.Common.Code public void ProcessRequest(HttpContext context) { - Bundler bundler = new Bundler(context.Server.MapPath("~/Scripts/")); + Bundler bundler = new Bundler( + assembly: Assembly.GetExecutingAssembly(), + assemblyNamespace: "Scripts", + absolutePath: context.Server.MapPath("~/Scripts/")); context.Response.PrepareCacheableResponse(); bundler.WriteResponse(context.Response, "text/javascript"); } diff --git a/VAR.WebForms.Common/Code/StylesBundler.cs b/VAR.WebForms.Common/Code/StylesBundler.cs index a11aad8..49452b6 100644 --- a/VAR.WebForms.Common/Code/StylesBundler.cs +++ b/VAR.WebForms.Common/Code/StylesBundler.cs @@ -1,4 +1,5 @@ -using System.Web; +using System.Reflection; +using System.Web; namespace VAR.WebForms.Common.Code { @@ -10,7 +11,10 @@ namespace VAR.WebForms.Common.Code public void ProcessRequest(HttpContext context) { - Bundler bundler = new Bundler(context.Server.MapPath("~/Styles/")); + Bundler bundler = new Bundler( + assembly: Assembly.GetExecutingAssembly(), + assemblyNamespace: "Styles", + absolutePath: context.Server.MapPath("~/Styles/")); context.Response.PrepareCacheableResponse(); bundler.WriteResponse(context.Response, "text/css"); } diff --git a/VAR.Focus.Web/Scripts/01. Base.js b/VAR.WebForms.Common/Scripts/01. Base.js similarity index 100% rename from VAR.Focus.Web/Scripts/01. Base.js rename to VAR.WebForms.Common/Scripts/01. Base.js diff --git a/VAR.Focus.Web/Scripts/02. Ajax.js b/VAR.WebForms.Common/Scripts/02. Ajax.js similarity index 100% rename from VAR.Focus.Web/Scripts/02. Ajax.js rename to VAR.WebForms.Common/Scripts/02. Ajax.js diff --git a/VAR.Focus.Web/Scripts/03. Controls.js b/VAR.WebForms.Common/Scripts/03. Controls.js similarity index 100% rename from VAR.Focus.Web/Scripts/03. Controls.js rename to VAR.WebForms.Common/Scripts/03. Controls.js diff --git a/VAR.Focus.Web/Styles/00. Reset.css b/VAR.WebForms.Common/Styles/00. Reset.css similarity index 100% rename from VAR.Focus.Web/Styles/00. Reset.css rename to VAR.WebForms.Common/Styles/00. Reset.css diff --git a/VAR.Focus.Web/Styles/01. base.css b/VAR.WebForms.Common/Styles/01. base.css similarity index 100% rename from VAR.Focus.Web/Styles/01. base.css rename to VAR.WebForms.Common/Styles/01. base.css diff --git a/VAR.WebForms.Common/VAR.WebForms.Common.csproj b/VAR.WebForms.Common/VAR.WebForms.Common.csproj index d0d799d..7be5c60 100644 --- a/VAR.WebForms.Common/VAR.WebForms.Common.csproj +++ b/VAR.WebForms.Common/VAR.WebForms.Common.csproj @@ -74,5 +74,12 @@ + + + + + + + \ No newline at end of file