Skip to content

Instantly share code, notes, and snippets.

@5up3rman
Last active July 13, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5up3rman/7d1924bd197a893420c1fec997f9669c to your computer and use it in GitHub Desktop.
Save 5up3rman/7d1924bd197a893420c1fec997f9669c to your computer and use it in GitHub Desktop.
Get Stylesheets Processor
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.IO;
using Sitecore.Xml;
namespace EditorEnhancementToolkit.Foundation.ContentEditor.Pipelines.RenderPageAssets
{
public class GetStylesheets : RenderAssetsProcessor
{
private readonly List<Source> Sources = new List<Source>();
private List<string> FilePaths { get; } = new List<string>();
public override void Process(RenderPageAssetsArgs args)
{
RetrieveFilesFromDirectory();
args.FilePaths.AddRange(FilePaths);
}
public void AddSource(XmlNode configNode)
{
Sources.Add(new Source(XmlUtil.GetAttribute("folder", configNode), MainUtil.GetBool(XmlUtil.GetAttribute("deep", configNode), false), XmlUtil.GetAttribute("pattern", configNode)));
}
protected virtual void RetrieveFilesFromDirectory()
{
foreach (var source in Sources)
{
try
{
var folder = FileUtil.MapPath(source.Folder);
var patterns = source.SearchPattern.Split(',');
RetrieveFiles(source, folder, patterns);
}
catch (Exception ex)
{
Log.Error("[ResolveScriptProcessor.Scan] Error processing source: " + source.Folder, ex, (object)this);
}
}
}
private void RetrieveFiles(Source source, string folder, IEnumerable<string> patterns)
{
if (!Directory.Exists(folder))
return;
foreach (var searchPattern in patterns)
{
foreach (var str in Directory.GetFiles(folder, searchPattern))
{
var fileInfo = new FileInfo(str);
if ((fileInfo.Attributes & FileAttributes.System) != FileAttributes.System && (fileInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
var unmappedPath = FileUtil.UnmapPath(str);
if (!FilePaths.Any(x => x.Equals(unmappedPath, StringComparison.InvariantCultureIgnoreCase)))
FilePaths.Add(FileUtil.UnmapPath(str));
}
}
}
if (!source.Deep)
return;
foreach (var subFolder in Directory.GetDirectories(folder))
RetrieveFiles(source, subFolder, patterns);
}
protected class Source
{
public bool Deep { get; }
public string Folder { get; }
public string SearchPattern { get; set; }
public Source(string folder, bool deep, string pattern)
{
Folder = folder;
Deep = deep;
SearchPattern = pattern;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment