/// <summary> /// Harvest a reg file. /// </summary> /// <param name="path">The path of the file.</param> /// <returns>A harvested registy file.</returns> public Wix.Fragment HarvestRegFile(string path) { if (null == path) { throw new ArgumentNullException("path"); } if (!File.Exists(path)) { throw new WixException(UtilErrors.FileNotFound(path)); } Wix.Directory directory = new Wix.Directory(); directory.Id = "TARGETDIR"; // Use absolute paths path = Path.GetFullPath(path); FileInfo file = new FileInfo(path); using (StreamReader sr = file.OpenText()) { string line; this.currentLineNumber = 0; while (null != (line = this.GetNextLine(sr))) { if (line.StartsWith(@"Windows Registry Editor Version 5.00")) { this.unicodeRegistry = true; } else if (line.StartsWith(@"REGEDIT4")) { this.unicodeRegistry = false; } else if (line.StartsWith(@"[HKEY_CLASSES_ROOT\")) { this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKCR, line.Substring(19, line.Length - 20)); } else if (line.StartsWith(@"[HKEY_CURRENT_USER\")) { this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKCU, line.Substring(19, line.Length - 20)); } else if (line.StartsWith(@"[HKEY_LOCAL_MACHINE\")) { this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKLM, line.Substring(20, line.Length - 21)); } else if (line.StartsWith(@"[HKEY_USERS\")) { this.ConvertKey(sr, ref directory, Wix.RegistryRootType.HKU, line.Substring(12, line.Length - 13)); } } } Console.WriteLine("Processing complete"); Wix.Fragment fragment = new Wix.Fragment(); fragment.AddChild(directory); return(fragment); }
/// <summary> /// Harvest a directory. /// </summary> /// <param name="path">The path of the directory.</param> /// <param name="relativePath">The relative path that will be used when harvesting.</param> /// <param name="harvestChildren">The option to harvest child directories and files.</param> /// <returns>The harvested directory.</returns> public Wix.Directory HarvestDirectory(string path, string relativePath, bool harvestChildren) { if (null == path) { throw new ArgumentNullException("path"); } if (File.Exists(path)) { throw new WixException(WixErrors.ExpectedDirectoryGotFile("dir", path)); } if (null == this.rootedDirectoryRef) { this.rootedDirectoryRef = "TARGETDIR"; } // use absolute paths path = Path.GetFullPath(path); // Remove any trailing separator to ensure Path.GetFileName() will return the directory name. path = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); Wix.Directory directory = new Wix.Directory(); directory.Name = Path.GetFileName(path); directory.FileSource = path; if (this.setUniqueIdentifiers) { if (this.suppressRootDirectory) { directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, this.rootedDirectoryRef); } else { directory.Id = this.Core.GenerateIdentifier(DirectoryPrefix, this.rootedDirectoryRef, directory.Name); } } if (harvestChildren) { try { int fileCount = this.HarvestDirectory(path, relativePath, directory); // its an error to not harvest anything with the option to keep empty directories off if (0 == fileCount && !this.keepEmptyDirectories) { throw new WixException(UtilErrors.EmptyDirectory(path)); } } catch (DirectoryNotFoundException) { throw new WixException(UtilErrors.DirectoryNotFound(path)); } } return(directory); }
/// <summary> /// Mutate a WiX document as a string. /// </summary> /// <param name="wix">The Wix document element as a string.</param> /// <returns>The mutated Wix document as a string.</returns> public override string Mutate(string wixString) { try { XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(this.transform, XsltSettings.TrustedXslt, new XmlUrlResolver()); using (XmlTextReader xmlReader = new XmlTextReader(new StringReader(wixString))) { using (StringWriter stringWriter = new StringWriter()) { XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Indent = true; xmlSettings.IndentChars = " "; xmlSettings.OmitXmlDeclaration = true; using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings)) { xslt.Transform(xmlReader, xmlWriter); } wixString = stringWriter.ToString(); } } } catch (Exception ex) { this.Core.OnMessage(UtilErrors.ErrorTransformingHarvestedWiX(this.transform, ex.Message)); return(null); } return(wixString); }
private string GetArgumentParameter(string[] args, int index, bool allowSpaces) { string truncatedCommandSwitch = args[index]; string commandSwitchValue = args[index + 1]; //increment the index to the switch value index++; if (CommandLine.IsValidArg(args, index) && !String.IsNullOrEmpty(commandSwitchValue.Trim())) { if (!allowSpaces && commandSwitchValue.Contains(" ")) { this.Core.OnMessage(UtilErrors.SpacesNotAllowedInArgumentValue(truncatedCommandSwitch, commandSwitchValue)); } else { return(commandSwitchValue); } } else { this.Core.OnMessage(UtilErrors.ArgumentRequiresValue(truncatedCommandSwitch)); } return(null); }
/// <summary> /// Harvest a directory. /// </summary> /// <param name="path">The path of the directory.</param> /// <param name="harvestChildren">The option to harvest child directories and files.</param> /// <returns>The harvested directory.</returns> public Wix.Directory HarvestDirectory(string path, bool harvestChildren) { if (null == path) { throw new ArgumentNullException("path"); } Wix.Directory directory = new Wix.Directory(); // use absolute paths path = Path.GetFullPath(path); directory.FileSource = path; directory.Name = Path.GetFileName(path); if (harvestChildren) { try { int fileCount = this.HarvestDirectory(path, directory); // its an error to not harvest anything with the option to keep empty directories off if (0 == fileCount && !this.keepEmptyDirectories) { throw new WixException(UtilErrors.EmptyDirectory(path)); } } catch (DirectoryNotFoundException) { throw new WixException(UtilErrors.DirectoryNotFound(path)); } } return(directory); }
/// <summary> /// Harvest a payload. /// </summary> /// <param name="path">The path of the payload.</param> /// <returns>A harvested payload.</returns> public Wix.RemotePayload HarvestRemotePayload(string path) { if (null == path) { throw new ArgumentNullException("path"); } if (!File.Exists(path)) { throw new WixException(UtilErrors.FileNotFound(path)); } Wix.RemotePayload remotePayload = new Wix.RemotePayload(); FileInfo fileInfo = new FileInfo(path); remotePayload.Size = (int)fileInfo.Length; remotePayload.Hash = this.GetFileHash(path); FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(path); if (null != versionInfo) { // Use the fixed version info block for the file since the resource text may not be a dotted quad. Version version = new Version(versionInfo.ProductMajorPart, versionInfo.ProductMinorPart, versionInfo.ProductBuildPart, versionInfo.ProductPrivatePart); remotePayload.Version = version.ToString(); remotePayload.Description = versionInfo.FileDescription; remotePayload.ProductName = versionInfo.ProductName; } return(remotePayload); }
/// <summary> /// Harvest a registry key. /// </summary> /// <param name="path">The path of the registry key to harvest.</param> /// <returns>The registry keys and values under the key.</returns> public Wix.RegistryValue[] HarvestRegistryKey(string path) { RegistryKey registryKey = null; ArrayList registryValues = new ArrayList(); string[] parts = GetPathParts(path); try { switch (parts[0]) { case "HKEY_CLASSES_ROOT": registryKey = Registry.ClassesRoot; break; case "HKEY_CURRENT_USER": registryKey = Registry.CurrentUser; break; case "HKEY_LOCAL_MACHINE": registryKey = Registry.LocalMachine; break; case "HKEY_USERS": registryKey = Registry.Users; break; default: // TODO: put a better exception here throw new Exception(); } if (1 < parts.Length) { registryKey = registryKey.OpenSubKey(parts[1]); if (null == registryKey) { throw new WixException(UtilErrors.UnableToOpenRegistryKey(parts[1])); } } this.HarvestRegistryKey(registryKey, registryValues); } finally { if (null != registryKey) { registryKey.Close(); } } return((Wix.RegistryValue[])registryValues.ToArray(typeof(Wix.RegistryValue))); }
/// <summary> /// Convert the PerformanceCounterType to the Wix type. /// </summary> /// <param name="performanceCounterType">Type to convert.</param> /// <returns>The wix performance counter type.</returns> private static Util.PerformanceCounterTypesType CounterTypeToWix(PerformanceCounterType performanceCounterType) { try { return(PerformanceCounterTypes.Single(e => string.Equals(e.ToString(), performanceCounterType.ToString(), StringComparison.OrdinalIgnoreCase))); } catch (Exception) { throw new WixException(UtilErrors.UnsupportedPerformanceCounterType(performanceCounterType.ToString())); } }
/// <summary> /// Harvest a performance category. /// </summary> /// <param name="category">The name of the performance category.</param> /// <returns>A harvested file.</returns> public Util.PerformanceCategory HarvestPerformanceCategory(string category) { if (null == category) { throw new ArgumentNullException("category"); } if (PerformanceCounterCategory.Exists(category)) { Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); // Get the performance counter category and set the appropriate WiX attributes PerformanceCounterCategory pcc = PerformanceCounterCategory.GetCategories().Single(c => string.Equals(c.CategoryName, category)); perfCategory.Id = this.Core.CreateIdentifierFromFilename(pcc.CategoryName); perfCategory.Name = pcc.CategoryName; perfCategory.Help = pcc.CategoryHelp; if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) { perfCategory.MultiInstance = Util.YesNoType.yes; } // If it's multi-instance, check if there are any instances and get counters from there; else we get // the counters straight up. For multi-instance, GetCounters() fails if there are any instances. If there // are no instances, then GetCounters(instance) can't be called since there is no instance. Instances // will exist for each counter even if only one of the counters was "intialized." string[] instances = pcc.GetInstanceNames(); bool hasInstances = instances.Length > 0; PerformanceCounter[] counters = hasInstances ? pcc.GetCounters(instances.First()) : pcc.GetCounters(); foreach (PerformanceCounter counter in counters) { Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); // Get the performance counter and set the appropriate WiX attributes perfCounter.Name = counter.CounterName; perfCounter.Type = CounterTypeToWix(counter.CounterType); perfCounter.Help = counter.CounterHelp; perfCategory.AddChild(perfCounter); } return(perfCategory); } else { throw new WixException(UtilErrors.PerformanceCategoryNotFound(category)); } }
/// <summary> /// Finds circular references in the constraints. /// </summary> /// <param name="constraints">Constraints to check.</param> /// <remarks>This is not particularly performant, but it works.</remarks> private void FindCircularReference(Constraints constraints) { foreach (string id in constraints.Keys) { List <string> seenIds = new List <string>(); string chain = null; if (FindCircularReference(constraints, id, id, seenIds, out chain)) { // We will show a separate message for every ID that's in // the loop. We could bail after the first one, but then // we wouldn't catch disjoint loops in a single run. this.Core.OnMessage(UtilErrors.CircularSearchReference(chain)); } } }
private Util.PerformanceCategory GetPerformanceCategory(string categoryName) { Console.WriteLine($"Getting counters for CategoryName: {categoryName}"); try { var category = PerformanceCounterCategory.GetCategories() .Single(c => string.Equals(categoryName, c.CategoryName, StringComparison.OrdinalIgnoreCase)); var isMultiInstance = category.CategoryType == PerformanceCounterCategoryType.MultiInstance; Trace.WriteLine($"Found category={categoryName}, MultiInstance={isMultiInstance}"); // If it's multi-instance, check if there are any instances and get counters from there; else we get // the counters straight up. For multi-instance, GetCounters() fails if there are any instances. If there // are no instances, then GetCounters(instance) can't be called since there is no instance. Instances // will exist for each counter even if only one of the counters was "intialized." var hasInstances = category.GetInstanceNames().Length > 0; var counters = hasInstances ? category.GetCounters(category.GetInstanceNames().First()) : category.GetCounters(); Trace.WriteLine($"Found {counters.Length} counters"); var result = new Util.PerformanceCategory { Id = CompilerCore.GetIdentifierFromName(category.CategoryName), Name = category.CategoryName, Help = category.CategoryHelp, MultiInstance = isMultiInstance ? Util.YesNoType.yes : Util.YesNoType.no }; foreach (var counter in counters) { Console.WriteLine($"Counter={counter.CounterName}, Type={counter.CounterType}"); result.AddChild(new Util.PerformanceCounter { Name = counter.CounterName, Type = CounterTypeToWix(counter.CounterType), Help = counter.CounterHelp, }); } return(result); } catch (Exception ex) { Trace.WriteLine(ex.Message); throw new WixException(UtilErrors.PerformanceCategoryNotFound(categoryName)); } }
/// <summary> /// Harvest a performance category. /// </summary> /// <param name="category">The name of the performance category.</param> /// <returns>A harvested file.</returns> public Util.PerformanceCategory HarvestPerformanceCategory(string category) { if (null == category) { throw new ArgumentNullException("category"); } if (PerformanceCounterCategory.Exists(category)) { Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); // Get the performance counter category and set the appropriate WiX attributes PerformanceCounterCategory pcc = new PerformanceCounterCategory(category); perfCategory.Id = CompilerCore.GetIdentifierFromName(pcc.CategoryName); perfCategory.Name = pcc.CategoryName; perfCategory.Help = pcc.CategoryHelp; if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) { perfCategory.MultiInstance = Util.YesNoType.yes; } foreach (InstanceDataCollection counter in pcc.ReadCategory().Values) { Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); // Get the performance counter and set the appropriate WiX attributes PerformanceCounter pc = new PerformanceCounter(pcc.CategoryName, counter.CounterName); perfCounter.Name = pc.CounterName; perfCounter.Type = CounterTypeToWix(pc.CounterType); perfCounter.Help = pc.CounterHelp; perfCategory.AddChild(perfCounter); } return(perfCategory); } else { throw new WixException(UtilErrors.PerformanceCategoryNotFound(category)); } }
/// <summary> /// Harvest a payload. /// </summary> /// <param name="path">The path of the payload.</param> /// <returns>A harvested payload.</returns> public Wix.RemotePayload HarvestRemotePayload(string path) { if (null == path) { throw new ArgumentNullException("path"); } if (!File.Exists(path)) { throw new WixException(UtilErrors.FileNotFound(path)); } PayloadInfo payloadInfo = new PayloadInfo() { SourceFile = Path.GetFullPath(path), SuppressSignatureValidation = false // assume signed, if its unsigned it won't get the certificate properties }; PayloadInfoRow.ResolvePayloadInfo(payloadInfo); return(payloadInfo); }
/// <summary> /// Harvest a file. /// </summary> /// <param name="path">The path of the file.</param> /// <returns>A harvested file.</returns> public Wix.File HarvestFile(string path) { if (null == path) { throw new ArgumentNullException("path"); } if (!File.Exists(path)) { throw new WixException(UtilErrors.FileNotFound(path)); } Wix.File file = new Wix.File(); // use absolute paths path = Path.GetFullPath(path); file.KeyPath = Wix.YesNoType.yes; file.Source = String.Concat("SourceDir\\", Path.GetFileName(path)); return(file); }
/// <summary> /// Get the WiX performance counter type. /// </summary> /// <param name="pct">The performance counter value to get.</param> /// <returns>The WiX performance counter type.</returns> private Util.PerformanceCounterTypesType CounterTypeToWix(PerformanceCounterType pct) { Util.PerformanceCounterTypesType type; switch (pct) { case PerformanceCounterType.AverageBase: type = Util.PerformanceCounterTypesType.averageBase; break; case PerformanceCounterType.AverageCount64: type = Util.PerformanceCounterTypesType.averageCount64; break; case PerformanceCounterType.AverageTimer32: type = Util.PerformanceCounterTypesType.averageTimer32; break; case PerformanceCounterType.CounterDelta32: type = Util.PerformanceCounterTypesType.counterDelta32; break; case PerformanceCounterType.CounterTimerInverse: type = Util.PerformanceCounterTypesType.counterTimerInverse; break; case PerformanceCounterType.SampleFraction: type = Util.PerformanceCounterTypesType.sampleFraction; break; case PerformanceCounterType.Timer100Ns: type = Util.PerformanceCounterTypesType.timer100Ns; break; case PerformanceCounterType.CounterTimer: type = Util.PerformanceCounterTypesType.counterTimer; break; case PerformanceCounterType.RawFraction: type = Util.PerformanceCounterTypesType.rawFraction; break; case PerformanceCounterType.Timer100NsInverse: type = Util.PerformanceCounterTypesType.timer100NsInverse; break; case PerformanceCounterType.CounterMultiTimer: type = Util.PerformanceCounterTypesType.counterMultiTimer; break; case PerformanceCounterType.CounterMultiTimer100Ns: type = Util.PerformanceCounterTypesType.counterMultiTimer100Ns; break; case PerformanceCounterType.CounterMultiTimerInverse: type = Util.PerformanceCounterTypesType.counterMultiTimerInverse; break; case PerformanceCounterType.CounterMultiTimer100NsInverse: type = Util.PerformanceCounterTypesType.counterMultiTimer100NsInverse; break; case PerformanceCounterType.ElapsedTime: type = Util.PerformanceCounterTypesType.elapsedTime; break; case PerformanceCounterType.SampleBase: type = Util.PerformanceCounterTypesType.sampleBase; break; case PerformanceCounterType.RawBase: type = Util.PerformanceCounterTypesType.rawBase; break; case PerformanceCounterType.CounterMultiBase: type = Util.PerformanceCounterTypesType.counterMultiBase; break; case PerformanceCounterType.RateOfCountsPerSecond64: type = Util.PerformanceCounterTypesType.rateOfCountsPerSecond64; break; case PerformanceCounterType.RateOfCountsPerSecond32: type = Util.PerformanceCounterTypesType.rateOfCountsPerSecond32; break; case PerformanceCounterType.CountPerTimeInterval64: type = Util.PerformanceCounterTypesType.countPerTimeInterval64; break; case PerformanceCounterType.CountPerTimeInterval32: type = Util.PerformanceCounterTypesType.countPerTimeInterval32; break; case PerformanceCounterType.SampleCounter: type = Util.PerformanceCounterTypesType.sampleCounter; break; case PerformanceCounterType.CounterDelta64: type = Util.PerformanceCounterTypesType.counterDelta64; break; case PerformanceCounterType.NumberOfItems64: type = Util.PerformanceCounterTypesType.numberOfItems64; break; case PerformanceCounterType.NumberOfItems32: type = Util.PerformanceCounterTypesType.numberOfItems32; break; case PerformanceCounterType.NumberOfItemsHEX64: type = Util.PerformanceCounterTypesType.numberOfItemsHEX64; break; case PerformanceCounterType.NumberOfItemsHEX32: type = Util.PerformanceCounterTypesType.numberOfItemsHEX32; break; default: throw new WixException(UtilErrors.UnsupportedPerformanceCounterType(pct.ToString())); } return(type); }