|
152 | 152 | </ParameterGroup> |
153 | 153 | <Task> |
154 | 154 | <Using Namespace="System.IO" /> |
155 | | - <Using Namespace="System.Text.Json" /> |
| 155 | + <Using Namespace="System.Text" /> |
| 156 | + <Using Namespace="System.Text.RegularExpressions" /> |
156 | 157 | <Code Type="Fragment" Language="cs"> |
157 | 158 | <![CDATA[ |
158 | 159 | try |
159 | 160 | { |
160 | | - var jsonString = File.ReadAllText(SbomPath); |
161 | | - var options = new JsonDocumentOptions { AllowTrailingCommas = true }; |
162 | | - using var document = JsonDocument.Parse(jsonString, options); |
163 | | - var root = document.RootElement; |
| 161 | + var jsonContent = File.ReadAllText(SbomPath); |
164 | 162 |
|
165 | | - // Check if version field exists |
166 | | - if (!root.TryGetProperty("version", out _)) |
| 163 | + // Simple check if version field already exists using regex |
| 164 | + // Look for "version" field at the root level (after opening brace, before other fields) |
| 165 | + var versionPattern = @"^\s*\{\s*""version""\s*:"; |
| 166 | + if (Regex.IsMatch(jsonContent, versionPattern, RegexOptions.Multiline)) |
167 | 167 | { |
168 | | - // Create a new JSON object with version field |
169 | | - using var outputStream = new MemoryStream(); |
170 | | - using (var writer = new Utf8JsonWriter(outputStream, new JsonWriterOptions { Indented = true })) |
171 | | - { |
172 | | - writer.WriteStartObject(); |
173 | | -
|
174 | | - // Write version field first |
175 | | - writer.WriteNumber("version", 1); |
| 168 | + Log.LogMessage(MessageImportance.Low, "[CycloneDX] SBOM already has version field"); |
| 169 | + return true; |
| 170 | + } |
176 | 171 |
|
177 | | - // Copy all existing properties from the already-parsed document |
178 | | - foreach (var property in root.EnumerateObject()) |
179 | | - { |
180 | | - property.WriteTo(writer); |
181 | | - } |
| 172 | + // Version field doesn't exist, add it |
| 173 | + // Find the opening brace and insert version field right after it |
| 174 | + var insertPattern = @"^(\s*\{)\s*$"; |
| 175 | + var replacement = "$1\n \"version\": 1,"; |
| 176 | + var updatedContent = Regex.Replace(jsonContent, insertPattern, replacement, RegexOptions.Multiline); |
182 | 177 |
|
183 | | - writer.WriteEndObject(); |
| 178 | + // If the pattern didn't match (single-line opening), try a simpler approach |
| 179 | + if (updatedContent == jsonContent) |
| 180 | + { |
| 181 | + // Handle compact JSON: { "bomFormat": ... } |
| 182 | + if (jsonContent.TrimStart().StartsWith("{")) |
| 183 | + { |
| 184 | + var firstBraceIndex = jsonContent.IndexOf('{'); |
| 185 | + updatedContent = jsonContent.Substring(0, firstBraceIndex + 1) + |
| 186 | + "\n \"version\": 1," + |
| 187 | + jsonContent.Substring(firstBraceIndex + 1); |
184 | 188 | } |
| 189 | + } |
185 | 190 |
|
186 | | - // Write back to file |
187 | | - File.WriteAllBytes(SbomPath, outputStream.ToArray()); |
| 191 | + // Write back to file if content changed |
| 192 | + if (updatedContent != jsonContent) |
| 193 | + { |
| 194 | + File.WriteAllText(SbomPath, updatedContent, Encoding.UTF8); |
188 | 195 | Log.LogMessage(MessageImportance.Low, "[CycloneDX] Added version field to SBOM"); |
189 | 196 | } |
190 | 197 | else |
191 | 198 | { |
192 | | - Log.LogMessage(MessageImportance.Low, "[CycloneDX] SBOM already has version field"); |
| 199 | + Log.LogWarning("[CycloneDX] Could not add version field - unexpected JSON format"); |
193 | 200 | } |
194 | 201 | } |
195 | 202 | catch (Exception ex) |
|
0 commit comments