Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support removing files that are outside the fsproj directory #1001

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/FsAutoComplete.Core/FsprojEdit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,27 @@ module FsProjEditor =

addFile fsprojPath relativePath

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