main
cs 45 lines 1.82 KB
Raw
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.BuildTasks
4 {
5 using System;
6 using System.Text.RegularExpressions;
7 using Microsoft.Build.Framework;
8
9 /// <summary>
10 /// Common WixTasks utility methods and types.
11 /// </summary>
12 public static class ToolsCommon
13 {
14 /// <summary>Metadata key name to turn off harvesting of project references.</summary>
15 public const string DoNotHarvest = "DoNotHarvest";
16
17 private static readonly Regex AddPrefix = new Regex(@"^[^a-zA-Z_]");
18 private static readonly Regex IllegalIdentifierCharacters = new Regex(@"[^A-Za-z0-9_\.]|\.{2,}"); // non 'words' and assorted valid characters
19
20 /// <summary>
21 /// Return an identifier based on passed value.
22 /// </summary>
23 /// <param name="value">Value to create identifer from.</param>
24 /// <returns>A version of the value that is a legal identifier.</returns>
25 public static string CreateIdentifierFromValue(string value)
26 {
27 var result = IllegalIdentifierCharacters.Replace(value, "_"); // replace illegal characters with "_".
28
29 // MSI identifiers must begin with an alphabetic character or an
30 // underscore. Prefix all other values with an underscore.
31 if (AddPrefix.IsMatch(value))
32 {
33 result = String.Concat("_", result);
34 }
35
36 return result;
37 }
38
39 public static string GetMetadataOrDefault(ITaskItem item, string metadataName, string defaultValue)
40 {
41 var value = item.GetMetadata(metadataName);
42 return String.IsNullOrWhiteSpace(value) ? defaultValue : value;
43 }
44 }
45 }