aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Common.cs
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2017-10-14 16:12:07 -0700
committerRob Mensching <rob@firegiant.com>2017-10-14 16:12:07 -0700
commitdbde9e7104b907bbbaea17e21247d8cafc8b3a4c (patch)
tree0f5fbbb6fe12c6b2e5e622a0e18ce4c5b4eb2b96 /src/WixToolset.Core/Common.cs
parentfbf986eb97f68396797a89fc7d40dec07b775440 (diff)
downloadwix-dbde9e7104b907bbbaea17e21247d8cafc8b3a4c.tar.gz
wix-dbde9e7104b907bbbaea17e21247d8cafc8b3a4c.tar.bz2
wix-dbde9e7104b907bbbaea17e21247d8cafc8b3a4c.zip
Massive refactoring to introduce the concept of IBackend
Diffstat (limited to 'src/WixToolset.Core/Common.cs')
-rw-r--r--src/WixToolset.Core/Common.cs162
1 files changed, 150 insertions, 12 deletions
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
17 /// <summary> 17 /// <summary>
18 /// Common Wix utility methods and types. 18 /// Common Wix utility methods and types.
19 /// </summary> 19 /// </summary>
20 internal static class Common 20 public static class Common
21 { 21 {
22 //------------------------------------------------------------------------------------------------- 22 //-------------------------------------------------------------------------------------------------
23 // Layout of an Access Mask (from http://technet.microsoft.com/en-us/library/cc783530(WS.10).aspx) 23 // Layout of an Access Mask (from http://technet.microsoft.com/en-us/library/cc783530(WS.10).aspx)
@@ -89,9 +89,7 @@ namespace WixToolset
89 // FILE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF) 89 // FILE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF)
90 internal static readonly string[] FilePermissions = { "Read", "Write", "Append", "ReadExtendedAttributes", "WriteExtendedAttributes", "Execute", "FileAllRights", "ReadAttributes", "WriteAttributes" }; 90 internal static readonly string[] FilePermissions = { "Read", "Write", "Append", "ReadExtendedAttributes", "WriteExtendedAttributes", "Execute", "FileAllRights", "ReadAttributes", "WriteAttributes" };
91 91
92 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" }; 92 public static readonly Regex WixVariableRegex = new Regex(@"(\!|\$)\((?<namespace>loc|wix|bind|bindpath)\.(?<fullname>(?<name>[_A-Za-z][0-9A-Za-z_]+)(\.(?<scope>[_A-Za-z][0-9A-Za-z_\.]*))?)(\=(?<value>.+?))?\)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
93
94 internal static readonly Regex WixVariableRegex = new Regex(@"(\!|\$)\((?<namespace>loc|wix|bind|bindpath)\.(?<fullname>(?<name>[_A-Za-z][0-9A-Za-z_]+)(\.(?<scope>[_A-Za-z][0-9A-Za-z_\.]*))?)(\=(?<value>.+?))?\)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
95 93
96 internal const char CustomRowFieldSeparator = '\x85'; 94 internal const char CustomRowFieldSeparator = '\x85';
97 95
@@ -170,15 +168,14 @@ namespace WixToolset
170 /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> 168 /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
171 /// <exception cref="NotSupportedException">The value doesn't not represent a valid code page name or integer value.</exception> 169 /// <exception cref="NotSupportedException">The value doesn't not represent a valid code page name or integer value.</exception>
172 /// <exception cref="WixException">The code page is invalid for summary information.</exception> 170 /// <exception cref="WixException">The code page is invalid for summary information.</exception>
173 internal static int GetValidCodePage(string value, bool allowNoChange = false, bool onlyAnsi = false, SourceLineNumber sourceLineNumbers = null) 171 public static int GetValidCodePage(string value, bool allowNoChange = false, bool onlyAnsi = false, SourceLineNumber sourceLineNumbers = null)
174 { 172 {
175 int codePage;
176 Encoding encoding;
177
178 try 173 try
179 { 174 {
175 Encoding encoding;
176
180 // check if a integer as a string was passed 177 // check if a integer as a string was passed
181 if (Int32.TryParse(value, out codePage)) 178 if (Int32.TryParse(value, out int codePage))
182 { 179 {
183 if (0 == codePage) 180 if (0 == codePage)
184 { 181 {
@@ -366,9 +363,9 @@ namespace WixToolset
366 /// Generate a new Windows Installer-friendly guid. 363 /// Generate a new Windows Installer-friendly guid.
367 /// </summary> 364 /// </summary>
368 /// <returns>A new guid.</returns> 365 /// <returns>A new guid.</returns>
369 internal static string GenerateGuid() 366 public static string GenerateGuid()
370 { 367 {
371 return Guid.NewGuid().ToString("B").ToUpper(CultureInfo.InvariantCulture); 368 return Guid.NewGuid().ToString("B").ToUpperInvariant();
372 } 369 }
373 370
374 /// <summary> 371 /// <summary>
@@ -465,7 +462,7 @@ namespace WixToolset
465 } 462 }
466 } 463 }
467 464
468 internal static string GetFileHash(string path) 465 public static string GetFileHash(string path)
469 { 466 {
470 using (SHA1Managed managed = new SHA1Managed()) 467 using (SHA1Managed managed = new SHA1Managed())
471 { 468 {
@@ -478,6 +475,147 @@ namespace WixToolset
478 } 475 }
479 476
480 /// <summary> 477 /// <summary>
478 /// Takes an id, and demodularizes it (if possible).
479 /// </summary>
480 /// <remarks>
481 /// If the output type is a module, returns a demodularized version of an id. Otherwise, returns the id.
482 /// </remarks>
483 /// <param name="outputType">The type of the output to bind.</param>
484 /// <param name="modularizationGuid">The modularization GUID.</param>
485 /// <param name="id">The id to demodularize.</param>
486 /// <returns>The demodularized id.</returns>
487 public static string Demodularize(OutputType outputType, string modularizationGuid, string id)
488 {
489 if (OutputType.Module == outputType && id.EndsWith(String.Concat(".", modularizationGuid), StringComparison.Ordinal))
490 {
491 id = id.Substring(0, id.Length - 37);
492 }
493
494 return id;
495 }
496
497 /// <summary>
498 /// Get the source/target and short/long file names from an MSI Filename column.
499 /// </summary>
500 /// <param name="value">The Filename value.</param>
501 /// <returns>An array of strings of length 4. The contents are: short target, long target, short source, and long source.</returns>
502 /// <remarks>
503 /// If any particular file name part is not parsed, its set to null in the appropriate location of the returned array of strings.
504 /// However, the returned array will always be of length 4.
505 /// </remarks>
506 public static string[] GetNames(string value)
507 {
508 string[] names = new string[4];
509 int targetSeparator = value.IndexOf(":", StringComparison.Ordinal);
510
511 // split source and target
512 string sourceName = null;
513 string targetName = value;
514 if (0 <= targetSeparator)
515 {
516 sourceName = value.Substring(targetSeparator + 1);
517 targetName = value.Substring(0, targetSeparator);
518 }
519
520 // split the source short and long names
521 string sourceLongName = null;
522 if (null != sourceName)
523 {
524 int sourceLongNameSeparator = sourceName.IndexOf("|", StringComparison.Ordinal);
525 if (0 <= sourceLongNameSeparator)
526 {
527 sourceLongName = sourceName.Substring(sourceLongNameSeparator + 1);
528 sourceName = sourceName.Substring(0, sourceLongNameSeparator);
529 }
530 }
531
532 // split the target short and long names
533 int targetLongNameSeparator = targetName.IndexOf("|", StringComparison.Ordinal);
534 string targetLongName = null;
535 if (0 <= targetLongNameSeparator)
536 {
537 targetLongName = targetName.Substring(targetLongNameSeparator + 1);
538 targetName = targetName.Substring(0, targetLongNameSeparator);
539 }
540
541 // remove the long source name when its identical to the long source name
542 if (null != sourceName && sourceName == sourceLongName)
543 {
544 sourceLongName = null;
545 }
546
547 // remove the long target name when its identical to the long target name
548 if (null != targetName && targetName == targetLongName)
549 {
550 targetLongName = null;
551 }
552
553 // remove the source names when they are identical to the target names
554 if (sourceName == targetName && sourceLongName == targetLongName)
555 {
556 sourceName = null;
557 sourceLongName = null;
558 }
559
560 // target name(s)
561 if ("." != targetName)
562 {
563 names[0] = targetName;
564 }
565
566 if (null != targetLongName && "." != targetLongName)
567 {
568 names[1] = targetLongName;
569 }
570
571 // source name(s)
572 if (null != sourceName)
573 {
574 names[2] = sourceName;
575 }
576
577 if (null != sourceLongName && "." != sourceLongName)
578 {
579 names[3] = sourceLongName;
580 }
581
582 return names;
583 }
584
585 /// <summary>
586 /// Get a source/target and short/long file name from an MSI Filename column.
587 /// </summary>
588 /// <param name="value">The Filename value.</param>
589 /// <param name="source">true to get a source name; false to get a target name</param>
590 /// <param name="longName">true to get a long name; false to get a short name</param>
591 /// <returns>The name.</returns>
592 public static string GetName(string value, bool source, bool longName)
593 {
594 string[] names = GetNames(value);
595
596 if (source)
597 {
598 if (longName && null != names[3])
599 {
600 return names[3];
601 }
602 else if (null != names[2])
603 {
604 return names[2];
605 }
606 }
607
608 if (longName && null != names[1])
609 {
610 return names[1];
611 }
612 else
613 {
614 return names[0];
615 }
616 }
617
618 /// <summary>
481 /// Get an attribute value. 619 /// Get an attribute value.
482 /// </summary> 620 /// </summary>
483 /// <param name="sourceLineNumbers">Source line information about the owner element.</param> 621 /// <param name="sourceLineNumbers">Source line information about the owner element.</param>