|
4 | 4 | <ParameterGroup> |
5 | 5 | <ZipFileName ParameterType="System.String" Required="true" /> |
6 | 6 | <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> |
7 | | - <WorkingDirectory ParameterType="System.String" Required="false" /> |
| 7 | + <WorkingDirectory ParameterType="System.String" Required="true" /> |
8 | 8 | </ParameterGroup> |
9 | 9 | <Task> |
10 | 10 | <Reference Include="System.IO.Compression" /> |
11 | 11 | <Using Namespace="System.IO.Compression" /> |
12 | 12 | <Code Type="Fragment" Language="cs"> |
13 | 13 | <![CDATA[ |
14 | | - var cwd = string.Empty; |
| 14 | + string cwd = null; |
15 | 15 | try { |
16 | | - if(!string.IsNullOrWhiteSpace(WorkingDirectory)) { |
17 | | - if(!Directory.Exists(WorkingDirectory)) { |
18 | | - Log.LogError(string.Format("{0} doesn't exist", WorkingDirectory)); |
19 | | - return false; |
20 | | - } |
21 | | - cwd = Environment.CurrentDirectory; |
22 | | - Environment.CurrentDirectory = WorkingDirectory; |
| 16 | + var di = new DirectoryInfo(WorkingDirectory); |
| 17 | + if (!di.Exists) { |
| 18 | + Log.LogError(string.Format("{0} doesn't exist", WorkingDirectory)); |
| 19 | + return false; |
23 | 20 | } |
| 21 | + cwd = Environment.CurrentDirectory; |
| 22 | + Environment.CurrentDirectory = di.FullName; |
| 23 | +
|
24 | 24 | using (Stream zipStream = new FileStream(Path.GetFullPath(ZipFileName), FileMode.Create, FileAccess.Write)) |
25 | 25 | using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) { |
26 | 26 | foreach (ITaskItem fileItem in Files) { |
27 | | - string filename = fileItem.ItemSpec; |
28 | | - using (Stream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read)) |
29 | | - using (Stream fileStreamInZip = archive.CreateEntry(new FileInfo(filename).Name).Open()) |
| 27 | + var filename = fileItem.ItemSpec; |
| 28 | + FileInfo fi = new FileInfo(filename); |
| 29 | + if (!fi.FullName.StartsWith(di.FullName)) { |
| 30 | + Log.LogError(string.Format("{0} not in {1}", filename, WorkingDirectory)); |
| 31 | + return false; |
| 32 | + } |
| 33 | + var archivename = fi.FullName.Substring(di.FullName.Length).TrimStart(new char [] { '\\', '/' }); |
| 34 | + using (Stream fileStream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read)) |
| 35 | + using (Stream fileStreamInZip = archive.CreateEntry(archivename).Open()) |
30 | 36 | fileStream.CopyTo(fileStreamInZip); |
31 | 37 | } |
32 | | - } |
| 38 | + } |
33 | 39 | return true; |
34 | 40 | } catch (Exception ex) { |
35 | 41 | Log.LogErrorFromException(ex); |
36 | 42 | return false; |
37 | 43 | } finally { |
38 | | - if(!string.IsNullOrWhiteSpace(cwd)) { |
39 | | - Environment.CurrentDirectory = cwd; |
| 44 | + if (cwd != null) { |
| 45 | + Environment.CurrentDirectory = cwd; |
40 | 46 | } |
41 | 47 | } |
42 | 48 | ]]> |
|
53 | 59 | <Task> |
54 | 60 | <Using Namespace="System.Text.RegularExpressions" /> |
55 | 61 | <Code Type="Fragment" Language="cs"> |
56 | | - <![CDATA[ |
57 | | - try { |
58 | | - System.Text.RegularExpressions.Regex replaceRegex = new System.Text.RegularExpressions.Regex(Expression); |
59 | | - foreach(ITaskItem item in Files) { |
60 | | - string fileName = item.ItemSpec; |
61 | | - Log.LogMessage(string.Format("Updating File \"{0}\".", fileName)); |
62 | | - string buffer = File.ReadAllText(fileName); |
63 | | - buffer = replaceRegex.Replace(buffer, Replacement); |
64 | | - File.WriteAllText(fileName, buffer); |
65 | | - } |
66 | | - } catch(Exception ex) { |
| 62 | + <![CDATA[ |
| 63 | + try { |
| 64 | + System.Text.RegularExpressions.Regex replaceRegex = new System.Text.RegularExpressions.Regex(Expression); |
| 65 | + foreach(ITaskItem item in Files) { |
| 66 | + string fileName = item.ItemSpec; |
| 67 | + Log.LogMessage(string.Format("Updating File \"{0}\".", fileName)); |
| 68 | + string buffer = File.ReadAllText(fileName); |
| 69 | + buffer = replaceRegex.Replace(buffer, Replacement); |
| 70 | + File.WriteAllText(fileName, buffer); |
| 71 | + } |
| 72 | + } catch(Exception ex) { |
67 | 73 | Log.LogErrorFromException(ex); |
68 | 74 | return false; |
69 | | - } |
70 | | - return true; |
71 | | - ]]> |
| 75 | + } |
| 76 | + return true; |
| 77 | + ]]> |
72 | 78 | </Code> |
73 | 79 | </Task> |
74 | 80 | </UsingTask> |
|
0 commit comments