forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasks.Targets
95 lines (90 loc) · 4.46 KB
/
Tasks.Targets
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
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="Current">
<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 (Success = false);
}
cwd = Environment.CurrentDirectory;
Environment.CurrentDirectory = di.FullName;
// rw-r--r-- file BSD
const int regAttr = (0b110100100 + 0x8000) << 16;
// rwxr-xr-x file BSD
const int exeAttr = (0b111101101 + 0x8000) << 16;
var propExternalAttributes = typeof(ZipArchiveEntry).GetProperty("ExternalAttributes");
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) {
string evaluatedInclude = fileItem.ItemSpec;
var fi = new FileInfo(evaluatedInclude);
if (!fi.FullName.StartsWith(di.FullName)) {
Log.LogError(string.Format("{0} not in {1}", evaluatedInclude, WorkingDirectory));
return (Success = false);
}
string filename = fileItem.GetMetadata("Link");
if (string.IsNullOrEmpty(filename)) {
filename = fi.FullName;
}
var archivename = fi.FullName.Substring(di.FullName.Length).Replace('\\', '/').TrimStart(new char [] { '/' });
bool isExe = archivename.EndsWith(".ps1") || archivename.EndsWith(".sh");
ZipArchiveEntry entry = archive.CreateEntry(archivename);
using Stream fileStreamInZip = entry.Open();
using Stream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
fileStream.CopyTo(fileStreamInZip);
//entry.ExternalAttributes = isExe ? exeAttr : regAttr;
propExternalAttributes.SetValue(entry, isExe ? exeAttr : regAttr);
}
Success = true;
} catch (Exception ex) {
Success = false;
Log.LogErrorFromException(ex);
} 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);
}
Success = true;
} catch(Exception ex) {
Success = false;
Log.LogErrorFromException(ex);
}
]]>
</Code>
</Task>
</UsingTask>
</Project>