-
Notifications
You must be signed in to change notification settings - Fork 9
/
Common.tasks
134 lines (118 loc) · 4.82 KB
/
Common.tasks
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="DownloadFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Address ParameterType="System.String" Required="true"/>
<FileName ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Class" Language="cs">
<![CDATA[
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System;
using System.Threading;
using System.IO;
using System.Net;
public class DownloadFile : Task, ICancelableTask
{
String _Address;
String _FileName;
WebClient client = new WebClient();
Exception error;
bool cancelled = false;
public string Address
{
get
{
return _Address;
}
set
{
_Address = value;
}
}
public string FileName
{
get
{
return _FileName;
}
set
{
_FileName = value;
}
}
public override bool Execute()
{
Log.LogMessage(MessageImportance.Normal, "Downloading: {0}", FileName);
client.DownloadProgressChanged += (o, e) =>
{
Log.LogMessage(MessageImportance.Normal, "Downloaded {0:N0} of {1:N0} bytes. {2}% ", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
};
client.DownloadFileCompleted += (o, e) =>
{
if (e.Cancelled)
Log.LogMessage(MessageImportance.Normal, "Download cancelled!");
else if (e.Error == null)
Log.LogMessage(MessageImportance.Normal, "Download finished!");
lock (this)
{
cancelled = e.Cancelled;
error = e.Error;
System.Threading.Monitor.Pulse(this);
}
};
lock (this)
{
client.DownloadFileAsync(new Uri(Address), FileName);
Monitor.Wait(this);
}
if (cancelled)
File.Delete(FileName);
if (error != null)
Log.LogErrorFromException(error, false, false, Address);
return error == null;
}
public void Cancel()
{
client.CancelAsync();
}
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask TaskName="UnzipFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<FileName ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Console.Out.WriteLine("Unzip: " + FileName);
using (System.IO.Compression.ZipArchive archive = System.IO.Compression.ZipFile.OpenRead(FileName))
{
foreach (System.IO.Compression.ZipArchiveEntry entry in archive.Entries)
{
Log.LogMessage(MessageImportance.Normal, " {0}", entry.FullName);
String entryFullName = Path.Combine(".", entry.FullName);
String entryPath = Path.GetDirectoryName(entryFullName);
if (!Directory.Exists(entryPath))
{
Directory.CreateDirectory(entryPath);
}
String entryFn = Path.GetFileName(entryFullName);
if (!String.IsNullOrEmpty(entryFn))
{
System.IO.Compression.ZipFileExtensions.ExtractToFile(entry, entryFullName, true);
}
}
}
]]>
</Code>
</Task>
</UsingTask>
</Project>