| 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 Microsoft.Build.Framework; |
| 7 | |
| 8 | internal class MetadataValue |
| 9 | { |
| 10 | public MetadataValue(ITaskItem item, string name, string valuePrefix = null, string defaultValue = "") |
| 11 | { |
| 12 | this.Item = item; |
| 13 | this.Name = name; |
| 14 | |
| 15 | var value = item.GetMetadata(name); |
| 16 | |
| 17 | this.HadValue = !String.IsNullOrWhiteSpace(value); |
| 18 | this.OriginalValue = value; |
| 19 | |
| 20 | if (!this.HadValue) |
| 21 | { |
| 22 | this.Value = defaultValue; |
| 23 | this.ValidValue = true; |
| 24 | } |
| 25 | else if (String.IsNullOrWhiteSpace(valuePrefix)) |
| 26 | { |
| 27 | this.Value = value; |
| 28 | this.ValidValue = true; |
| 29 | } |
| 30 | else if (value.StartsWith(valuePrefix) && value.Length > valuePrefix.Length) |
| 31 | { |
| 32 | this.Value = value.Substring(valuePrefix.Length); |
| 33 | this.ValidValue = true; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public ITaskItem Item { get; } |
| 38 | |
| 39 | public string Name { get; } |
| 40 | |
| 41 | public bool HadValue { get; } |
| 42 | |
| 43 | public bool Modified => !String.Equals(this.OriginalValue, this.Value, StringComparison.Ordinal); |
| 44 | |
| 45 | public string OriginalValue { get; } |
| 46 | |
| 47 | public string Value { get; private set; } |
| 48 | |
| 49 | public bool ValidValue { get; } |
| 50 | |
| 51 | public void SetValue(string value) |
| 52 | { |
| 53 | this.Value = value; |
| 54 | } |
| 55 | |
| 56 | public void Apply() |
| 57 | { |
| 58 | if (String.IsNullOrWhiteSpace(this.Value)) |
| 59 | { |
| 60 | this.Item.RemoveMetadata(this.Name); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | this.Item.SetMetadata(this.Name, this.Value); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |