From 4f188e06b6b895bd4c7318aa95b12df1cff163bf Mon Sep 17 00:00:00 2001 From: Devedse Date: Sat, 25 Jul 2020 03:32:37 +0200 Subject: [PATCH] Added template files to resource file --- .../GitHubRepositoryOptimizer.cs | 6 +- .../Helpers/TemplatesHandler.cs | 20 ++- .../CommitInPullRequestMarkdownTemplate.txt | 0 .../CommitMarkdownTemplate.txt | 0 .../PullRequestMarkdownTemplate.txt | 0 .../Resources/Templates.Designer.cs | 111 +++++++++++++++ .../Resources/Templates.resx | 130 ++++++++++++++++++ .../WebOptimizationProject.csproj | 21 +-- 8 files changed, 264 insertions(+), 24 deletions(-) rename WebOptimizationProject/{ => Resources}/CommitInPullRequestMarkdownTemplate.txt (100%) rename WebOptimizationProject/{ => Resources}/CommitMarkdownTemplate.txt (100%) rename WebOptimizationProject/{ => Resources}/PullRequestMarkdownTemplate.txt (100%) create mode 100644 WebOptimizationProject/Resources/Templates.Designer.cs create mode 100644 WebOptimizationProject/Resources/Templates.resx diff --git a/WebOptimizationProject/GitHubRepositoryOptimizer.cs b/WebOptimizationProject/GitHubRepositoryOptimizer.cs index 3a6a7b5..44335e8 100644 --- a/WebOptimizationProject/GitHubRepositoryOptimizer.cs +++ b/WebOptimizationProject/GitHubRepositoryOptimizer.cs @@ -107,11 +107,11 @@ public async Task GoOptimize(string repositoryOwner, string repositoryName, stri await _git.RunHubCommand("add ."); - var descriptionForCommit = await TemplatesHandler.GetDescriptionForCommit(); + var descriptionForCommit = TemplatesHandler.GetDescriptionForCommit(); await _git.Commit("Wop optimized this repository", descriptionForCommit); await _git.RunHubCommand($"push"); - var descriptionForPullRequest = await TemplatesHandler.GetDescriptionForPullRequest(); + var descriptionForPullRequest = TemplatesHandler.GetDescriptionForPullRequest(); //Only create pull request if there were actually any successful optimizations if (optimizedFileResults.Any(t => t.OptimizationResult == OptimizationResult.Success) && optimizedFileResults.Sum(t => t.OriginalSize) > optimizedFileResults.Sum(t => t.OptimizedSize)) @@ -141,7 +141,7 @@ public async Task GoOptimize(string repositoryOwner, string repositoryName, stri commitCount = splittedBody.Count(t => t.StartsWith(commitIdentifier)); } - var descriptionForCommitInPr = await TemplatesHandler.GetCommitDescriptionForPullRequest(clonedRepo, branchName, optimizedFileResults, commitCount + 1); + var descriptionForCommitInPr = TemplatesHandler.GetCommitDescriptionForPullRequest(clonedRepo, branchName, optimizedFileResults, commitCount + 1); bodySb.AppendLine(); bodySb.AppendLine(); diff --git a/WebOptimizationProject/Helpers/TemplatesHandler.cs b/WebOptimizationProject/Helpers/TemplatesHandler.cs index 801eed5..dfea44b 100644 --- a/WebOptimizationProject/Helpers/TemplatesHandler.cs +++ b/WebOptimizationProject/Helpers/TemplatesHandler.cs @@ -1,5 +1,4 @@ using DeveImageOptimizer; -using DeveImageOptimizer.Helpers; using DeveImageOptimizer.State; using System; using System.Collections.Generic; @@ -7,16 +6,15 @@ using System.Linq; using System.Reflection; using System.Text; -using System.Threading.Tasks; +using WebOptimizationProject.Resources; namespace WebOptimizationProject.Helpers { public static class TemplatesHandler { - public static async Task GetDescriptionForPullRequest() + public static string GetDescriptionForPullRequest() { - var filePath = Path.Combine(FolderHelperMethods.Internal_AssemblyDirectory.Value, "PullRequestMarkdownTemplate.txt"); - var templateText = await Task.Run(() => File.ReadAllText(filePath)); + var templateText = Templates.PullRequestMarkdownTemplate; //var stringForCommitDetails = await GetCommitDescriptionForPullRequest(optimizedFileResults, 1); @@ -25,10 +23,9 @@ public static async Task GetDescriptionForPullRequest() return templateText; } - public static async Task GetCommitDescriptionForPullRequest(string clonedRepoPath, string branchName, IEnumerable optimizedFileResults, int commitNumber) + public static string GetCommitDescriptionForPullRequest(string clonedRepoPath, string branchName, IEnumerable optimizedFileResults, int commitNumber) { - var filePath = Path.Combine(FolderHelperMethods.Internal_AssemblyDirectory.Value, "CommitInPullRequestMarkdownTemplate.txt"); - var templateText = await Task.Run(() => File.ReadAllText(filePath)); + var templateText = Templates.CommitInPullRequestMarkdownTemplate; templateText = templateText.Replace("{CommitNumber}", commitNumber.ToString()); templateText = templateText.Replace("{SupportedFileExtensions}", string.Join(" ", ConstantsAndConfig.ValidExtensions)); @@ -37,7 +34,7 @@ public static async Task GetCommitDescriptionForPullRequest(string clone var totalBytesBefore = optimizedFileResults.Sum(t => t.OriginalSize); var totalBytesSaved = optimizedFileResults.Where(t => t.OptimizationResult == OptimizationResult.Success).Sum(t => t.OriginalSize - t.OptimizedSize); var totalBytesAfter = totalBytesBefore - totalBytesSaved; - var percentageRemaining = Math.Round((double)totalBytesAfter / (double)totalBytesBefore * 100.0, 2); + var percentageRemaining = Math.Round(totalBytesAfter / (double)totalBytesBefore * 100.0, 2); var timeSpan = TimeSpan.Zero; foreach (var duration in optimizedFileResults.Select(t => t.Duration)) @@ -89,10 +86,9 @@ public static async Task GetCommitDescriptionForPullRequest(string clone return templateText; } - public static async Task GetDescriptionForCommit() + public static string GetDescriptionForCommit() { - var filePath = Path.Combine(FolderHelperMethods.Internal_AssemblyDirectory.Value, "CommitMarkdownTemplate.txt"); - var templateText = await Task.Run(() => File.ReadAllText(filePath)); + var templateText = Templates.CommitMarkdownTemplate; return templateText; } diff --git a/WebOptimizationProject/CommitInPullRequestMarkdownTemplate.txt b/WebOptimizationProject/Resources/CommitInPullRequestMarkdownTemplate.txt similarity index 100% rename from WebOptimizationProject/CommitInPullRequestMarkdownTemplate.txt rename to WebOptimizationProject/Resources/CommitInPullRequestMarkdownTemplate.txt diff --git a/WebOptimizationProject/CommitMarkdownTemplate.txt b/WebOptimizationProject/Resources/CommitMarkdownTemplate.txt similarity index 100% rename from WebOptimizationProject/CommitMarkdownTemplate.txt rename to WebOptimizationProject/Resources/CommitMarkdownTemplate.txt diff --git a/WebOptimizationProject/PullRequestMarkdownTemplate.txt b/WebOptimizationProject/Resources/PullRequestMarkdownTemplate.txt similarity index 100% rename from WebOptimizationProject/PullRequestMarkdownTemplate.txt rename to WebOptimizationProject/Resources/PullRequestMarkdownTemplate.txt diff --git a/WebOptimizationProject/Resources/Templates.Designer.cs b/WebOptimizationProject/Resources/Templates.Designer.cs new file mode 100644 index 0000000..fb45ae4 --- /dev/null +++ b/WebOptimizationProject/Resources/Templates.Designer.cs @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace WebOptimizationProject.Resources { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Templates { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Templates() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WebOptimizationProject.Resources.Templates", typeof(Templates).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to ### Commit {CommitNumber} + /// + ///Information | Value + ///-- | -- + ///Version | {Version} + ///Support file extension | {SupportedFileExtensions} + /// + ///#### Statistics + /// + ///Stats | Value + ///-- | -- + ///Total optimizable files | {OptimizableFileCount} + ///Files optimized successfully (size reduced) | {FilesOptimizedSuccessfully} + ///Files already fully optimized | {FilesAlreadyOptimized} + ///Files that failed to be optimized | {FilesFailedOptimization} + ///Total size before optimization | {TotalBytesBefore} + ///Total size after optimization | {Tot [rest of string was truncated]";. + /// + internal static string CommitInPullRequestMarkdownTemplate { + get { + return ResourceManager.GetString("CommitInPullRequestMarkdownTemplate", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The Web Optimization Project optimized this repository. This commit contains the optimized files in this repository.. + /// + internal static string CommitMarkdownTemplate { + get { + return ResourceManager.GetString("CommitMarkdownTemplate", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to # Web Optimization Project (Beta) + /// + ///The [Web Optimization Project](https://www.github.com/devedse/weboptimizationproject) is a project that will automatically optimizes files in git repositories by compressing them losslessly. This project was created by [Devedse](https://github.com/devedse) in an effort to save the world! :smirk: + /// + ///If you have any questions, feel free to create an issue or join the gitter chat: + ///[![Join the chat at https://gitter.im/WebOptimizationProject/Lobby](https://badges.gitter.im/ [rest of string was truncated]";. + /// + internal static string PullRequestMarkdownTemplate { + get { + return ResourceManager.GetString("PullRequestMarkdownTemplate", resourceCulture); + } + } + } +} diff --git a/WebOptimizationProject/Resources/Templates.resx b/WebOptimizationProject/Resources/Templates.resx new file mode 100644 index 0000000..6404667 --- /dev/null +++ b/WebOptimizationProject/Resources/Templates.resx @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + CommitInPullRequestMarkdownTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + + CommitMarkdownTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + + PullRequestMarkdownTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + \ No newline at end of file diff --git a/WebOptimizationProject/WebOptimizationProject.csproj b/WebOptimizationProject/WebOptimizationProject.csproj index c7fd872..e65ae81 100644 --- a/WebOptimizationProject/WebOptimizationProject.csproj +++ b/WebOptimizationProject/WebOptimizationProject.csproj @@ -21,15 +21,18 @@ - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - + + True + True + Templates.resx + + + + + + ResXFileCodeGenerator + Templates.Designer.cs + \ No newline at end of file