diff --git a/README.md b/README.md index a40f3b7..899e5da 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![NuGet Badge](https://img.shields.io/nuget/vpre/MSBuild.CompactJsonResources)](https://www.nuget.org/packages/MSBuild.CompactJsonResources/) [![license](https://img.shields.io/github/license/dimonovdd/MSBuild.CompactJsonResources)](https://github.com/dimonovdd/MSBuild.CompactJsonResources/blob/main/LICENSE) [![fuget.org](https://www.fuget.org/packages/MSBuild.CompactJsonResources/badge.svg)](https://www.fuget.org/packages/MSBuild.CompactJsonResources) -Often, Json files are added to a project as one line to reduce the size of the build. Such files are uncomfortable to read and track changes through version control systems. This package removes whitespaces, trailing commas and comments from the builds without changing the beautiful Json source files by adding their single-line copies. +Often, Json files are added to a project as one line to reduce the size of the build. Such files are uncomfortable to read and track changes through version control systems. This package resolve this problem by removing whitespaces, trailing commas and comments from the builds without changing the beautiful Json source files by adding their single-line copies. ```json { diff --git a/src/CompactJson/CompactJsonTask.cs b/src/CompactJson/CompactJsonTask.cs index bb3b8a0..49d8b9a 100644 --- a/src/CompactJson/CompactJsonTask.cs +++ b/src/CompactJson/CompactJsonTask.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; +using static Microsoft.Build.Framework.MessageImportance; namespace MSBuild.CompactJsonResources { @@ -19,42 +21,38 @@ public class CompactJsonTask : Task public override bool Execute() { - LogMessage($"{nameof(TempOutputPath)}: {TempOutputPath}"); + LogMessage($"{nameof(TempOutputPath)}: {TempOutputPath}", High); try { if (JsonFiles?.Length > 0) OutputJsonFiles = ParseAndCopyFiles(JsonFiles)?.ToArray(); else - Log.LogMessage($"{nameof(JsonFiles)} is null or empty"); + LogMessage($"{nameof(JsonFiles)} is null or empty"); } catch (Exception ex) { - LogMessage(ex.StackTrace); - Log.LogErrorFromException(ex); + Log.LogErrorFromException(ex, true); return false; } return true; } - - List ParseAndCopyFiles(ITaskItem[] items) + IEnumerable ParseAndCopyFiles(ITaskItem[] items) { - var output = new List(); foreach(var item in items) { - LogMessage(item.ToString("Original File")); + LogMessage(item.ToString("Original File"), Low); var json = new JsonFile(item, TempOutputPath); LogMessage($"Temp File Full Path: {json.TempFullPath}"); var outputItem = json.WriteCompactTempFile(); - LogMessage(outputItem.ToString("Temp File")); - output.Add(outputItem); + LogMessage(outputItem.ToString("Temp File"), Low); + yield return outputItem; } - return output; } - - void LogMessage(string mess) => Log.LogMessage($"{LogTag}{mess}"); + void LogMessage(string mess, MessageImportance importance = Normal) + => Log.LogMessage(importance, $"{LogTag}{mess}"); } } diff --git a/src/CompactJson/Extension.cs b/src/CompactJson/Extension.cs index 729ad04..096db13 100644 --- a/src/CompactJson/Extension.cs +++ b/src/CompactJson/Extension.cs @@ -17,5 +17,8 @@ public static string ToString(this ITaskItem item, string title) return result; } + + public static bool IsEmpty(this string value) + => string.IsNullOrWhiteSpace(value); } } diff --git a/src/CompactJson/JsonFile.cs b/src/CompactJson/JsonFile.cs index d333e43..a12f6e4 100644 --- a/src/CompactJson/JsonFile.cs +++ b/src/CompactJson/JsonFile.cs @@ -12,6 +12,7 @@ public class JsonFile const string definingProjectFullPath = "DefiningProjectFullPath"; const string definingProjectName = "DefiningProjectName"; const string definingProjectExtension = "DefiningProjectExtension"; + const char dotChar = '.'; readonly ITaskItem item; readonly string tempOutputPath; @@ -53,21 +54,23 @@ public JsonFile(string link, string filename, string extension, string fullPath, public TaskItem WriteCompactTempFile() { - using var fs = File.OpenRead(FullPath); - using var jDoc = JsonDocument.Parse(fs, new JsonDocumentOptions { AllowTrailingCommas = true, CommentHandling = JsonCommentHandling.Skip }); + using var frs = File.OpenRead(FullPath); + using var jDoc = JsonDocument.Parse(frs, + new JsonDocumentOptions { AllowTrailingCommas = true, CommentHandling = JsonCommentHandling.Skip }); var directory = Path.GetDirectoryName(TempFullPath); + if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); if (File.Exists(TempFullPath)) File.Delete(TempFullPath); - using var stream = File.OpenWrite(TempFullPath); - using var writer = new Utf8JsonWriter(stream); + using var fws = File.OpenWrite(TempFullPath); + using var writer = new Utf8JsonWriter(fws); jDoc.WriteTo(writer); writer.Flush(); - stream.Flush(); + fws.Flush(); return GetTaskItem(); } @@ -75,12 +78,12 @@ public TaskItem WriteCompactTempFile() public override string ToString() => FullPath; TaskItem GetTaskItem() - => new TaskItem(TempFullPath, new Dictionary + => new(TempFullPath, new Dictionary { { nameof(Link), Link }, { nameof(FullPath), TempFullPath }, - { nameof(Filename), Filename }, - { nameof(Extension), Extension }, + { nameof(Filename), Path.GetFileNameWithoutExtension(TempFullPath) }, + { nameof(Extension), Path.GetExtension(TempFullPath) }, { nameof(DefiningProjectDirectory), DefiningProjectDirectory }, { definingProjectFullPath, GetMetadata(definingProjectFullPath) }, { definingProjectName, GetMetadata(definingProjectName) }, @@ -91,24 +94,41 @@ TaskItem GetTaskItem() void Verify() { - if (string.IsNullOrWhiteSpace(DefiningProjectDirectory) || string.IsNullOrWhiteSpace(FullPath)) - throw new Exception(); + if (DefiningProjectDirectory.IsEmpty() || FullPath.IsEmpty()) + throw new KeyNotFoundException($"{nameof(DefiningProjectDirectory)} or {nameof(FullPath)} not found in {item.ToString(null)}"); + + if (!File.Exists(FullPath)) + throw new FileNotFoundException(FullPath); + + if (!Directory.Exists(DefiningProjectDirectory)) + throw new DirectoryNotFoundException(DefiningProjectDirectory); + + VerifyFileNameWithExtension(); - if (string.IsNullOrWhiteSpace(Link)) - SetLink(); + if (Link.IsEmpty()) + Link = FullPath.StartsWith(DefiningProjectDirectory) + ? FullPath.Replace(DefiningProjectDirectory, string.Empty) + : $"{Filename}{Extension}"; - if (string.IsNullOrWhiteSpace(Link)) - throw new Exception(); + if (Link.IsEmpty()) + throw new ArgumentException($"The relative {nameof(Link)} of the file location in the project could not be determined in {item.ToString(null)}"); TempFullPath = Path.Combine(tempOutputPath, Link); } - void SetLink() + void VerifyFileNameWithExtension() { - var locatedInProject = FullPath.StartsWith(DefiningProjectDirectory); - Link = locatedInProject - ? FullPath.Replace(DefiningProjectDirectory, string.Empty) - : $"{Filename}{Extension}"; + if (Filename.IsEmpty()) + Filename = Path.GetFileNameWithoutExtension(FullPath); + + if (Extension.IsEmpty()) + Extension = Path.GetExtension(FullPath); + + if (!Extension.IsEmpty() && !Extension.StartsWith(dotChar)) + Extension = dotChar + Extension; + + if (Filename.IsEmpty() && Extension.IsEmpty()) + throw new ArgumentException($"The {nameof(Filename)} and {nameof(Extension)} could not be determined in {item.ToString(null)}"); } } } diff --git a/src/CompactJson/MSBuild.CompactJsonResources.csproj b/src/CompactJson/MSBuild.CompactJsonResources.csproj index dde8115..fd1c159 100644 --- a/src/CompactJson/MSBuild.CompactJsonResources.csproj +++ b/src/CompactJson/MSBuild.CompactJsonResources.csproj @@ -5,23 +5,24 @@ true true false + 9 NU5100;NU5128 MSBuild.CompactJsonResources MSBuild, Json - 1.0.0-preview1 + 1.0.0-preview2 dimonovdd dimonovdd - Compact Json data, remove all whitespaces before build + Compact Json data by removing whitespaces before building https://github.com/dimonovdd/MSBuild.CompactJsonResources See: https://github.com/dimonovdd/MSBuild.CompactJsonResources/releases LICENSE icon.png - - - + + + diff --git a/src/TestMSBuildTasks/TestMSBuildTasks.csproj b/src/TestMSBuildTasks/TestMSBuildTasks.csproj index 349c88f..0c90450 100644 --- a/src/TestMSBuildTasks/TestMSBuildTasks.csproj +++ b/src/TestMSBuildTasks/TestMSBuildTasks.csproj @@ -4,7 +4,7 @@ Exe net5.0 false - true + false @@ -13,8 +13,8 @@ - - + + + + \ No newline at end of file