forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasks.Targets
More file actions
80 lines (78 loc) · 3.37 KB
/
Tasks.Targets
File metadata and controls
80 lines (78 loc) · 3.37 KB
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
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<UsingTask TaskName="Zip" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<ZipFileName ParameterType="System.String" Required="true" />
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<WorkingDirectory ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO.Compression" />
<Code Type="Fragment" Language="cs">
<![CDATA[
string cwd = null;
try {
var di = new DirectoryInfo(WorkingDirectory);
if (!di.Exists) {
Log.LogError(string.Format("{0} doesn't exist", WorkingDirectory));
return false;
}
cwd = Environment.CurrentDirectory;
Environment.CurrentDirectory = di.FullName;
using (Stream zipStream = new FileStream(Path.GetFullPath(ZipFileName), FileMode.Create, FileAccess.Write))
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) {
foreach (ITaskItem fileItem in Files) {
var filename = fileItem.ItemSpec;
FileInfo fi = new FileInfo(filename);
if (!fi.FullName.StartsWith(di.FullName)) {
Log.LogError(string.Format("{0} not in {1}", filename, WorkingDirectory));
return false;
}
var archivename = fi.FullName.Substring(di.FullName.Length).TrimStart(new char [] { '\\', '/' });
using (Stream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
using (Stream fileStreamInZip = archive.CreateEntry(archivename).Open())
fileStream.CopyTo(fileStreamInZip);
}
}
return true;
} catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
} finally {
if (cwd != null) {
Environment.CurrentDirectory = cwd;
}
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="FileUpdate" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<Expression ParameterType="System.String" Required="true" />
<Replacement ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
System.Text.RegularExpressions.Regex replaceRegex = new System.Text.RegularExpressions.Regex(Expression);
foreach(ITaskItem item in Files) {
string fileName = item.ItemSpec;
Log.LogMessage(string.Format("Updating File \"{0}\".", fileName));
string buffer = File.ReadAllText(fileName);
buffer = replaceRegex.Replace(buffer, Replacement);
File.WriteAllText(fileName, buffer);
}
} catch(Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
return true;
]]>
</Code>
</Task>
</UsingTask>
</Project>