| 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 | |
| 3 | namespace 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 | var directoryRef = DirectoryHelper.CreateDirectoryReference(this.rootedDirectoryRef); |
| 84 | |
| 85 | Wix.File file = this.HarvestFile(fullPath); |
| 86 | |
| 87 | if (!this.suppressRootDirectory) |
| 88 | { |
| 89 | file.Source = String.Concat("SourceDir\\", Path.GetFileName(Path.GetDirectoryName(fullPath)), "\\", Path.GetFileName(fullPath)); |
| 90 | } |
| 91 | |
| 92 | Wix.Component component = new Wix.Component(); |
| 93 | component.AddChild(file); |
| 94 | |
| 95 | Wix.Directory directory = new Wix.Directory(); |
| 96 | |
| 97 | if (this.suppressRootDirectory) |
| 98 | { |
| 99 | directoryRef.AddChild(component); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | string directoryPath = Path.GetDirectoryName(Path.GetFullPath(argument)); |
| 104 | directory.Name = Path.GetFileName(directoryPath); |
| 105 | |
| 106 | if (this.setUniqueIdentifiers) |
| 107 | { |
| 108 | directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, directoryRef.Id, directory.Name); |
| 109 | } |
| 110 | directory.AddChild(component); |
| 111 | directoryRef.AddChild(directory); |
| 112 | } |
| 113 | |
| 114 | if (this.setUniqueIdentifiers) |
| 115 | { |
| 116 | file.Id = this.Core.GenerateIdentifier(FilePrefix, (this.suppressRootDirectory) ? directoryRef.Id : directory.Id, Path.GetFileName(file.Source)); |
| 117 | component.Id = this.Core.GenerateIdentifier(ComponentPrefix, (this.suppressRootDirectory) ? directoryRef.Id : directory.Id, file.Id); |
| 118 | } |
| 119 | |
| 120 | Wix.Fragment fragment = new Wix.Fragment(); |
| 121 | fragment.AddChild(directoryRef); |
| 122 | |
| 123 | return new Wix.Fragment[] { fragment }; |
| 124 | } |
| 125 | |
| 126 | /// <summary> |
| 127 | /// Harvest a file. |
| 128 | /// </summary> |
| 129 | /// <param name="path">The path of the file.</param> |
| 130 | /// <returns>A harvested file.</returns> |
| 131 | public Wix.File HarvestFile(string path) |
| 132 | { |
| 133 | if (null == path) |
| 134 | { |
| 135 | throw new ArgumentNullException("path"); |
| 136 | } |
| 137 | |
| 138 | if (!File.Exists(path)) |
| 139 | { |
| 140 | throw new WixException(HarvesterErrors.FileNotFound(path)); |
| 141 | } |
| 142 | |
| 143 | Wix.File file = new Wix.File(); |
| 144 | |
| 145 | // use absolute paths |
| 146 | path = Path.GetFullPath(path); |
| 147 | |
| 148 | file.KeyPath = Wix.YesNoType.yes; |
| 149 | |
| 150 | file.Source = String.Concat("SourceDir\\", Path.GetFileName(path)); |
| 151 | |
| 152 | return file; |
| 153 | } |
| 154 | } |
| 155 | } |