aboutsummaryrefslogtreecommitdiff
path: root/src/tools/heat/DirectoryHarvester.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/heat/DirectoryHarvester.cs')
-rw-r--r--src/tools/heat/DirectoryHarvester.cs313
1 files changed, 0 insertions, 313 deletions
diff --git a/src/tools/heat/DirectoryHarvester.cs b/src/tools/heat/DirectoryHarvester.cs
deleted file mode 100644
index e69857ab..00000000
--- a/src/tools/heat/DirectoryHarvester.cs
+++ /dev/null
@@ -1,313 +0,0 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolset.Harvesters
4{
5 using System;
6 using System.IO;
7 using WixToolset.Data;
8 using WixToolset.Data.WindowsInstaller;
9 using WixToolset.Harvesters.Data;
10 using WixToolset.Harvesters.Extensibility;
11 using Wix = WixToolset.Harvesters.Serialize;
12
13 /// <summary>
14 /// Harvest WiX authoring for a directory from the file system.
15 /// </summary>
16 public sealed class DirectoryHarvester : BaseHarvesterExtension
17 {
18 private readonly FileHarvester fileHarvester;
19
20 private const string ComponentPrefix = "cmp";
21 private const string DirectoryPrefix = "dir";
22 private const string FilePrefix = "fil";
23
24 /// <summary>
25 /// Instantiate a new DirectoryHarvester.
26 /// </summary>
27 public DirectoryHarvester()
28 {
29 this.fileHarvester = new FileHarvester();
30 this.SetUniqueIdentifiers = true;
31 }
32
33 /// <summary>
34 /// Gets or sets what type of elements are to be generated.
35 /// </summary>
36 /// <value>The type of elements being generated.</value>
37 public GenerateType GenerateType { get; set; }
38
39 /// <summary>
40 /// Gets or sets the option to keep empty directories.
41 /// </summary>
42 /// <value>The option to keep empty directories.</value>
43 public bool KeepEmptyDirectories { get; set; }
44
45 /// <summary>
46 /// Gets or sets the rooted DirectoryRef Id if the user has supplied it.
47 /// </summary>
48 /// <value>The DirectoryRef Id to use as the root.</value>
49 public string RootedDirectoryRef { get; set; }
50
51 /// <summary>
52 /// Gets of sets the option to set unique identifiers.
53 /// </summary>
54 /// <value>The option to set unique identifiers.</value>
55 public bool SetUniqueIdentifiers { get; set; }
56
57 /// <summary>
58 /// Gets or sets the option to suppress including the root directory as an element.
59 /// </summary>
60 /// <value>The option to suppress including the root directory as an element.</value>
61 public bool SuppressRootDirectory { get; set; }
62
63 /// <summary>
64 /// Harvest a directory.
65 /// </summary>
66 /// <param name="argument">The path of the directory.</param>
67 /// <returns>The harvested directory.</returns>
68 public override Wix.Fragment[] Harvest(string argument)
69 {
70 if (null == argument)
71 {
72 throw new ArgumentNullException("argument");
73 }
74
75 Wix.IParentElement harvestParent = this.HarvestDirectory(argument, true, this.GenerateType);
76 Wix.ISchemaElement harvestElement;
77
78 if (this.GenerateType == GenerateType.PayloadGroup)
79 {
80 Wix.PayloadGroup payloadGroup = (Wix.PayloadGroup)harvestParent;
81 payloadGroup.Id = this.RootedDirectoryRef;
82 harvestElement = payloadGroup;
83 }
84 else
85 {
86 Wix.Directory directory = (Wix.Directory)harvestParent;
87
88 var directoryRef = DirectoryHelper.CreateDirectoryReference(this.RootedDirectoryRef);
89
90 if (this.SuppressRootDirectory)
91 {
92 foreach (Wix.ISchemaElement element in directory.Children)
93 {
94 directoryRef.AddChild(element);
95 }
96 }
97 else
98 {
99 directoryRef.AddChild(directory);
100 }
101 harvestElement = directoryRef;
102 }
103
104 Wix.Fragment fragment = new Wix.Fragment();
105 fragment.AddChild(harvestElement);
106
107 return new Wix.Fragment[] { fragment };
108 }
109
110 /// <summary>
111 /// Harvest a directory.
112 /// </summary>
113 /// <param name="path">The path of the directory.</param>
114 /// <param name="harvestChildren">The option to harvest child directories and files.</param>
115 /// <returns>The harvested directory.</returns>
116 public Wix.Directory HarvestDirectory(string path, bool harvestChildren)
117 {
118 if (null == path)
119 {
120 throw new ArgumentNullException("path");
121 }
122
123 return (Wix.Directory)this.HarvestDirectory(path, harvestChildren, GenerateType.Components);
124 }
125
126 /// <summary>
127 /// Harvest a directory.
128 /// </summary>
129 /// <param name="path">The path of the directory.</param>
130 /// <param name="harvestChildren">The option to harvest child directories and files.</param>
131 /// <param name="generateType">The type to generate.</param>
132 /// <returns>The harvested directory.</returns>
133 private Wix.IParentElement HarvestDirectory(string path, bool harvestChildren, GenerateType generateType)
134 {
135 if (File.Exists(path))
136 {
137 throw new WixException(ErrorMessages.ExpectedDirectoryGotFile("dir", path));
138 }
139
140 if (null == this.RootedDirectoryRef)
141 {
142 this.RootedDirectoryRef = "TARGETDIR";
143 }
144
145 // use absolute paths
146 path = Path.GetFullPath(path);
147
148 // Remove any trailing separator to ensure Path.GetFileName() will return the directory name.
149 path = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
150
151 Wix.IParentElement harvestParent;
152 if (generateType == GenerateType.PayloadGroup)
153 {
154 harvestParent = new Wix.PayloadGroup();
155 }
156 else
157 {
158 Wix.Directory directory = new Wix.Directory();
159 directory.Name = Path.GetFileName(path);
160 directory.FileSource = path;
161
162 if (this.SetUniqueIdentifiers)
163 {
164 if (this.SuppressRootDirectory)
165 {
166 directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, this.RootedDirectoryRef);
167 }
168 else
169 {
170 directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, this.RootedDirectoryRef, directory.Name);
171 }
172 }
173 harvestParent = directory;
174 }
175
176 if (harvestChildren)
177 {
178 try
179 {
180 int fileCount = this.HarvestDirectory(path, "", harvestParent, generateType);
181
182 if (generateType != GenerateType.PayloadGroup)
183 {
184 // its an error to not harvest anything with the option to keep empty directories off
185 if (0 == fileCount && !this.KeepEmptyDirectories)
186 {
187 throw new WixException(HarvesterErrors.EmptyDirectory(path));
188 }
189 }
190 }
191 catch (DirectoryNotFoundException)
192 {
193 throw new WixException(HarvesterErrors.DirectoryNotFound(path));
194 }
195 }
196
197 return harvestParent;
198 }
199
200 /// <summary>
201 /// Harvest a directory.
202 /// </summary>
203 /// <param name="path">The path of the directory.</param>
204 /// <param name="relativePath">The relative path that will be used when harvesting.</param>
205 /// <param name="harvestParent">The directory for this path.</param>
206 /// <param name="generateType"></param>
207 /// <returns>The number of files harvested.</returns>
208 private int HarvestDirectory(string path, string relativePath, Wix.IParentElement harvestParent, GenerateType generateType)
209 {
210 int fileCount = 0;
211 Wix.DirectoryBase directory = generateType != GenerateType.PayloadGroup ? (Wix.DirectoryBase)harvestParent : null;
212
213 // harvest the child directories
214 foreach (string childDirectoryPath in Directory.GetDirectories(path))
215 {
216 var childDirectoryName = Path.GetFileName(childDirectoryPath);
217 Wix.IParentElement newParent;
218 Wix.Directory childDirectory = null;
219
220 if (generateType == GenerateType.PayloadGroup)
221 {
222 newParent = harvestParent;
223 }
224 else
225 {
226 childDirectory = new Wix.Directory();
227 newParent = childDirectory;
228
229 childDirectory.Name = childDirectoryName;
230 childDirectory.FileSource = childDirectoryPath;
231
232 if (this.SetUniqueIdentifiers)
233 {
234 childDirectory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, directory.Id, childDirectory.Name);
235 }
236 }
237
238 int childFileCount = this.HarvestDirectory(childDirectoryPath, String.Concat(relativePath, childDirectoryName, "\\"), newParent, generateType);
239
240 if (generateType != GenerateType.PayloadGroup)
241 {
242 // keep the directory if it contained any files (or empty directories are being kept)
243 if (0 < childFileCount || this.KeepEmptyDirectories)
244 {
245 directory.AddChild(childDirectory);
246 }
247 }
248
249 fileCount += childFileCount;
250 }
251
252 // harvest the files
253 string[] files = Directory.GetFiles(path);
254 if (0 < files.Length)
255 {
256 foreach (string filePath in Directory.GetFiles(path))
257 {
258 string fileName = Path.GetFileName(filePath);
259 string source = String.Concat("SourceDir\\", relativePath, fileName);
260
261 Wix.ISchemaElement newChild;
262 if (generateType == GenerateType.PayloadGroup)
263 {
264 Wix.Payload payload = new Wix.Payload();
265 newChild = payload;
266
267 payload.SourceFile = source;
268
269 if (!String.IsNullOrEmpty(relativePath))
270 {
271 payload.Name = String.Concat(relativePath, fileName);
272 }
273 }
274 else
275 {
276 Wix.Component component = new Wix.Component();
277 newChild = component;
278
279 Wix.File file = this.fileHarvester.HarvestFile(filePath);
280 file.Source = source;
281
282 if (this.SetUniqueIdentifiers)
283 {
284 file.Id = this.Core.GenerateIdentifier(FilePrefix, directory.Id, fileName);
285 component.Id = this.Core.GenerateIdentifier(ComponentPrefix, directory.Id, file.Id);
286 }
287
288 component.AddChild(file);
289 }
290
291 harvestParent.AddChild(newChild);
292 }
293 }
294 else if (generateType != GenerateType.PayloadGroup && 0 == fileCount && this.KeepEmptyDirectories)
295 {
296 Wix.Component component = new Wix.Component();
297 component.KeyPath = Wix.YesNoType.yes;
298
299 if (this.SetUniqueIdentifiers)
300 {
301 component.Id = this.Core.GenerateIdentifier(ComponentPrefix, directory.Id);
302 }
303
304 Wix.CreateFolder createFolder = new Wix.CreateFolder();
305 component.AddChild(createFolder);
306
307 directory.AddChild(component);
308 }
309
310 return fileCount + files.Length;
311 }
312 }
313}