|
8 | 8 | using Microsoft.Build.Framework; |
9 | 9 | using Microsoft.Build.Utilities; |
10 | 10 |
|
11 | | -namespace Nerdbank.GitVersioning.Tasks |
| 11 | +namespace Nerdbank.GitVersioning.Tasks; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// MSBuild task that stamps version information into an MCP server.json file. |
| 15 | +/// </summary> |
| 16 | +public class StampMcpServerJson : Microsoft.Build.Utilities.Task |
12 | 17 | { |
13 | 18 | /// <summary> |
14 | | - /// MSBuild task that stamps version information into an MCP server.json file. |
| 19 | + /// Gets or sets the path to the source server.json file. |
15 | 20 | /// </summary> |
16 | | - public class StampMcpServerJson : Microsoft.Build.Utilities.Task |
17 | | -{ |
18 | | - /// <summary> |
19 | | - /// Gets or sets the path to the source server.json file. |
20 | | - /// </summary> |
21 | | - [Required] |
22 | | - public string SourceServerJson { get; set; } |
23 | | - |
24 | | - /// <summary> |
25 | | - /// Gets or sets the path where the stamped server.json file should be written. |
26 | | - /// </summary> |
27 | | - [Required] |
28 | | - public string OutputServerJson { get; set; } |
29 | | - |
30 | | - /// <summary> |
31 | | - /// Gets or sets the version to stamp into the server.json file. |
32 | | - /// </summary> |
33 | | - [Required] |
34 | | - public string Version { get; set; } |
35 | | - |
36 | | - /// <summary> |
37 | | - /// Executes the task to stamp version information into the MCP server.json file. |
38 | | - /// </summary> |
39 | | - /// <returns><see langword="true"/> if the task succeeded; <see langword="false"/> otherwise.</returns> |
40 | | - public override bool Execute() |
| 21 | + [Required] |
| 22 | + public string SourceServerJson { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets the path where the stamped server.json file should be written. |
| 26 | + /// </summary> |
| 27 | + [Required] |
| 28 | + public string OutputServerJson { get; set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets or sets the version to stamp into the server.json file. |
| 32 | + /// </summary> |
| 33 | + [Required] |
| 34 | + public string Version { get; set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Executes the task to stamp version information into the MCP server.json file. |
| 38 | + /// </summary> |
| 39 | + /// <returns><see langword="true"/> if the task succeeded; <see langword="false"/> otherwise.</returns> |
| 40 | + public override bool Execute() |
| 41 | + { |
| 42 | + try |
41 | 43 | { |
42 | | - try |
| 44 | + if (string.IsNullOrEmpty(this.SourceServerJson) || string.IsNullOrEmpty(this.OutputServerJson) || string.IsNullOrEmpty(this.Version)) |
43 | 45 | { |
44 | | - if (string.IsNullOrEmpty(this.SourceServerJson) || string.IsNullOrEmpty(this.OutputServerJson) || string.IsNullOrEmpty(this.Version)) |
45 | | - { |
46 | | - this.Log.LogError("SourceServerJson, OutputServerJson, and Version are required parameters."); |
47 | | - return false; |
48 | | - } |
| 46 | + this.Log.LogError("SourceServerJson, OutputServerJson, and Version are required parameters."); |
| 47 | + return !this.Log.HasLoggedErrors; |
| 48 | + } |
49 | 49 |
|
50 | | - if (!File.Exists(this.SourceServerJson)) |
51 | | - { |
52 | | - this.Log.LogError($"Source server.json file not found: {this.SourceServerJson}"); |
53 | | - return false; |
54 | | - } |
| 50 | + if (!File.Exists(this.SourceServerJson)) |
| 51 | + { |
| 52 | + this.Log.LogError($"Source server.json file not found: {this.SourceServerJson}"); |
| 53 | + return !this.Log.HasLoggedErrors; |
| 54 | + } |
55 | 55 |
|
56 | | - // Ensure output directory exists |
57 | | - string outputDir = Path.GetDirectoryName(this.OutputServerJson); |
58 | | - if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) |
59 | | - { |
60 | | - Directory.CreateDirectory(outputDir); |
61 | | - } |
| 56 | + // Ensure output directory exists |
| 57 | + string outputDir = Path.GetDirectoryName(this.OutputServerJson); |
| 58 | + if (!string.IsNullOrEmpty(outputDir)) |
| 59 | + { |
| 60 | + Directory.CreateDirectory(outputDir); |
| 61 | + } |
| 62 | + |
| 63 | + // Read and parse the server.json file |
| 64 | + string jsonContent = File.ReadAllText(this.SourceServerJson); |
| 65 | + JsonNode jsonNode = JsonNode.Parse(jsonContent); |
62 | 66 |
|
63 | | - // Read and parse the server.json file |
64 | | - string jsonContent = File.ReadAllText(this.SourceServerJson); |
65 | | - JsonNode jsonNode = JsonNode.Parse(jsonContent); |
| 67 | + if (jsonNode is JsonObject jsonObject) |
| 68 | + { |
| 69 | + // Replace all __VERSION__ placeholders in the JSON tree |
| 70 | + this.ReplaceVersionPlaceholders(jsonNode, this.Version); |
66 | 71 |
|
67 | | - if (jsonNode is JsonObject jsonObject) |
| 72 | + // Write the updated JSON with indentation for readability |
| 73 | + var options = new JsonSerializerOptions |
68 | 74 | { |
69 | | - // Stamp the version |
70 | | - jsonObject["version"] = this.Version; |
| 75 | + WriteIndented = true, |
| 76 | + }; |
71 | 77 |
|
72 | | - // Write the updated JSON with indentation for readability |
73 | | - var options = new JsonSerializerOptions |
74 | | - { |
75 | | - WriteIndented = true, |
76 | | - }; |
| 78 | + string updatedJson = JsonSerializer.Serialize(jsonObject, options); |
| 79 | + File.WriteAllText(this.OutputServerJson, updatedJson); |
| 80 | + |
| 81 | + this.Log.LogMessage(MessageImportance.Low, $"Stamped version '{this.Version}' into server.json: {this.OutputServerJson}"); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + this.Log.LogError($"server.json does not contain a valid JSON object: {this.SourceServerJson}"); |
| 86 | + } |
| 87 | + } |
| 88 | + catch (Exception ex) |
| 89 | + { |
| 90 | + this.Log.LogErrorFromException(ex); |
| 91 | + } |
| 92 | + |
| 93 | + return !this.Log.HasLoggedErrors; |
| 94 | + } |
77 | 95 |
|
78 | | - string updatedJson = JsonSerializer.Serialize(jsonObject, options); |
79 | | - File.WriteAllText(this.OutputServerJson, updatedJson); |
| 96 | + /// <summary> |
| 97 | + /// Recursively walks the JSON tree and replaces any string values containing "__VERSION__" with the actual version. |
| 98 | + /// </summary> |
| 99 | + /// <param name="node">The JSON node to process.</param> |
| 100 | + /// <param name="version">The version string to replace "__VERSION__" with.</param> |
| 101 | + private void ReplaceVersionPlaceholders(JsonNode node, string version) |
| 102 | + { |
| 103 | + switch (node) |
| 104 | + { |
| 105 | + case JsonObject jsonObject: |
| 106 | + foreach (var property in jsonObject.ToArray()) |
| 107 | + { |
| 108 | + if (property.Value != null) |
| 109 | + { |
| 110 | + this.ReplaceVersionPlaceholders(property.Value, version); |
| 111 | + } |
| 112 | + } |
| 113 | + break; |
80 | 114 |
|
81 | | - this.Log.LogMessage(MessageImportance.Low, $"Stamped version '{this.Version}' into server.json: {this.OutputServerJson}"); |
82 | | - return true; |
| 115 | + case JsonArray jsonArray: |
| 116 | + for (int i = 0; i < jsonArray.Count; i++) |
| 117 | + { |
| 118 | + if (jsonArray[i] != null) |
| 119 | + { |
| 120 | + this.ReplaceVersionPlaceholders(jsonArray[i], version); |
| 121 | + } |
83 | 122 | } |
84 | | - else |
| 123 | + break; |
| 124 | + |
| 125 | + case JsonValue jsonValue: |
| 126 | + if (jsonValue.TryGetValue<string>(out string stringValue) && stringValue.Contains("__VERSION__")) |
85 | 127 | { |
86 | | - this.Log.LogError($"server.json does not contain a valid JSON object: {this.SourceServerJson}"); |
87 | | - return false; |
| 128 | + string replacedValue = stringValue.Replace("__VERSION__", version); |
| 129 | + JsonNode parent = jsonValue.Parent; |
| 130 | + if (parent is JsonObject parentObject) |
| 131 | + { |
| 132 | + // Find the property key for this value |
| 133 | + foreach (var kvp in parentObject) |
| 134 | + { |
| 135 | + if (ReferenceEquals(kvp.Value, jsonValue)) |
| 136 | + { |
| 137 | + parentObject[kvp.Key] = replacedValue; |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + else if (parent is JsonArray parentArray) |
| 143 | + { |
| 144 | + // Find the index for this value |
| 145 | + for (int i = 0; i < parentArray.Count; i++) |
| 146 | + { |
| 147 | + if (ReferenceEquals(parentArray[i], jsonValue)) |
| 148 | + { |
| 149 | + parentArray[i] = replacedValue; |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
88 | 154 | } |
89 | | - } |
90 | | - catch (Exception ex) |
91 | | - { |
92 | | - this.Log.LogErrorFromException(ex); |
93 | | - return false; |
94 | | - } |
| 155 | + break; |
95 | 156 | } |
96 | 157 | } |
97 | 158 | } |
0 commit comments