Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FontAwesome 5.6.1, fix XML docs, unit tests & concurrency bug fix. #8

Merged
merged 3 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Geta.Epi.FontThumbnail.EnumGenerator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers" Version="2.9.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Octokit" Version="0.32.0" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions src/Geta.Epi.FontThumbnail.EnumGenerator/GithubDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Octokit;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Geta.Epi.FontThumbnail.EnumGenerator
{
public static class GithubDownloader
{
public static async Task<Stream> DownloadLatestReleaseAsync(string owner, string repo)
{
var gitHubClient = new GitHubClient(new ProductHeaderValue("Geta.Epi.FontThumbnail.EnumGenerator"));
var latestRelease = await gitHubClient.Repository.Release.GetLatest(owner, repo);
var fileName = Path.GetFileName(latestRelease.ZipballUrl);

Console.WriteLine(
"The latest release is tagged at {0} and is named {1}",
latestRelease.TagName,
latestRelease.Name);

var response = await gitHubClient.Connection.Get<object>(new Uri(latestRelease.ZipballUrl), TimeSpan.FromMinutes(1));
var bytes = response.HttpResponse.Body as byte[];

return new MemoryStream(bytes);
}
}
}
96 changes: 60 additions & 36 deletions src/Geta.Epi.FontThumbnail.EnumGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,79 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Geta.Epi.FontThumbnail.EnumGenerator.Models;
using Newtonsoft.Json;

namespace Geta.Epi.FontThumbnail.EnumGenerator
{
internal static class Program
{
private static void Main(string[] args)
private static async Task Main(string[] args)
{
var sourcePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName;

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Font Awesome Enum Generator");
Console.WriteLine("{0}", sourcePath);
var enumBasePath = $@"{sourcePath}\Geta.Epi.FontThumbnail";
Console.WriteLine("\nOutput directory: {0}", enumBasePath);

AskForFontAwesomeDirectory:
Console.WriteLine("\nEnter the Font Awesome directory:");
var fontAwesomePath = Console.ReadLine();
if (!Directory.Exists(fontAwesomePath))
{
Console.WriteLine("\nDirectory does not exist.");
goto AskForFontAwesomeDirectory;
}
var fontAwesomeZipStream = await GithubDownloader.DownloadLatestReleaseAsync("FortAwesome", "Font-Awesome");

var metadataPath = $@"{fontAwesomePath}\metadata\icons.json";
if (!File.Exists(metadataPath))
using (var archive = new ZipArchive(fontAwesomeZipStream))
{
Console.WriteLine("\nDirectory does not contain a metadata directory with a icons.json file.");
goto AskForFontAwesomeDirectory;
}
var rootEntry = archive.Entries[0];
var metaDataEntry = archive.GetEntry(rootEntry + "metadata/icons.json");

Console.WriteLine("\nLoading metadata from: {0}", metadataPath);
var metadata = LoadMetadata(metadataPath);
if (metaDataEntry == null)
{
Console.WriteLine("\nArchive does not contain a metadata directory with a icons.json file.");
}

// Get a list all the different styles
var styles = metadata.SelectMany(x => x.Styles).Distinct();
Console.WriteLine("\nLoading metadata from: {0}", metaDataEntry);
var metadata = LoadMetadata(metaDataEntry.Open());

foreach (var item in styles)
{
var styleName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item);
var enumName = $"FontAwesome5{styleName}";
var localPath = $@"{enumBasePath}\{enumName}.cs";
// Get a list all the different styles
var styles = metadata.SelectMany(x => x.Styles).Distinct();

Console.WriteLine("\nGenerating {0}.cs...", enumName);
var icons = metadata.Where(x => x.Styles.Contains(item) && !x.Private);
WriteEnumToFile(localPath, enumName, icons);
}
foreach (var item in styles)
{
var styleName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item);
var enumName = $"FontAwesome5{styleName}";
var localPath = $@"{enumBasePath}\{enumName}.cs";

Console.WriteLine("\nGenerating {0}.cs...", enumName);
var icons = metadata.Where(x => x.Styles.Contains(item) && !x.Private);
WriteEnumToFile(localPath, enumName, icons);
}

CopyFontFiles(fontAwesomePath, enumBasePath);
CopyFontFiles(archive, enumBasePath);
}

Console.WriteLine("\nDone generating Enums. Press enter to exit.");
Console.ReadLine();
}

private static void CopyFontFiles(string fontAwesomePath, string enumBasePath)
private static void CopyFontFiles(ZipArchive archive, string enumBasePath)
{
var source = $@"{fontAwesomePath}\webfonts\";
var destination = $@"{enumBasePath}\Fonts\";

var filesToCopy = Directory.GetFiles(source, "*.ttf");
foreach (var fileToCopy in filesToCopy)
var rootEntry = archive.Entries[0];
var fontEntries = archive.Entries.Where(x => x.FullName.StartsWith(rootEntry + "webfonts") && x.FullName.EndsWith(".ttf"));

foreach (var fileToCopy in fontEntries)
{
Console.WriteLine("\nCopying {0} to {1}...", fileToCopy, destination);
File.Copy(fileToCopy, destination + Path.GetFileName(fileToCopy), true);
Console.WriteLine("\nCopying {0} to {1}...", fileToCopy.Name, destination);
fileToCopy.ExtractToFile(destination + Path.GetFileName(fileToCopy.Name), true);
}
}

private static IList<MetadataIcon> LoadMetadata(string path)
private static IList<MetadataIcon> LoadMetadata(Stream stream)
{
using (var file = File.OpenText(path))
using (var file = new StreamReader(stream))
{
var serializer = new JsonSerializer();
serializer.Converters.Add(new FontAwesomeJsonConverter());
Expand Down Expand Up @@ -110,6 +111,7 @@ private static void WriteEnumToFile(string path, string enumName, IEnumerable<Me
{
writer.WriteLine("\t\t/// <summary>");
writer.WriteLine($"\t\t/// {icon.Label.ToTitleCase()} ({icon.Name})");
WriteStyles(writer, icon);
WriteSearchTerms(writer, icon);
WriteChanges(writer, icon);
writer.WriteLine("\t\t/// </summary>");
Expand All @@ -122,6 +124,14 @@ private static void WriteEnumToFile(string path, string enumName, IEnumerable<Me
}
}

private static void WriteStyles(StreamWriter writer, MetadataIcon icon)
{
if (icon.Styles?.Count > 1)
{
writer.WriteLine($"\t\t/// <para>Styles: {string.Join(", ", icon.Styles)}</para>");
}
}

private static void WriteSearchTerms(StreamWriter writer, MetadataIcon icon)
{
if (icon.Search?.Terms?.Count > 0)
Expand Down Expand Up @@ -154,7 +164,21 @@ private static string GetEnumSafeName(MetadataIcon icon)
name = "_" + name;
}

// Verify reverse conversion
var reverse = name.ToDashCase().Replace("_", string.Empty);
if (!icon.Name.Equals(reverse))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{icon.Name}\t!=\t{reverse}\t{name}");
Console.ForegroundColor = ConsoleColor.White;
}

return name;
}

public static string ToDashCase(this string input)
{
return string.Concat(input.Select((c, i) => i > 0 && char.IsUpper(c) && (!char.IsDigit(input[i - 1]) || !char.IsDigit(input[i - 2 > 0 ? i - 2 : 0])) ? "-" + c : c.ToString())).ToLower();
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EPiServer.CMS.UI.Core" Version="11.1.0" />
johanbenschop marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.3" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
Expand All @@ -22,4 +23,19 @@
<Reference Include="System.Configuration" />
</ItemGroup>

<ItemGroup>
<None Update="App_Data\fonts\fa-brands-400.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="App_Data\fonts\fa-regular-400.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="App_Data\fonts\fa-solid-900.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="App_Data\fonts\fontawesome.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions src/Geta.Epi.FontThumbnail.Tests/ThumbnailIconControllerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Configuration;
using System.IO;
using EPiServer.Web;
using Geta.Epi.FontThumbnail.Controllers;
using Geta.Epi.FontThumbnail.Settings;

namespace Geta.Epi.FontThumbnail.Tests
{
public class ThumbnailIconControllerFixture : IDisposable
{
public readonly ThumbnailIconController controller;
public readonly ThumbnailSettings settings;
private readonly string _temporaryDirectory;

public ThumbnailIconControllerFixture()
{
var partialDirectpry = $"[appDataPath]\\thumb_cache\\{Guid.NewGuid()}\\";
ConfigurationManager.AppSettings["FontThumbnail.CachePath"] = partialDirectpry;
_temporaryDirectory = VirtualPathUtilityEx.RebasePhysicalPath(partialDirectpry);

Directory.CreateDirectory(_temporaryDirectory);

var service = new FontThumbnailService();
controller = new ThumbnailIconController(service);
settings = new ThumbnailSettings
{
FontSize = Constants.DefaultFontSize,
BackgroundColor = Constants.DefaultBackgroundColor,
ForegroundColor = Constants.DefaultForegroundColor,
Height = Constants.DefaultHeight,
Width = Constants.DefaultWidth
};
}

public void Dispose()
{
Directory.Delete(_temporaryDirectory, true);
}
}
}
96 changes: 0 additions & 96 deletions src/Geta.Epi.FontThumbnail.Tests/ThumbnailIconControllerTest.cs

This file was deleted.