-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathFsprojEdit.fs
186 lines (154 loc) · 7 KB
/
FsprojEdit.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace FsAutoComplete
open System.IO
module FsProjEditor =
/// <summary>
/// Check if the provided file is already included in the project via a <Compile Include="..." /> tag
/// </summary>
/// <param name="doc">Document to search in</param>
/// <param name="searchedFile">File to search for</param>
/// <returns>
/// Returns true if the file is already included in the project via a <Compile Include="..." /> tag.
///
/// Otherwise returns false.
/// </returns>
let private fileAlreadyIncludedViaCompileTag (doc: System.Xml.XmlDocument) (searchedFile: string) =
let inline sanitizeFile (file: string) = file.Replace("\\", "/")
let searchedFile = sanitizeFile searchedFile
// Take all the <Compile Include="..." /> nodes
doc.SelectNodes("//Compile[@Include]")
|> Seq.cast<System.Xml.XmlNode>
// Find the node that match the file we want to remove
// Note: We sanitize 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 sanitizedInclude = node.Attributes.["Include"].InnerText |> sanitizeFile
sanitizedInclude = searchedFile)
|> Option.isSome
/// <summary>
/// Create a new <Compile Include="..." /> node
/// </summary>
/// <param name="doc">XmlDocument instance to for which we want to create the node</param>
/// <param name="includePath">Name of the path to include</param>
/// <returns>
/// Returns a new <Compile Include="..." /> node
/// </returns>
let private createNewCompileNode (doc: System.Xml.XmlDocument) (includePath: string) =
let node = doc.CreateElement("Compile")
node.SetAttribute("Include", includePath)
node
let moveFileUp (fsprojPath: string) (file: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.PreserveWhitespace <- true // to keep custom formatting if present
xDoc.Load fsprojPath
let xpath = sprintf "//Compile[@Include='%s']/.." file
let itemGroup = xDoc.SelectSingleNode(xpath)
let childXPath = sprintf "//Compile[@Include='%s']" file
let node = itemGroup.SelectSingleNode(childXPath)
let upNode = node.PreviousSibling
if isNull upNode then
()
else
itemGroup.RemoveChild node |> ignore
itemGroup.InsertBefore(node, upNode) |> ignore
xDoc.Save fsprojPath
let moveFileDown (fsprojPath: string) (file: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.PreserveWhitespace <- true // to keep custom formatting if present
xDoc.Load fsprojPath
let xpath = sprintf "//Compile[@Include='%s']/.." file
let itemGroup = xDoc.SelectSingleNode(xpath)
let childXPath = sprintf "//Compile[@Include='%s']" file
let node = itemGroup.SelectSingleNode(childXPath)
let downNode = node.NextSibling
if isNull downNode then
()
else
itemGroup.RemoveChild node |> ignore
itemGroup.InsertAfter(node, downNode) |> ignore
xDoc.Save fsprojPath
let addFileAbove (fsprojPath: string) (aboveFile: string) (newFileName: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.Load fsprojPath
if fileAlreadyIncludedViaCompileTag xDoc newFileName then
Error "File already included in the project"
else
let xpath = sprintf "//Compile[@Include='%s']/.." aboveFile
let itemGroup = xDoc.SelectSingleNode(xpath)
let childXPath = sprintf "//Compile[@Include='%s']" aboveFile
let aboveNode = itemGroup.SelectSingleNode(childXPath)
let node = createNewCompileNode xDoc newFileName
itemGroup.InsertBefore(node, aboveNode) |> ignore
xDoc.Save fsprojPath
Ok()
let addFileBelow (fsprojPath: string) (belowFile: string) (newFileName: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.PreserveWhitespace <- true // to keep custom formatting if present
xDoc.Load fsprojPath
if fileAlreadyIncludedViaCompileTag xDoc newFileName then
Error "File already included in the project"
else
let xpath = sprintf "//Compile[@Include='%s']/.." belowFile
let itemGroup = xDoc.SelectSingleNode(xpath)
let childXPath = sprintf "//Compile[@Include='%s']" belowFile
let aboveNode = itemGroup.SelectSingleNode(childXPath)
let node = createNewCompileNode xDoc newFileName
itemGroup.InsertAfter(node, aboveNode) |> ignore
xDoc.Save fsprojPath
Ok()
let renameFile (fsprojPath: string) (oldFileName: string) (newFileName: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.PreserveWhitespace <- true // to keep custom formatting if present
xDoc.Load fsprojPath
let xpath = sprintf "//Compile[@Include='%s']" oldFileName
let node = xDoc.SelectSingleNode(xpath)
node.Attributes["Include"].InnerText <- newFileName
xDoc.Save fsprojPath
let addFile (fsprojPath: string) (newFileName: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.PreserveWhitespace <- true // to keep custom formatting if present
xDoc.Load fsprojPath
if fileAlreadyIncludedViaCompileTag xDoc newFileName then
Error "File already included in the project"
else
let newNode = createNewCompileNode xDoc newFileName
let compileItemGroups =
xDoc.SelectNodes("//Compile/.. | //None/.. | //EmbeddedResource/.. | //Content/..")
let hasExistingCompileElement = compileItemGroups.Count > 0
if hasExistingCompileElement then
let firstCompileItemGroup =
compileItemGroups |> Seq.cast<System.Xml.XmlNode> |> Seq.head
let x = firstCompileItemGroup.FirstChild
firstCompileItemGroup.InsertBefore(newNode, x) |> ignore
else
let itemGroup = xDoc.CreateElement("ItemGroup")
itemGroup.AppendChild(newNode) |> ignore
let projectNode = xDoc.SelectSingleNode("//Project")
projectNode.AppendChild(itemGroup) |> ignore
xDoc.Save fsprojPath
Ok()
let addExistingFile (fsprojPath: string) (existingFile: string) =
let relativePath =
Path.GetRelativePath(Path.GetDirectoryName fsprojPath, existingFile)
addFile fsprojPath relativePath
let removeFile (fsprojPath: string) (fileToRemove: string) =
let xDoc = System.Xml.XmlDocument()
xDoc.PreserveWhitespace <- true // to keep custom formatting if present
xDoc.Load fsprojPath
let sanitizedFileToRemove = 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 sanitize 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 sanitizedInclude = node.Attributes.["Include"].InnerText.Replace("\\", "/")
sanitizedInclude = sanitizedFileToRemove)
match nodeToRemoveOpt with
| Some nodeToRemove ->
nodeToRemove.ParentNode.RemoveChild(nodeToRemove) |> ignore
xDoc.Save fsprojPath
| None ->
// Node not found, do nothing
()