Convert base scripts and styles to embedded resources and move to VAR.WebForms.Common.

This commit is contained in:
2020-03-02 09:09:29 +01:00
parent d1e8907d5c
commit 4deae439af
10 changed files with 69 additions and 19 deletions

View File

@@ -52,7 +52,6 @@
<ItemGroup>
<Content Include="Images\BGCork.png" />
<Content Include="priv\keep.txt" />
<Content Include="Scripts\01. Base.js" />
<None Include="packages.config" />
<None Include="Properties\PublishProfiles\FolderProfile.pubxml" />
<Content Include="Resources\Literals.en.json" />
@@ -67,12 +66,8 @@
</None>
</ItemGroup>
<ItemGroup>
<Content Include="Scripts\02. Ajax.js" />
<Content Include="Scripts\03. Controls.js" />
<Content Include="Scripts\05. Cards.js" />
<Content Include="Scripts\10. Chat.js" />
<Content Include="Styles\00. Reset.css" />
<Content Include="Styles\01. base.css" />
<Content Include="Styles\02. Boards.css" />
<Content Include="Styles\05. Cards.css" />
<Content Include="Styles\10. Chat.css" />

View File

@@ -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<string> _files = null;
private Assembly _assembly = null;
private string _assemblyNamespace = null;
private List<string> _assemblyFiles = null;
private string _absolutePath = null;
private List<string> _absoluteFiles = null;
#endregion Declarations
#region Properties
private List<string> Files
private List<string> AssemblyFiles
{
get
{
if (_files != null) { return _files; }
if (_assemblyFiles != null) { return _assemblyFiles; }
if (_assembly == null || string.IsNullOrEmpty(_assemblyNamespace))
{
_assemblyFiles = new List<string>();
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<string> AbsoluteFiles
{
get
{
if (_absoluteFiles != null) { return _absoluteFiles; }
if (string.IsNullOrEmpty(_absolutePath))
{
_absoluteFiles = new List<string>();
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);

View File

@@ -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");
}

View File

@@ -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");
}

View File

@@ -74,5 +74,12 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Scripts\01. Base.js" />
<EmbeddedResource Include="Scripts\02. Ajax.js" />
<EmbeddedResource Include="Scripts\03. Controls.js" />
<EmbeddedResource Include="Styles\00. Reset.css" />
<EmbeddedResource Include="Styles\01. base.css" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>