Skip to content

Commit

Permalink
Support removing files that are outside the fsproj directory
Browse files Browse the repository at this point in the history
  • Loading branch information
MangelMaxime committed Aug 26, 2022
1 parent 680a95b commit a324a03
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/FsAutoComplete.Core/FsprojEdit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,27 @@ module FsProjEditor =

xdoc.Save fsprojPath

let removeFile (fsprojPath: string) (fileNameToRemove: string) =
let removeFile (fsprojPath: string) (fileToRemove: string) =
let xdoc = System.Xml.XmlDocument()
xdoc.Load fsprojPath
let xpath = sprintf "//Compile[@Include='%s']/.." fileNameToRemove
let itemGroup = xdoc.SelectSingleNode(xpath)
let childXPath = sprintf "//Compile[@Include='%s']" fileNameToRemove
let node = itemGroup.SelectSingleNode(childXPath)
let sanitazedFileToRemove = fileToRemove.Replace("\\", "/")

let nodeToRemoveOpt =
// Take all the <Compile Include="..." /> nodes
xdoc.SelectNodes("//Compile[@Include]")
|> Seq.cast<System.Xml.XmlNode>
// Find the node that match the file we want to remove
// Note: We sanitaze the file name path because it can changes depending on the OS
// and also on if the user used / or \ as the separator
|> Seq.tryFind (fun node ->
let sanitazedInclude = node.Attributes.["Include"].InnerText.Replace("\\", "/")
sanitazedInclude = sanitazedFileToRemove)

match nodeToRemoveOpt with
| Some nodeToRemove ->
nodeToRemove.ParentNode.RemoveChild(nodeToRemove) |> ignore
xdoc.Save fsprojPath

// If the node is not found do nothing
if isNull node then
| None ->
// Node not found, do nothing
()
else
itemGroup.RemoveChild node |> ignore
xdoc.Save fsprojPath

0 comments on commit a324a03

Please sign in to comment.