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

[tools] Fix issue when serialized json file is null #18565

Merged
merged 1 commit into from
Jun 17, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/Tools.Common/Models/ModuleMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override bool Equals(Object obj)

public static ModuleMetadata DeserializeCmdlets(string fileName)
{
return JsonConvert.DeserializeObject<ModuleMetadata>(File.ReadAllText(fileName));
return File.Exists(fileName) ? JsonConvert.DeserializeObject<ModuleMetadata>(File.ReadAllText(fileName)) : null;
}

public override int GetHashCode()
Expand Down
8 changes: 5 additions & 3 deletions tools/VersionController/Models/VersionMetadataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public VersionMetadataHelper(VersionFileHelper fileHelper)
/// <returns></returns>
private ModuleMetadata DeserializeCmdlets(string fileName)
{
return JsonConvert.DeserializeObject<ModuleMetadata>(File.ReadAllText(fileName));
return File.Exists(fileName) ? JsonConvert.DeserializeObject<ModuleMetadata>(File.ReadAllText(fileName)) : null;
}

/// <summary>
/// Serialize the cmdlets so they can be compared to change modules later
/// </summary>
/// <param name="fileName">Name of the file cmdlets are being serialized to.</param>
/// <param name="cmdlets">List of cmdlets that are to be serialized.</param>
public static void SerializeCmdlets(string fileName, ModuleMetadata moduleMetadata)
public static string SerializeCmdlets(string fileName, ModuleMetadata moduleMetadata)
{
string json = JsonConvert.SerializeObject(moduleMetadata, new JsonSerializerSettings
{
Expand All @@ -67,6 +67,7 @@ public static void SerializeCmdlets(string fileName, ModuleMetadata moduleMetada
ContractResolver = VersionMetadataContractResolver.Instance
});
File.WriteAllText(fileName, json);
return fileName;
}

/// <summary>
Expand Down Expand Up @@ -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;
Expand Down