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

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Web; using System.Web;
@@ -10,23 +11,47 @@ namespace VAR.WebForms.Common.Code
{ {
#region Declarations #region Declarations
private string _path = null; private Assembly _assembly = null;
private List<string> _files = null; private string _assemblyNamespace = null;
private List<string> _assemblyFiles = null;
private string _absolutePath = null;
private List<string> _absoluteFiles = null;
#endregion Declarations #endregion Declarations
#region Properties #region Properties
private List<string> Files private List<string> AssemblyFiles
{ {
get 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(); FileInfo[] files = dir.GetFiles();
_files = files.OrderBy(file => file.FullName).Select(file2 => file2.FullName).ToList(); _absoluteFiles = files.OrderBy(file => file.FullName).Select(file2 => file2.FullName).ToList();
return _files; return _absoluteFiles;
} }
} }
@@ -34,9 +59,11 @@ namespace VAR.WebForms.Common.Code
#region Creator #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 #endregion Creator
@@ -46,7 +73,20 @@ namespace VAR.WebForms.Common.Code
public void WriteResponse(HttpResponse response, string contentType) public void WriteResponse(HttpResponse response, string contentType)
{ {
response.ContentType = 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); string fileContent = File.ReadAllText(fileName);
byte[] byteArray = Encoding.UTF8.GetBytes(fileContent); 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 namespace VAR.WebForms.Common.Code
{ {
@@ -10,7 +11,10 @@ namespace VAR.WebForms.Common.Code
public void ProcessRequest(HttpContext context) 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(); context.Response.PrepareCacheableResponse();
bundler.WriteResponse(context.Response, "text/javascript"); 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 namespace VAR.WebForms.Common.Code
{ {
@@ -10,7 +11,10 @@ namespace VAR.WebForms.Common.Code
public void ProcessRequest(HttpContext context) 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(); context.Response.PrepareCacheableResponse();
bundler.WriteResponse(context.Response, "text/css"); bundler.WriteResponse(context.Response, "text/css");
} }

View File

@@ -74,5 +74,12 @@
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </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" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>