-
Notifications
You must be signed in to change notification settings - Fork 533
/
ReplaceFileContents.cs
65 lines (55 loc) · 1.87 KB
/
ReplaceFileContents.cs
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
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Xamarin.Android.BuildTools.PrepTasks
{
public class ReplaceFileContents : Task
{
[Required]
public ITaskItem SourceFile { get; set; }
[Required]
public ITaskItem DestinationFile { get; set; }
public string[] Replacements { get; set; }
public override bool Execute ()
{
Log.LogMessage (MessageImportance.Low, $"Task {nameof (ReplaceFileContents)}");
Log.LogMessage (MessageImportance.Low, $" {nameof (SourceFile)}: {SourceFile.ItemSpec}");
Log.LogMessage (MessageImportance.Low, $" {nameof (DestinationFile)}: {DestinationFile.ItemSpec}");
Log.LogMessage (MessageImportance.Low, $" {nameof (Replacements)}:");
foreach (var replacement in Replacements) {
Log.LogMessage (MessageImportance.Low, $" {replacement}");
}
File.Delete (DestinationFile.ItemSpec);
var r = GetReplacementInfo (Replacements);
using (var i = File.OpenText (SourceFile.ItemSpec))
using (var o = File.CreateText (DestinationFile.ItemSpec)) {
string line;
while ((line = i.ReadLine ()) != null) {
foreach (var e in r) {
line = line.Replace (e.Key, e.Value);
}
o.WriteLine (line);
}
}
return !Log.HasLoggedErrors;
}
static readonly char[] Separator = new [] { '=' };
static Dictionary<string, string> GetReplacementInfo (string[] replacements)
{
var r = new Dictionary<string, string> (replacements?.Length ?? 0);
if (replacements == null)
return r;
foreach (var e in replacements) {
if (string.IsNullOrEmpty (e))
continue;
var kvp = e.Split (Separator, 2, StringSplitOptions.RemoveEmptyEntries);
r.Add (kvp [0], kvp.Length > 1 ? kvp [1] : "");
}
return r;
}
}
}