Skip to content

Commit f05da8d

Browse files
committed
Align Tasks.targets code snippets with IronPython
1 parent a646d7e commit f05da8d

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

eng/Tasks.targets

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,55 @@
1010
<Using Namespace="System.IO.Compression" />
1111
<Code Type="Fragment" Language="cs">
1212
<![CDATA[
13-
string cwd = null;
14-
try {
15-
var di = new DirectoryInfo(WorkingDirectory);
16-
if (!di.Exists) {
17-
Log.LogError(string.Format("{0} doesn't exist", WorkingDirectory));
18-
return false;
19-
}
20-
cwd = Environment.CurrentDirectory;
21-
Environment.CurrentDirectory = di.FullName;
13+
string cwd = null;
14+
try {
15+
var di = new DirectoryInfo(WorkingDirectory);
16+
if (!di.Exists) {
17+
Log.LogError(string.Format("{0} doesn't exist", WorkingDirectory));
18+
return (Success = false);
19+
}
20+
cwd = Environment.CurrentDirectory;
21+
Environment.CurrentDirectory = di.FullName;
22+
// rw-r--r-- file BSD
23+
const int regAttr = (0b110100100 + 0x8000) << 16;
24+
// rwxr-xr-x file BSD
25+
const int exeAttr = (0b111101101 + 0x8000) << 16;
26+
var propExternalAttributes = typeof(ZipArchiveEntry).GetProperty("ExternalAttributes");
2227
23-
using (Stream zipStream = new FileStream(Path.GetFullPath(ZipFileName), FileMode.Create, FileAccess.Write))
24-
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) {
25-
foreach (ITaskItem fileItem in Files) {
26-
var filename = fileItem.ItemSpec;
27-
FileInfo fi = new FileInfo(filename);
28-
if (!fi.FullName.StartsWith(di.FullName)) {
29-
Log.LogError(string.Format("{0} not in {1}", filename, WorkingDirectory));
30-
return false;
31-
}
32-
var archivename = fi.FullName.Substring(di.FullName.Length).TrimStart(new char [] { '\\', '/' });
33-
using (Stream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
34-
using (Stream fileStreamInZip = archive.CreateEntry(archivename).Open())
35-
fileStream.CopyTo(fileStreamInZip);
28+
using Stream zipStream = new FileStream(Path.GetFullPath(ZipFileName), FileMode.Create, FileAccess.Write);
29+
using ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create);
30+
31+
foreach (ITaskItem fileItem in Files) {
32+
string evaluatedInclude = fileItem.ItemSpec;
33+
var fi = new FileInfo(evaluatedInclude);
34+
if (!fi.FullName.StartsWith(di.FullName)) {
35+
Log.LogError(string.Format("{0} not in {1}", evaluatedInclude, WorkingDirectory));
36+
return (Success = false);
37+
}
38+
string filename = fileItem.GetMetadata("Link");
39+
if (string.IsNullOrEmpty(filename)) {
40+
filename = fi.FullName;
41+
}
42+
var archivename = fi.FullName.Substring(di.FullName.Length).Replace('\\', '/').TrimStart(new char [] { '/' });
43+
bool isExe = archivename.EndsWith(".ps1") || archivename.EndsWith(".sh");
44+
45+
ZipArchiveEntry entry = archive.CreateEntry(archivename);
46+
using Stream fileStreamInZip = entry.Open();
47+
using Stream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
48+
49+
fileStream.CopyTo(fileStreamInZip);
50+
//entry.ExternalAttributes = isExe ? exeAttr : regAttr;
51+
propExternalAttributes.SetValue(entry, isExe ? exeAttr : regAttr);
52+
}
53+
Success = true;
54+
} catch (Exception ex) {
55+
Success = false;
56+
Log.LogErrorFromException(ex);
57+
} finally {
58+
if (cwd != null) {
59+
Environment.CurrentDirectory = cwd;
60+
}
3661
}
37-
}
38-
return true;
39-
} catch (Exception ex) {
40-
Log.LogErrorFromException(ex);
41-
return false;
42-
} finally {
43-
if (cwd != null) {
44-
Environment.CurrentDirectory = cwd;
45-
}
46-
}
4762
]]>
4863
</Code>
4964
</Task>
@@ -59,20 +74,20 @@
5974
<Using Namespace="System.Text.RegularExpressions" />
6075
<Code Type="Fragment" Language="cs">
6176
<![CDATA[
62-
try {
63-
System.Text.RegularExpressions.Regex replaceRegex = new System.Text.RegularExpressions.Regex(Expression);
64-
foreach(ITaskItem item in Files) {
65-
string fileName = item.ItemSpec;
66-
Log.LogMessage(string.Format("Updating File \"{0}\".", fileName));
67-
string buffer = File.ReadAllText(fileName);
68-
buffer = replaceRegex.Replace(buffer, Replacement);
69-
File.WriteAllText(fileName, buffer);
70-
}
71-
} catch(Exception ex) {
72-
Log.LogErrorFromException(ex);
73-
return false;
74-
}
75-
return true;
77+
try {
78+
System.Text.RegularExpressions.Regex replaceRegex = new System.Text.RegularExpressions.Regex(Expression);
79+
foreach (ITaskItem item in Files) {
80+
string fileName = item.ItemSpec;
81+
Log.LogMessage(string.Format("Updating File \"{0}\".", fileName));
82+
string buffer = File.ReadAllText(fileName);
83+
buffer = replaceRegex.Replace(buffer, Replacement);
84+
File.WriteAllText(fileName, buffer);
85+
}
86+
Success = true;
87+
} catch (Exception ex) {
88+
Success = false;
89+
Log.LogErrorFromException(ex);
90+
}
7691
]]>
7792
</Code>
7893
</Task>

0 commit comments

Comments
 (0)