aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.BuildTasks/ReplaceString.cs
blob: e5041923a08952a61ba1938c9b34c2cc12462267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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.

namespace WixToolset.BuildTasks
{
    using System;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;

    /// <summary>
    /// Replaces occurances of OldValues with NewValues in String.
    /// </summary>
    public class ReplaceString : Task
    {
        /// <summary>
        /// Text to operate on.
        /// </summary>
        [Output]
        [Required]
        public string Text { get; set; }

        /// <summary>
        /// List of old values to replace.
        /// </summary>
        [Required]
        public string OldValue { get; set; }

        /// <summary>
        /// List of new values to replace old values with.  If not specified, occurances of OldValue will be removed.
        /// </summary>
        public string NewValue { get; set; }

        /// <summary>
        /// Does the string replacement.
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            if (String.IsNullOrEmpty(this.Text))
            {
                return true;
            }

            if (String.IsNullOrEmpty(this.OldValue))
            {
                Log.LogError("OldValue must be specified");
                return false;
            }

            this.Text = this.Text.Replace(this.OldValue, this.NewValue);

            return true;
        }
    }
}