Skip to content

Commit 2d95d00

Browse files
committed
refactor(ci): parse NuGet.config with System.Xml.Linq
1 parent d86eac4 commit 2d95d00

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

build/build.fs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
open System
2-
open System.Xml
2+
open System.Xml.Linq
33
open Fake.Core
44
open Fake.DotNet
55
open Fake.Tools
@@ -211,34 +211,33 @@ module NuGetConfig =
211211

212212
try
213213
if IO.File.Exists nugetConfigPath then
214-
let doc = XmlDocument()
215-
doc.Load(nugetConfigPath)
214+
let doc = XDocument.Load(nugetConfigPath)
216215

217-
let configNode = doc.SelectSingleNode("//configuration")
218-
if configNode <> null then
216+
match doc.Root with
217+
| null ->
218+
Trace.logf "Warning: Invalid XML structure in %s" nugetConfigPath
219+
| configRoot ->
219220
// Remove existing packageSourceMapping if it exists
220-
let existingMapping = configNode.SelectSingleNode("packageSourceMapping")
221-
if existingMapping <> null then
222-
configNode.RemoveChild(existingMapping) |> ignore
221+
configRoot.Element(XName.Get("packageSourceMapping"))
222+
|> ValueOption.ofObj
223+
|> ValueOption.iter (fun existingMapping -> existingMapping.Remove())
223224

224225
// Create new packageSourceMapping element
225-
let packageSourceMapping = doc.CreateElement("packageSourceMapping")
226+
let packageSourceMapping = XElement(XName.Get("packageSourceMapping"))
226227

227228
// Create nuget.org source mapping
228-
let nugetSource = doc.CreateElement("packageSource")
229-
nugetSource.SetAttribute("key", "nuget.org")
229+
let nugetSource = XElement(XName.Get("packageSource"))
230+
nugetSource.SetAttributeValue(XName.Get("key"), "nuget.org")
230231

231-
let nugetPattern = doc.CreateElement("package")
232-
nugetPattern.SetAttribute("pattern", "*")
232+
let nugetPattern = XElement(XName.Get("package"))
233+
nugetPattern.SetAttributeValue(XName.Get("pattern"), "*")
233234

234-
nugetSource.AppendChild(nugetPattern) |> ignore
235-
packageSourceMapping.AppendChild(nugetSource) |> ignore
236-
configNode.AppendChild(packageSourceMapping) |> ignore
235+
nugetSource.Add(nugetPattern)
236+
packageSourceMapping.Add(nugetSource)
237+
configRoot.Add(packageSourceMapping)
237238

238239
doc.Save(nugetConfigPath)
239240
Trace.log "Successfully updated NuGet package source mapping"
240-
else
241-
Trace.logf "Warning: Could not find configuration node in %s" nugetConfigPath
242241
else
243242
Trace.logf "Warning: NuGet config file not found at %s" nugetConfigPath
244243
with

0 commit comments

Comments
 (0)