From dbde9e7104b907bbbaea17e21247d8cafc8b3a4c Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 14 Oct 2017 16:12:07 -0700 Subject: Massive refactoring to introduce the concept of IBackend --- src/WixToolset.Core/Common.cs | 162 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 150 insertions(+), 12 deletions(-) (limited to 'src/WixToolset.Core/Common.cs') diff --git a/src/WixToolset.Core/Common.cs b/src/WixToolset.Core/Common.cs index a2881984..28e7ee7b 100644 --- a/src/WixToolset.Core/Common.cs +++ b/src/WixToolset.Core/Common.cs @@ -17,7 +17,7 @@ namespace WixToolset /// /// Common Wix utility methods and types. /// - internal static class Common + public static class Common { //------------------------------------------------------------------------------------------------- // Layout of an Access Mask (from http://technet.microsoft.com/en-us/library/cc783530(WS.10).aspx) @@ -89,9 +89,7 @@ namespace WixToolset // FILE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF) internal static readonly string[] FilePermissions = { "Read", "Write", "Append", "ReadExtendedAttributes", "WriteExtendedAttributes", "Execute", "FileAllRights", "ReadAttributes", "WriteAttributes" }; - internal static readonly string[] ReservedFileNames = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; - - internal static readonly Regex WixVariableRegex = new Regex(@"(\!|\$)\((?loc|wix|bind|bindpath)\.(?(?[_A-Za-z][0-9A-Za-z_]+)(\.(?[_A-Za-z][0-9A-Za-z_\.]*))?)(\=(?.+?))?\)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture); + public static readonly Regex WixVariableRegex = new Regex(@"(\!|\$)\((?loc|wix|bind|bindpath)\.(?(?[_A-Za-z][0-9A-Za-z_]+)(\.(?[_A-Za-z][0-9A-Za-z_\.]*))?)(\=(?.+?))?\)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture); internal const char CustomRowFieldSeparator = '\x85'; @@ -170,15 +168,14 @@ namespace WixToolset /// is null. /// The value doesn't not represent a valid code page name or integer value. /// The code page is invalid for summary information. - internal static int GetValidCodePage(string value, bool allowNoChange = false, bool onlyAnsi = false, SourceLineNumber sourceLineNumbers = null) + public static int GetValidCodePage(string value, bool allowNoChange = false, bool onlyAnsi = false, SourceLineNumber sourceLineNumbers = null) { - int codePage; - Encoding encoding; - try { + Encoding encoding; + // check if a integer as a string was passed - if (Int32.TryParse(value, out codePage)) + if (Int32.TryParse(value, out int codePage)) { if (0 == codePage) { @@ -366,9 +363,9 @@ namespace WixToolset /// Generate a new Windows Installer-friendly guid. /// /// A new guid. - internal static string GenerateGuid() + public static string GenerateGuid() { - return Guid.NewGuid().ToString("B").ToUpper(CultureInfo.InvariantCulture); + return Guid.NewGuid().ToString("B").ToUpperInvariant(); } /// @@ -465,7 +462,7 @@ namespace WixToolset } } - internal static string GetFileHash(string path) + public static string GetFileHash(string path) { using (SHA1Managed managed = new SHA1Managed()) { @@ -477,6 +474,147 @@ namespace WixToolset } } + /// + /// Takes an id, and demodularizes it (if possible). + /// + /// + /// If the output type is a module, returns a demodularized version of an id. Otherwise, returns the id. + /// + /// The type of the output to bind. + /// The modularization GUID. + /// The id to demodularize. + /// The demodularized id. + public static string Demodularize(OutputType outputType, string modularizationGuid, string id) + { + if (OutputType.Module == outputType && id.EndsWith(String.Concat(".", modularizationGuid), StringComparison.Ordinal)) + { + id = id.Substring(0, id.Length - 37); + } + + return id; + } + + /// + /// Get the source/target and short/long file names from an MSI Filename column. + /// + /// The Filename value. + /// An array of strings of length 4. The contents are: short target, long target, short source, and long source. + /// + /// If any particular file name part is not parsed, its set to null in the appropriate location of the returned array of strings. + /// However, the returned array will always be of length 4. + /// + public static string[] GetNames(string value) + { + string[] names = new string[4]; + int targetSeparator = value.IndexOf(":", StringComparison.Ordinal); + + // split source and target + string sourceName = null; + string targetName = value; + if (0 <= targetSeparator) + { + sourceName = value.Substring(targetSeparator + 1); + targetName = value.Substring(0, targetSeparator); + } + + // split the source short and long names + string sourceLongName = null; + if (null != sourceName) + { + int sourceLongNameSeparator = sourceName.IndexOf("|", StringComparison.Ordinal); + if (0 <= sourceLongNameSeparator) + { + sourceLongName = sourceName.Substring(sourceLongNameSeparator + 1); + sourceName = sourceName.Substring(0, sourceLongNameSeparator); + } + } + + // split the target short and long names + int targetLongNameSeparator = targetName.IndexOf("|", StringComparison.Ordinal); + string targetLongName = null; + if (0 <= targetLongNameSeparator) + { + targetLongName = targetName.Substring(targetLongNameSeparator + 1); + targetName = targetName.Substring(0, targetLongNameSeparator); + } + + // remove the long source name when its identical to the long source name + if (null != sourceName && sourceName == sourceLongName) + { + sourceLongName = null; + } + + // remove the long target name when its identical to the long target name + if (null != targetName && targetName == targetLongName) + { + targetLongName = null; + } + + // remove the source names when they are identical to the target names + if (sourceName == targetName && sourceLongName == targetLongName) + { + sourceName = null; + sourceLongName = null; + } + + // target name(s) + if ("." != targetName) + { + names[0] = targetName; + } + + if (null != targetLongName && "." != targetLongName) + { + names[1] = targetLongName; + } + + // source name(s) + if (null != sourceName) + { + names[2] = sourceName; + } + + if (null != sourceLongName && "." != sourceLongName) + { + names[3] = sourceLongName; + } + + return names; + } + + /// + /// Get a source/target and short/long file name from an MSI Filename column. + /// + /// The Filename value. + /// true to get a source name; false to get a target name + /// true to get a long name; false to get a short name + /// The name. + public static string GetName(string value, bool source, bool longName) + { + string[] names = GetNames(value); + + if (source) + { + if (longName && null != names[3]) + { + return names[3]; + } + else if (null != names[2]) + { + return names[2]; + } + } + + if (longName && null != names[1]) + { + return names[1]; + } + else + { + return names[0]; + } + } + /// /// Get an attribute value. /// -- cgit v1.2.3-55-g6feb