Skip to content

Commit

Permalink
Merge pull request #315 from ryansroberts/master
Browse files Browse the repository at this point in the history
Use namespace agnostic xpath
  • Loading branch information
forki committed Oct 30, 2014
2 parents 1c3e550 + 76e81a2 commit a50e2ef
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions src/Paket.Core/Nuspec.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ type Nuspec =
Dependencies : (string * VersionRequirement) list
OfficialName : string
FrameworkAssemblyReferences : FrameworkAssemblyReference list }
static member KnownNamespaces =
["ns1","http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"
"ns2","http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"
"ns3","http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd"
"ns4","http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"
"ns5","http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd"]

static member All = { References = NuspecReferences.All; Dependencies = []; FrameworkAssemblyReferences = []; OfficialName = "" }
static member Explicit references = { References = NuspecReferences.Explicit references; Dependencies = []; FrameworkAssemblyReferences = []; OfficialName = "" }
Expand All @@ -99,22 +93,9 @@ type Nuspec =
else
let doc = new XmlDocument()
doc.Load fi.FullName
let manager = new XmlNamespaceManager(doc.NameTable)

Nuspec.KnownNamespaces
|> List.iter (fun (name,ns) -> manager.AddNamespace(name, ns))

let ns =
let packageNs = manager.LookupPrefix(doc.LastChild.NamespaceURI)
if String.IsNullOrEmpty packageNs then
let metaDataNs = manager.LookupPrefix(doc.LastChild.LastChild.NamespaceURI)
if String.IsNullOrEmpty metaDataNs then
failwithf "Unrecognized namespace in nuspec file %s" fileName
else metaDataNs
else packageNs

let dependencies =
doc.SelectNodes(sprintf "//%s:metadata/%s:dependencies/%s:dependency" ns ns ns, manager)
doc.SelectNodes "//*[local-name() = 'metadata']/*[local-name() = 'dependencies']/*[local-name() = 'dependency']"
|> Seq.cast<XmlNode>
|> Seq.map (fun node ->
let name = node.Attributes.["id"].Value
Expand All @@ -127,19 +108,19 @@ type Nuspec =
|> Seq.toList

let officialName =
doc.SelectNodes(sprintf "//%s:metadata/%s:id" ns ns, manager)
doc.SelectNodes "//*[local-name() = 'metadata']/*[local-name() = 'id']"
|> Seq.cast<XmlNode>
|> Seq.head
|> fun node -> node.InnerText

let references =
if List.isEmpty [ for node in doc.SelectNodes(sprintf "//%s:references" ns, manager) -> node] then [] else
[ for node in doc.SelectNodes(sprintf "//%s:reference" ns, manager) ->
if List.isEmpty [ for node in doc.SelectNodes "//*[local-name() = 'references']" -> node] then [] else
[ for node in doc.SelectNodes "//*[local-name() = 'reference']" ->
node.Attributes.["file"].InnerText]

let frameworkAssemblyReferences =
if List.isEmpty [ for node in doc.SelectNodes(sprintf "//%s:frameworkAssemblies" ns, manager) -> node] then [] else
[ for node in doc.SelectNodes(sprintf "//%s:frameworkAssembly" ns, manager) do
if List.isEmpty [ for node in doc.SelectNodes "//*[local-name() = 'frameworkAssemblies']" -> node] then [] else
[ for node in doc.SelectNodes "//*[local-name() = 'frameworkAssembly']" do
let name = node.Attributes.["assemblyName"].InnerText
for framework in node.Attributes.["targetFramework"].InnerText.Split([|','; ' '|],System.StringSplitOptions.RemoveEmptyEntries) do
match FrameworkIdentifier.Extract framework with
Expand All @@ -149,4 +130,4 @@ type Nuspec =
{ References = if references = [] then NuspecReferences.All else NuspecReferences.Explicit references
Dependencies = dependencies
OfficialName = officialName
FrameworkAssemblyReferences = frameworkAssemblyReferences }
FrameworkAssemblyReferences = frameworkAssemblyReferences }

0 comments on commit a50e2ef

Please sign in to comment.