Skip to content

Commit

Permalink
Merge pull request #2296 from fsprojects/fix_2253_multiple_nuspec_in_…
Browse files Browse the repository at this point in the history
…fix_nuspec

accept multiple nuspec files in fix-nuspec, fixes
  • Loading branch information
forki committed Apr 27, 2017
2 parents ebbae74 + 653d5f8 commit d99c8f6
Showing 1 changed file with 58 additions and 54 deletions.
112 changes: 58 additions & 54 deletions src/Paket/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -265,63 +265,67 @@ let fixNuspec silent (results : ParseResults<_>) =
| None ->
failwithf "Please specify the nuspec file with the 'file' parameter."

| Some nuspecFileName ->
if not (File.Exists nuspecFileName) then
failwithf "Specified file '%s' does not exist." nuspecFileName

let nuspecText = File.ReadAllText nuspecFileName

let doc =
try
let doc = Xml.XmlDocument()
doc.LoadXml nuspecText
doc
with
| exn -> failwithf "Could not parse nuspec file '%s'.%sMessage: %s" nuspecFileName Environment.NewLine exn.Message

match results.TryGetResult <@ FixNuspecArgs.ReferencesFile @> with
| None ->
failwithf "Please specify the references-file with the 'references-file' parameter."

| Some referencesFileName ->
if not (File.Exists referencesFileName) then
failwithf "Specified references-file '%s' does not exist." referencesFileName

let referencesText = File.ReadAllLines referencesFileName
let transitiveReferences =
referencesText
|> Array.map (fun l -> l.Split [|','|])
|> Array.choose (fun x ->
if x.[2] = "Transitive" then
Some x.[0]
else
None)
|> Set.ofArray

let rec traverse (parent:XmlNode) =
let nodesToRemove = System.Collections.Generic.List<_>()
for node in parent.ChildNodes do
if node.Name = "dependency" then
let packageName =
match node.Attributes.["id"] with
| null -> ""
| x -> x.InnerText

if transitiveReferences.Contains packageName then
nodesToRemove.Add node |> ignore

if nodesToRemove.Count = 0 then
| Some nuspecFileNames ->
// if multiple, msbuild will join them via ';'
let fileNameSplits = nuspecFileNames.Split([|';'|])
for nuspecFileName in fileNameSplits do
if not (File.Exists nuspecFileName) then
failwithf "Specified file '%s' does not exist." nuspecFileName

for nuspecFileName in fileNameSplits do
let nuspecText = File.ReadAllText nuspecFileName

let doc =
try
let doc = Xml.XmlDocument()
doc.LoadXml nuspecText
doc
with
| exn -> failwithf "Could not parse nuspec file '%s'.%sMessage: %s" nuspecFileName Environment.NewLine exn.Message

match results.TryGetResult <@ FixNuspecArgs.ReferencesFile @> with
| None ->
failwithf "Please specify the references-file with the 'references-file' parameter."

| Some referencesFileName ->
if not (File.Exists referencesFileName) then
failwithf "Specified references-file '%s' does not exist." referencesFileName

let referencesText = File.ReadAllLines referencesFileName
let transitiveReferences =
referencesText
|> Array.map (fun l -> l.Split [|','|])
|> Array.choose (fun x ->
if x.[2] = "Transitive" then
Some x.[0]
else
None)
|> Set.ofArray

let rec traverse (parent:XmlNode) =
let nodesToRemove = System.Collections.Generic.List<_>()
for node in parent.ChildNodes do
traverse node
else
for node in nodesToRemove do
parent.RemoveChild node |> ignore

traverse doc
if node.Name = "dependency" then
let packageName =
match node.Attributes.["id"] with
| null -> ""
| x -> x.InnerText

if transitiveReferences.Contains packageName then
nodesToRemove.Add node |> ignore

if nodesToRemove.Count = 0 then
for node in parent.ChildNodes do
traverse node
else
for node in nodesToRemove do
parent.RemoveChild node |> ignore

traverse doc

use fileStream = File.Open(nuspecFileName, FileMode.Create)
use fileStream = File.Open(nuspecFileName, FileMode.Create)

doc.Save(fileStream)
doc.Save(fileStream)


// separated out from showInstalledPackages to allow Paket.PowerShell to get the types
Expand Down

0 comments on commit d99c8f6

Please sign in to comment.