From 5fe28a3d70d36ebc7566e25f4d8f746b09d111ca Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Fri, 17 Jun 2022 15:48:01 +0800 Subject: [PATCH] [tools] Fix issue when serialized json file is null (#18565) --- tools/Tools.Common/Models/ModuleMetadata.cs | 2 +- tools/VersionController/Models/VersionMetadataHelper.cs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/Tools.Common/Models/ModuleMetadata.cs b/tools/Tools.Common/Models/ModuleMetadata.cs index 561927fd3bc9..a068aa372705 100644 --- a/tools/Tools.Common/Models/ModuleMetadata.cs +++ b/tools/Tools.Common/Models/ModuleMetadata.cs @@ -99,7 +99,7 @@ public override bool Equals(Object obj) public static ModuleMetadata DeserializeCmdlets(string fileName) { - return JsonConvert.DeserializeObject(File.ReadAllText(fileName)); + return File.Exists(fileName) ? JsonConvert.DeserializeObject(File.ReadAllText(fileName)) : null; } public override int GetHashCode() diff --git a/tools/VersionController/Models/VersionMetadataHelper.cs b/tools/VersionController/Models/VersionMetadataHelper.cs index 7d886545e863..ef71fc468ad5 100644 --- a/tools/VersionController/Models/VersionMetadataHelper.cs +++ b/tools/VersionController/Models/VersionMetadataHelper.cs @@ -50,7 +50,7 @@ public VersionMetadataHelper(VersionFileHelper fileHelper) /// private ModuleMetadata DeserializeCmdlets(string fileName) { - return JsonConvert.DeserializeObject(File.ReadAllText(fileName)); + return File.Exists(fileName) ? JsonConvert.DeserializeObject(File.ReadAllText(fileName)) : null; } /// @@ -58,7 +58,7 @@ private ModuleMetadata DeserializeCmdlets(string fileName) /// /// Name of the file cmdlets are being serialized to. /// List of cmdlets that are to be serialized. - public static void SerializeCmdlets(string fileName, ModuleMetadata moduleMetadata) + public static string SerializeCmdlets(string fileName, ModuleMetadata moduleMetadata) { string json = JsonConvert.SerializeObject(moduleMetadata, new JsonSerializerSettings { @@ -67,6 +67,7 @@ public static void SerializeCmdlets(string fileName, ModuleMetadata moduleMetada ContractResolver = VersionMetadataContractResolver.Instance }); File.WriteAllText(fileName, json); + return fileName; } /// @@ -297,7 +298,8 @@ public Version GetVersionBumpUsingSerialized() Console.WriteLine($"Warning: {moduleName} does not have a previously serialized cmdlet for comparison."); Console.ForegroundColor = currentColor; var newCmdletFile = Path.Join(serializedCmdletsDirectory, serializedCmdletName); - SerializeCmdlets(newCmdletFile, newModuleMetadata); + // Use newly build one, only bump PATCH version + serializedCmdletFile = SerializeCmdlets(newCmdletFile, newModuleMetadata); } var oldModuleMetadata = DeserializeCmdlets(serializedCmdletFile); CmdletLoader.ModuleMetadata = oldModuleMetadata;