-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextReplacementTask.cs
98 lines (82 loc) · 2.38 KB
/
TextReplacementTask.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using System.IO;
using System.Security.AccessControl;
namespace RhysG.MSBuild.Common
{
public class TextReplacementTask : ITask
{
private IBuildEngine _buildEngine;
private ITaskHost _taskHost;
public IBuildEngine BuildEngine
{
get
{
return _buildEngine;
}
set
{
_buildEngine = value;
}
}
[Required]
public string OutputFile
{
get;
set;
}
[Required]
public string File
{
get;
set;
}
[Required]
public string StringToReplace
{
get;
set;
}
[Required]
public string Value
{
get;
set;
}
public bool Execute()
{
FileInfo file = new FileInfo(File);
FileInfo outputFile = new FileInfo(OutputFile);
if (file.Exists)
{
if (outputFile.Exists && outputFile.LastWriteTimeUtc == file.LastWriteTimeUtc)
{
return true;
}
string input = System.IO.File.ReadAllText(file.FullName);
string replaced = input.Replace(StringToReplace, Value);
System.IO.File.WriteAllText(outputFile.FullName, replaced);
outputFile.LastWriteTimeUtc = file.LastWriteTimeUtc;
FileSecurity security = new FileSecurity();
security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.ReadAndExecute, AccessControlType.Allow));
outputFile.SetAccessControl(security);
BuildEngine.LogMessageEvent(new BuildMessageEventArgs(String.Format("Packed: {0}", file.Name), String.Empty, "PackJavascriptFileTask", MessageImportance.Normal));
}
return true;
}
public ITaskHost HostObject
{
get
{
return _taskHost;
}
set
{
_taskHost = value;
}
}
}
}