using System;
using System.IO;
using Yahoo.Yui.Compressor;
using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;
namespace LCCA.Clinical.Tools
{
public class Program
{
static void Main()
{
//Replace the project folder path as per your local environment
const string projectDir = @"Paths"; // Local path where JavaScript files are stored in your machine
const string operation = "mergejs";
if (operation.ToLower().Equals("mergejs"))
{
CompressJS(projectDir);
MergeJS(projectDir);
}
else if (operation.ToLower().Equals("compresscss"))
{
CompressCSS(projectDir);
}
else if (operation.ToLower().Equals("compressjs"))
{
CompressJS(projectDir);
}
else if (operation.ToLower().Equals("all"))
{
CompressCSS(projectDir);
CompressJS(projectDir);
MergeJS(projectDir);
}
}
private static void MergeJS(string projectDir)
{
string scriptCatalog = Path.Combine(projectDir, "Scripts\\script-catalog.xml");
string mergedFolderPath = Path.Combine(projectDir, "Scripts\\merged");
string scriptsFolder = Path.Combine(projectDir, "Scripts");
IList<string> scriptFiles =
Directory.GetFiles(scriptsFolder, "*.js", SearchOption.AllDirectories).ToListIfNotNullOrEmpty();
string content = File.ReadAllText(scriptCatalog);
XmlDocument doc = new XmlDocument();
doc.LoadXml(content);
XmlNodeList files = doc.SelectNodes("//files/file");
foreach (XmlElement file in files)
{
StringBuilder sb = new StringBuilder();
XmlNodeList references = file.SelectNodes("reference");
string targetFilePath = Path.Combine(mergedFolderPath, file.Attributes["name"].Value);
foreach (XmlElement reference in references)
{
string referenceFileName = reference.Attributes["name"].Value;
string referenceFilePath = scriptFiles.Single(x => new FileInfo(x).Name.Equals(referenceFileName));
string referenceContent = File.ReadAllText(referenceFilePath);
sb.AppendLine("//" + referenceFileName);
sb.AppendLine();
sb.AppendLine(referenceContent);
sb.AppendLine();
}
File.WriteAllText(targetFilePath, sb.ToString());
}
}
private static void CompressJS(string projectDir)
{
string jsSourceFolder = Path.Combine(projectDir, "Scripts\\custom\\debug");
string jsTargetFolder = Path.Combine(projectDir, "Scripts\\custom");
CompressJS(jsSourceFolder, jsTargetFolder);
}
private static void CompressJS(string sourceFolder, string targetFolder)
{
foreach (string sourceFilePath in Directory.GetFiles(sourceFolder))
{
string sourceFilePathWithoutExtn = Path.GetFileNameWithoutExtension(sourceFilePath);
string targetFilePath = Path.Combine(targetFolder, sourceFilePathWithoutExtn + ".min.js");
if (new FileInfo(sourceFilePath).Extension.EqualsIgnoreCase(".js"))
{
string content = File.ReadAllText(sourceFilePath);
string compressedContent = JavaScriptCompressor.Compress(content, true, true, false, false, 73);
File.WriteAllText(targetFilePath, compressedContent);
}
}
}
private static void CompressCSS(string projectDir)
{
//First Run
string cssSourceFolder = Path.Combine(projectDir, "Content\\css");
string cssTargetFolder = Path.Combine(projectDir, "Content\\css");
CompressCSS(cssSourceFolder, cssTargetFolder);
//Second Run
cssSourceFolder = Path.Combine(projectDir, "Content");
cssTargetFolder = Path.Combine(projectDir, "Content");
CompressCSS(cssSourceFolder, cssTargetFolder);
}
private static void CompressCSS(string sourceFolder, string targetFolder)
{
foreach (string sourceFilePath in Directory.GetFiles(sourceFolder))
{
if (new FileInfo(sourceFilePath).Extension.EqualsIgnoreCase(".css"))
{
string sourceFilePathWithoutExtn = Path.GetFileNameWithoutExtension(sourceFilePath);
string targetFilePath = Path.Combine(targetFolder, sourceFilePathWithoutExtn + ".css");
string content = File.ReadAllText(sourceFilePath);
string compressedContent = CssCompressor.Compress(content, 73, CssCompressionType.StockYuiCompressor);
File.WriteAllText(targetFilePath, compressedContent);
}
}
}
}
}
No comments:
Post a Comment