summaryrefslogtreecommitdiff
path: root/src/tools/heat/FileHarvester.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/heat/FileHarvester.cs')
-rw-r--r--src/tools/heat/FileHarvester.cs156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/tools/heat/FileHarvester.cs b/src/tools/heat/FileHarvester.cs
new file mode 100644
index 00000000..886b942a
--- /dev/null
+++ b/src/tools/heat/FileHarvester.cs
@@ -0,0 +1,156 @@
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.Harvesters.Data;
9 using WixToolset.Harvesters.Extensibility;
10 using Wix = WixToolset.Harvesters.Serialize;
11
12 /// <summary>
13 /// Harvest WiX authoring for a file from the file system.
14 /// </summary>
15 public sealed class FileHarvester : BaseHarvesterExtension
16 {
17 private string rootedDirectoryRef;
18 private bool setUniqueIdentifiers;
19 private bool suppressRootDirectory;
20
21 private static readonly string ComponentPrefix = "cmp";
22 private static readonly string DirectoryPrefix = "dir";
23 private static readonly string FilePrefix = "fil";
24
25 /// <summary>
26 /// Instantiate a new FileHarvester.
27 /// </summary>
28 public FileHarvester()
29 {
30 this.setUniqueIdentifiers = true;
31 this.suppressRootDirectory = false;
32 }
33
34 /// <summary>
35 /// Gets or sets the rooted DirectoryRef Id if the user has supplied it.
36 /// </summary>
37 /// <value>The DirectoryRef Id to use as the root.</value>
38 public string RootedDirectoryRef
39 {
40 get { return this.rootedDirectoryRef; }
41 set { this.rootedDirectoryRef = value; }
42 }
43
44 /// <summary>
45 /// Gets of sets the option to set unique identifiers.
46 /// </summary>
47 /// <value>The option to set unique identifiers.</value>
48 public bool SetUniqueIdentifiers
49 {
50 get { return this.setUniqueIdentifiers; }
51 set { this.setUniqueIdentifiers = value; }
52 }
53
54 /// <summary>
55 /// Gets or sets the option to suppress including the root directory as an element.
56 /// </summary>
57 /// <value>The option to suppress including the root directory as an element.</value>
58 public bool SuppressRootDirectory
59 {
60 get { return this.suppressRootDirectory; }
61 set { this.suppressRootDirectory = value; }
62 }
63
64 /// <summary>
65 /// Harvest a file.
66 /// </summary>
67 /// <param name="argument">The path of the file.</param>
68 /// <returns>A harvested file.</returns>
69 public override Wix.Fragment[] Harvest(string argument)
70 {
71 if (null == argument)
72 {
73 throw new ArgumentNullException("argument");
74 }
75
76 if (null == this.rootedDirectoryRef)
77 {
78 this.rootedDirectoryRef = "TARGETDIR";
79 }
80
81 string fullPath = Path.GetFullPath(argument);
82
83 Wix.DirectoryRef directoryRef = new Wix.DirectoryRef();
84 directoryRef.Id = this.rootedDirectoryRef;
85
86 Wix.File file = this.HarvestFile(fullPath);
87
88 if (!this.suppressRootDirectory)
89 {
90 file.Source = String.Concat("SourceDir\\", Path.GetFileName(Path.GetDirectoryName(fullPath)), "\\", Path.GetFileName(fullPath));
91 }
92
93 Wix.Component component = new Wix.Component();
94 component.AddChild(file);
95
96 Wix.Directory directory = new Wix.Directory();
97
98 if (this.suppressRootDirectory)
99 {
100 directoryRef.AddChild(component);
101 }
102 else
103 {
104 string directoryPath = Path.GetDirectoryName(Path.GetFullPath(argument));
105 directory.Name = Path.GetFileName(directoryPath);
106
107 if (this.setUniqueIdentifiers)
108 {
109 directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, directoryRef.Id, directory.Name);
110 }
111 directory.AddChild(component);
112 directoryRef.AddChild(directory);
113 }
114
115 if (this.setUniqueIdentifiers)
116 {
117 file.Id = this.Core.GenerateIdentifier(FilePrefix, (this.suppressRootDirectory) ? directoryRef.Id : directory.Id, Path.GetFileName(file.Source));
118 component.Id = this.Core.GenerateIdentifier(ComponentPrefix, (this.suppressRootDirectory) ? directoryRef.Id : directory.Id, file.Id);
119 }
120
121 Wix.Fragment fragment = new Wix.Fragment();
122 fragment.AddChild(directoryRef);
123
124 return new Wix.Fragment[] { fragment };
125 }
126
127 /// <summary>
128 /// Harvest a file.
129 /// </summary>
130 /// <param name="path">The path of the file.</param>
131 /// <returns>A harvested file.</returns>
132 public Wix.File HarvestFile(string path)
133 {
134 if (null == path)
135 {
136 throw new ArgumentNullException("path");
137 }
138
139 if (!File.Exists(path))
140 {
141 throw new WixException(HarvesterErrors.FileNotFound(path));
142 }
143
144 Wix.File file = new Wix.File();
145
146 // use absolute paths
147 path = Path.GetFullPath(path);
148
149 file.KeyPath = Wix.YesNoType.yes;
150
151 file.Source = String.Concat("SourceDir\\", Path.GetFileName(path));
152
153 return file;
154 }
155 }
156}