OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Diagnostics; | |
5 using System.Diagnostics.CodeAnalysis; | |
6 using System.Globalization; | |
7 using System.IO; | |
8 using System.Runtime.InteropServices; | |
9 using EnvDTE; | |
10 using Microsoft.VisualStudio; | |
11 using Microsoft.VisualStudio.Shell.Interop; | |
12 | |
13 namespace Microsoft.VisualStudio.Project.Automation | |
14 { | |
15 /// <summary> | |
16 /// Contains ProjectItem objects | |
17 /// </summary> | |
18 [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrec
tSuffix")] | |
19 [ComVisible(true), CLSCompliant(false)] | |
20 public class OAProjectItems : OANavigableProjectItems | |
21 { | |
22 #region ctor | |
23 public OAProjectItems(OAProject project, HierarchyNode nodeWithI
tems) | |
24 : base(project, nodeWithItems) | |
25 { | |
26 } | |
27 #endregion | |
28 | |
29 #region EnvDTE.ProjectItems | |
30 /// <summary> | |
31 /// Creates a new project item from an existing item template fi
le and adds it to the project. | |
32 /// </summary> | |
33 /// <param name="fileName">The full path and file name of the te
mplate project file.</param> | |
34 /// <param name="name">The file name to use for the new project
item.</param> | |
35 /// <returns>A ProjectItem object. </returns> | |
36 public override EnvDTE.ProjectItem AddFromTemplate(string fileNa
me, string name) | |
37 { | |
38 | |
39 if(this.Project == null || this.Project.Project == null
|| this.Project.Project.Site == null || this.Project.Project.IsClosed) | |
40 { | |
41 throw new InvalidOperationException(); | |
42 } | |
43 | |
44 ProjectNode proj = this.Project.Project; | |
45 EnvDTE.ProjectItem itemAdded = null; | |
46 | |
47 using(AutomationScope scope = new AutomationScope(this.P
roject.Project.Site)) | |
48 { | |
49 // Determine the operation based on the extensio
n of the filename. | |
50 // We should run the wizard only if the extensio
n is vstemplate | |
51 // otherwise it's a clone operation | |
52 VSADDITEMOPERATION op; | |
53 | |
54 if(Utilities.IsTemplateFile(fileName)) | |
55 { | |
56 op = VSADDITEMOPERATION.VSADDITEMOP_RUNW
IZARD; | |
57 } | |
58 else | |
59 { | |
60 op = VSADDITEMOPERATION.VSADDITEMOP_CLON
EFILE; | |
61 } | |
62 | |
63 VSADDRESULT[] result = new VSADDRESULT[1]; | |
64 | |
65 // It is not a very good idea to throw since the
AddItem might return Cancel or Abort. | |
66 // The problem is that up in the call stack the
wizard code does not check whether it has received a ProjectItem or not and will
crash. | |
67 // The other problem is that we cannot get add w
izard dialog back if a cancel or abort was returned because we throw and that co
de will never be executed. Typical catch 22. | |
68 ErrorHandler.ThrowOnFailure(proj.AddItem(this.No
deWithItems.ID, op, name, 0, new string[1] { fileName }, IntPtr.Zero, result)); | |
69 | |
70 string fileDirectory = proj.GetBaseDirectoryForA
ddingFiles(this.NodeWithItems); | |
71 string templateFilePath = System.IO.Path.Combine
(fileDirectory, name); | |
72 itemAdded = this.EvaluateAddResult(result[0], te
mplateFilePath); | |
73 } | |
74 | |
75 return itemAdded; | |
76 } | |
77 | |
78 /// <summary> | |
79 /// Adds a folder to the collection of ProjectItems with the giv
en name. | |
80 /// | |
81 /// The kind must be null, empty string, or the string value of
vsProjectItemKindPhysicalFolder. | |
82 /// Virtual folders are not supported by this implementation. | |
83 /// </summary> | |
84 /// <param name="name">The name of the new folder to add</param> | |
85 /// <param name="kind">A string representing a Guid of the folde
r kind.</param> | |
86 /// <returns>A ProjectItem representing the newly added folder.<
/returns> | |
87 public override ProjectItem AddFolder(string name, string kind) | |
88 { | |
89 if(this.Project == null || this.Project.Project == null
|| this.Project.Project.Site == null || this.Project.Project.IsClosed) | |
90 { | |
91 throw new InvalidOperationException(); | |
92 } | |
93 //Verify name is not null or empty | |
94 Utilities.ValidateFileName(this.Project.Project.Site, na
me); | |
95 | |
96 //Verify that kind is null, empty, or a physical folder | |
97 if(!(string.IsNullOrEmpty(kind) || kind.Equals(EnvDTE.Co
nstants.vsProjectItemKindPhysicalFolder))) | |
98 { | |
99 throw new ArgumentException("Parameter specifica
tion for AddFolder was not meet", "kind"); | |
100 } | |
101 | |
102 for(HierarchyNode child = this.NodeWithItems.FirstChild;
child != null; child = child.NextSibling) | |
103 { | |
104 if(child.Caption.Equals(name, StringComparison.O
rdinalIgnoreCase)) | |
105 { | |
106 throw new ArgumentException(String.Forma
t(CultureInfo.CurrentCulture, "Folder already exists with the name '{0}'", name)
); | |
107 } | |
108 } | |
109 | |
110 ProjectNode proj = this.Project.Project; | |
111 | |
112 HierarchyNode newFolder = null; | |
113 using(AutomationScope scope = new AutomationScope(this.P
roject.Project.Site)) | |
114 { | |
115 | |
116 //In the case that we are adding a folder to a f
older, we need to build up | |
117 //the path to the project node. | |
118 name = Path.Combine(this.NodeWithItems.VirtualNo
deName, name); | |
119 | |
120 newFolder = proj.CreateFolderNodes(name); | |
121 } | |
122 | |
123 return newFolder.GetAutomationObject() as ProjectItem; | |
124 } | |
125 | |
126 /// <summary> | |
127 /// Copies a source file and adds it to the project. | |
128 /// </summary> | |
129 /// <param name="filePath">The path and file name of the project
item to be added.</param> | |
130 /// <returns>A ProjectItem object. </returns> | |
131 public override EnvDTE.ProjectItem AddFromFileCopy(string filePa
th) | |
132 { | |
133 return this.AddItem(filePath, VSADDITEMOPERATION.VSADDIT
EMOP_OPENFILE); | |
134 } | |
135 | |
136 /// <summary> | |
137 /// Adds a project item from a file that is installed in a proje
ct directory structure. | |
138 /// </summary> | |
139 /// <param name="fileName">The file name of the item to add as a
project item. </param> | |
140 /// <returns>A ProjectItem object. </returns> | |
141 public override EnvDTE.ProjectItem AddFromFile(string fileName) | |
142 { | |
143 // TODO: VSADDITEMOP_LINKTOFILE | |
144 return this.AddItem(fileName, VSADDITEMOPERATION.VSADDIT
EMOP_OPENFILE); | |
145 } | |
146 | |
147 #endregion | |
148 | |
149 #region helper methods | |
150 /// <summary> | |
151 /// Adds an item to the project. | |
152 /// </summary> | |
153 /// <param name="path">The full path of the item to add.</param> | |
154 /// <param name="op">The <paramref name="VSADDITEMOPERATION"/> t
o use when adding the item.</param> | |
155 /// <returns>A ProjectItem object. </returns> | |
156 protected virtual EnvDTE.ProjectItem AddItem(string path, VSADDI
TEMOPERATION op) | |
157 { | |
158 if(this.Project == null || this.Project.Project == null
|| this.Project.Project.Site == null || this.Project.Project.IsClosed) | |
159 { | |
160 throw new InvalidOperationException(); | |
161 } | |
162 | |
163 ProjectNode proj = this.Project.Project; | |
164 | |
165 EnvDTE.ProjectItem itemAdded = null; | |
166 using(AutomationScope scope = new AutomationScope(this.P
roject.Project.Site)) | |
167 { | |
168 VSADDRESULT[] result = new VSADDRESULT[1]; | |
169 ErrorHandler.ThrowOnFailure(proj.AddItem(this.No
deWithItems.ID, op, path, 0, new string[1] { path }, IntPtr.Zero, result)); | |
170 | |
171 string fileName = System.IO.Path.GetFileName(pat
h); | |
172 string fileDirectory = proj.GetBaseDirectoryForA
ddingFiles(this.NodeWithItems); | |
173 string filePathInProject = System.IO.Path.Combin
e(fileDirectory, fileName); | |
174 | |
175 itemAdded = this.EvaluateAddResult(result[0], fi
lePathInProject); | |
176 } | |
177 | |
178 return itemAdded; | |
179 } | |
180 | |
181 /// <summary> | |
182 /// Evaluates the result of an add operation. | |
183 /// </summary> | |
184 /// <param name="result">The <paramref name="VSADDRESULT"/> retu
rned by the Add methods</param> | |
185 /// <param name="path">The full path of the item added.</param> | |
186 /// <returns>A ProjectItem object.</returns> | |
187 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Perf
ormance", "CA1800:DoNotCastUnnecessarily")] | |
188 protected virtual EnvDTE.ProjectItem EvaluateAddResult(VSADDRESU
LT result, string path) | |
189 { | |
190 if(result == VSADDRESULT.ADDRESULT_Success) | |
191 { | |
192 HierarchyNode nodeAdded = this.NodeWithItems.Fin
dChild(path); | |
193 Debug.Assert(nodeAdded != null, "We should have
been able to find the new element in the hierarchy"); | |
194 if(nodeAdded != null) | |
195 { | |
196 EnvDTE.ProjectItem item = null; | |
197 if(nodeAdded is FileNode) | |
198 { | |
199 item = new OAFileItem(this.Proje
ct, nodeAdded as FileNode); | |
200 } | |
201 else if(nodeAdded is NestedProjectNode) | |
202 { | |
203 item = new OANestedProjectItem(t
his.Project, nodeAdded as NestedProjectNode); | |
204 } | |
205 else | |
206 { | |
207 item = new OAProjectItem<Hierarc
hyNode>(this.Project, nodeAdded); | |
208 } | |
209 | |
210 this.Items.Add(item); | |
211 return item; | |
212 } | |
213 } | |
214 return null; | |
215 } | |
216 #endregion | |
217 } | |
218 } | |
OLD | NEW |