forked from DotNetOpenAuth/DotNetOpenAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexFileReplace.cs
55 lines (46 loc) · 1.57 KB
/
RegexFileReplace.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
//-----------------------------------------------------------------------
// <copyright file="RegexFileReplace.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.BuildTasks {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Text.RegularExpressions;
public class RegexFileReplace : Task {
/// <summary>
/// Gets or sets the set of files to search.
/// </summary>
[Required]
public ITaskItem[] Files { get; set; }
/// <summary>
/// Gets or sets the files to save the changed files to. This may be the same as the input files to make the change in place.
/// </summary>
public ITaskItem[] OutputFiles { get; set; }
public string Pattern { get; set; }
public string Replacement { get; set; }
/// <summary>
/// Executes this instance.
/// </summary>
public override bool Execute() {
if (this.OutputFiles == null || this.OutputFiles.Length == 0) {
this.OutputFiles = this.Files;
}
foreach (var file in this.Files) {
string[] lines = File.ReadAllLines(file.ItemSpec);
for (int i = 0; i < lines.Length; i++) {
lines[i] = Regex.Replace(lines[i], this.Pattern, this.Replacement);
}
File.WriteAllLines(file.ItemSpec, lines);
}
return !this.Log.HasLoggedErrors;
}
}
}