<commit_before>﻿namespace Paket

open System.Xml
open System.IO

[<RequireQualifiedAccess>]
type NuspecReferences = 
    | All
    | Explicit of string list

type FrameworkAssemblyReference = {
    AssemblyName: string
    TargetFramework : string}

type Nuspec = 
    { References : NuspecReferences 
      FrameworkAssemblyReferences : FrameworkAssemblyReference list}
    static member All = { References = NuspecReferences.All; FrameworkAssemblyReferences = []}
    static member Load(fileName : string) = 
        let fi = FileInfo(fileName)
        if not fi.Exists then Nuspec.All
        else 
            let doc = new XmlDocument()
            doc.Load fi.FullName
            let manager = new XmlNamespaceManager(doc.NameTable)
            manager.AddNamespace("ns1", "http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd")
            manager.AddNamespace("ns2", "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd")
            manager.AddNamespace("ns3", "http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd")

            let referencesNodes = 
                [ for node in doc.SelectNodes("//ns1:references", manager) do
                    yield node
                  for node in doc.SelectNodes("//ns2:references", manager) do
                    yield node
                  for node in doc.SelectNodes("//ns3:references", manager) do
                    yield node]

            let references =
                if List.isEmpty referencesNodes then NuspecReferences.All else 
                let files = 
                    [ for node in doc.SelectNodes("//ns1:reference", manager) do
                        yield node.Attributes.["file"].InnerText                      
                      for node in doc.SelectNodes("//ns2:reference", manager) do
                        yield node.Attributes.["file"].InnerText 
                        for node in doc.SelectNodes("//ns3:reference", manager) do
                        yield node.Attributes.["file"].InnerText ]
                NuspecReferences.Explicit files

            let frameworkAssemblyNodes = 
                [ for node in doc.SelectNodes("//ns1:frameworkAssemblies", manager) do
                    yield node
                  for node in doc.SelectNodes("//ns2:frameworkAssemblies", manager) do
                    yield node
                  for node in doc.SelectNodes("//ns3:frameworkAssemblies", manager) do
                    yield node ]

            let frameworkAssemblyReferences = 
                if List.isEmpty frameworkAssemblyNodes then [] else 
                [ for node in doc.SelectNodes("//ns1:frameworkAssembly", manager) do
                      yield { AssemblyName = node.Attributes.["assemblyName"].InnerText
                              TargetFramework = node.Attributes.["targetFramework"].InnerText }
                  for node in doc.SelectNodes("//ns2:frameworkAssembly", manager) do
                      yield { AssemblyName = node.Attributes.["assemblyName"].InnerText
                              TargetFramework = node.Attributes.["targetFramework"].InnerText } 
                  for node in doc.SelectNodes("//ns3:frameworkAssembly", manager) do
                      yield { AssemblyName = node.Attributes.["assemblyName"].InnerText
                              TargetFramework = node.Attributes.["targetFramework"].InnerText }                               ]

            { References = references; FrameworkAssemblyReferences = frameworkAssemblyReferences }<commit_msg>Extract KnownNamespaces in nuspec - references #241<commit_after>﻿namespace Paket

open System.Xml
open System.IO

[<RequireQualifiedAccess>]
type NuspecReferences = 
    | All
    | Explicit of string list

type FrameworkAssemblyReference = {
    AssemblyName: string
    TargetFramework : string}

type Nuspec = 
    { References : NuspecReferences 
      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"]

    static member All = { References = NuspecReferences.All; FrameworkAssemblyReferences = []}
    static member Load(fileName : string) = 
        let fi = FileInfo(fileName)
        if not fi.Exists then Nuspec.All
        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 getReferences ns =
                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) -> 
                        node.Attributes.["file"].InnerText]

            let getframeworkAssemblyReferences ns =
                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) -> 
                        { AssemblyName = node.Attributes.["assemblyName"].InnerText
                          TargetFramework = node.Attributes.["targetFramework"].InnerText }]

            let references =
                Nuspec.KnownNamespaces
                |> List.map (fun (name,ns) -> getReferences name)
                |> List.concat
            
            let frameworkAssemblyReferences =
                Nuspec.KnownNamespaces
                |> List.map (fun (name,ns) -> getframeworkAssemblyReferences name)
                |> List.concat

            { References = if references = [] then NuspecReferences.All else NuspecReferences.Explicit references; 
              FrameworkAssemblyReferences = frameworkAssemblyReferences }
{'fraction_non_alphanumeric': 0.09184180043024988, 'fraction_numerical': 0.009432401125268906, 'mean_word_length': 1.9729463846532218, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2193843', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1419, 'n_words': 344}