|
10 | 10 | <Using Namespace="System.IO.Compression" /> |
11 | 11 | <Code Type="Fragment" Language="cs"> |
12 | 12 | <![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"); |
22 | 27 |
|
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 | + } |
36 | 61 | } |
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 | | - } |
47 | 62 | ]]> |
48 | 63 | </Code> |
49 | 64 | </Task> |
|
59 | 74 | <Using Namespace="System.Text.RegularExpressions" /> |
60 | 75 | <Code Type="Fragment" Language="cs"> |
61 | 76 | <![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 | + } |
76 | 91 | ]]> |
77 | 92 | </Code> |
78 | 93 | </Task> |
|
0 commit comments