2
0

downloadpackage.task 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3. <ImportGroup Label="PropertySheets">
  4. <Import Project="basedir.props" Condition=" '$(BaseDirImported)' == ''"/>
  5. </ImportGroup>
  6. <UsingTask TaskName="DownloadPackageTask"
  7. TaskFactory="CodeTaskFactory"
  8. AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
  9. <ParameterGroup>
  10. <package Required="true" />
  11. <expectfileordirectory Required="true" />
  12. <outputfolder />
  13. <outputfilename />
  14. <extractto />
  15. <moveafter />
  16. <archivecontains />
  17. </ParameterGroup>
  18. <Task>
  19. <Reference Include="Microsoft.Build" />
  20. <Reference Include="Microsoft.Build.Framework" />
  21. <Reference Include="Microsoft.Build.Utilities.Core" />
  22. <Code Type="Class" Language="cs">
  23. <![CDATA[
  24. using System;
  25. using System.IO;
  26. using System.Threading;
  27. using Microsoft.Build.Framework;
  28. using System.Reflection;
  29. using Microsoft.Build.Execution;
  30. using System.Net;
  31. using System.ComponentModel;
  32. using System.Diagnostics;
  33. public class DownloadPackageTask : Microsoft.Build.Utilities.Task, Microsoft.Build.Framework.ICancelableTask
  34. {
  35. public class State
  36. {
  37. public string filename;
  38. public int progress;
  39. }
  40. protected ManualResetEvent TaskCanceled { get; private set; }
  41. public void Cancel()
  42. {
  43. TaskCanceled.Set();
  44. }
  45. private string basedir;
  46. [Required]
  47. public string package { get; set; }
  48. [Required]
  49. public string expectfileordirectory { get; set; }
  50. public string outputfolder { get; set; }
  51. public string outputfilename { get; set; }
  52. public string extractto { get; set; }
  53. public string moveafter { get; set; }
  54. public string archivecontains { get; set; }
  55. internal static bool FileOrDirectoryExists(string name)
  56. {
  57. return (Directory.Exists(name) || File.Exists(name));
  58. }
  59. public override bool Execute()
  60. {
  61. basedir = Path.GetFullPath(@"$(BaseDir)");
  62. TaskCanceled = new ManualResetEvent(false);
  63. //Log.LogMessage(MessageImportance.High,
  64. // "Checking for package \"" + package + "\".");
  65. //Log.LogMessage(MessageImportance.High,
  66. // "BaseDir \"" + basedir + "\"");
  67. //Log.LogMessage(MessageImportance.High,
  68. // "expectfileordirectory \"" + expectfileordirectory + "\"");
  69. string librarypath = Path.Combine(basedir, "libs");
  70. Mutex m = new Mutex(false, Path.Combine(librarypath, package).Replace(":", "/").Replace("\\","/"));
  71. m.WaitOne();
  72. if (FileOrDirectoryExists(expectfileordirectory))
  73. {
  74. //Log.LogMessage(MessageImportance.High,
  75. // "Package \"" + package + "\" exists. Do nothing.");
  76. }
  77. else
  78. {
  79. Log.LogMessage(MessageImportance.High,
  80. "Start downloading package \"" + package + "\".");
  81. using (var client = new System.Net.WebClient())
  82. {
  83. Uri uri = new Uri(package);
  84. string urifilename = Path.GetFileName(uri.LocalPath);
  85. string output = Path.Combine(outputfolder ?? librarypath, (outputfilename ?? urifilename));
  86. //if (!File.Exists(output)) // Uncomment to skip download if exists
  87. {
  88. var syncObject = new State
  89. {
  90. filename = urifilename,
  91. progress = -1
  92. };
  93. lock (syncObject)
  94. {
  95. client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
  96. client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
  97. client.DownloadFileAsync(uri, output, syncObject);
  98. while (!Monitor.Wait(syncObject, 1000))
  99. {
  100. if (TaskCanceled.WaitOne(0))
  101. {
  102. client.CancelAsync();
  103. Monitor.Wait(syncObject);
  104. if (File.Exists(output))
  105. {
  106. Log.LogMessage(MessageImportance.High,
  107. "Deleting incomplete file " + output + " for package \"" + package + "\".");
  108. File.Delete(output);
  109. }
  110. Log.LogMessage(MessageImportance.High,
  111. "Downloading canceled for package \"" + package + "\".");
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. if (File.Exists(output))
  118. {
  119. // Successful download.
  120. string extracttofolder = Path.GetFullPath((extractto ?? Path.GetDirectoryName(output)) + "/");
  121. if (Path.GetExtension(output) != ".exe")
  122. {
  123. Extract(output, extracttofolder);
  124. string filename = Path.Combine(Path.GetDirectoryName(extracttofolder), Path.GetFileNameWithoutExtension(output));
  125. Log.LogMessage(MessageImportance.High,
  126. "Filename \"" + filename + "\".");
  127. if (archivecontains == null || archivecontains.Trim() == "")
  128. archivecontains = "tarball";
  129. if (File.Exists(filename) && archivecontains.ToLower() == "tarball")
  130. {
  131. Extract(filename, extracttofolder);
  132. File.Delete(filename);
  133. }
  134. }
  135. if (moveafter != null && moveafter != "") {
  136. var items = moveafter.Split('|');
  137. string s = extracttofolder + items[0];
  138. string d = extracttofolder + items[1];
  139. Log.LogMessage(MessageImportance.High,
  140. "Move directory from \"" + s + "\" to \"" + d + "\".");
  141. Directory.Move(s, d);
  142. }
  143. }
  144. }
  145. if (!TaskCanceled.WaitOne(0))
  146. {
  147. Log.LogMessage(MessageImportance.High,
  148. "Downloading finished for package \"" + package + "\".");
  149. }
  150. }
  151. m.ReleaseMutex();
  152. return true;
  153. }
  154. private void Extract(string filename, string extracttofolder)
  155. {
  156. string arctool = Path.Combine(new string[] { basedir, "libs", "win32", "7za1701.exe" });
  157. string args = " x \"" + filename + "\" -y -o\"" + extracttofolder + "\"";
  158. Log.LogMessage(MessageImportance.High,
  159. "arctool : " + arctool);
  160. Log.LogMessage(MessageImportance.High,
  161. "args : " + args);
  162. Log.LogMessage(MessageImportance.High,
  163. "WorkingDirectory : " + Path.GetDirectoryName(arctool));
  164. var proc = new Process
  165. {
  166. StartInfo = new ProcessStartInfo
  167. {
  168. FileName = arctool,
  169. Arguments = args,
  170. UseShellExecute = false,
  171. RedirectStandardOutput = true,
  172. CreateNoWindow = true,
  173. WorkingDirectory = Path.GetDirectoryName(arctool)
  174. }
  175. };
  176. proc.Start();
  177. while (!proc.StandardOutput.EndOfStream)
  178. {
  179. string line = proc.StandardOutput.ReadLine();
  180. Log.LogMessage(MessageImportance.High,
  181. Path.GetFileName(filename) + " : " + line);
  182. if (TaskCanceled.WaitOne(0))
  183. {
  184. proc.Kill();
  185. break;
  186. }
  187. }
  188. proc.WaitForExit();
  189. }
  190. private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  191. {
  192. lock (e.UserState)
  193. {
  194. //releases blocked thread
  195. Monitor.Pulse(e.UserState);
  196. }
  197. }
  198. private string humanSize(double len)
  199. {
  200. string[] sizes = { "B", "KB", "MB", "GB", "TB" };
  201. int order = 0;
  202. while (len >= 1024 && order < sizes.Length - 1)
  203. {
  204. order++;
  205. len = len / 1024;
  206. }
  207. return String.Format("{0:0.##} {1}", len, sizes[order]);
  208. }
  209. private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
  210. {
  211. if (((State)e.UserState).progress < e.ProgressPercentage)
  212. {
  213. ((State)e.UserState).progress = e.ProgressPercentage;
  214. // Displays the transfer progress.
  215. Log.LogMessage(MessageImportance.High, ((State)e.UserState).filename + " : downloaded " + humanSize(e.BytesReceived) + " of " + humanSize(e.TotalBytesToReceive) + " " + e.ProgressPercentage + " % complete...");
  216. }
  217. }
  218. }
  219. ]]>
  220. </Code>
  221. </Task>
  222. </UsingTask>
  223. <Target Name="7za" BeforeTargets="Build">
  224. <DownloadPackageTask
  225. package="http://files.freeswitch.org/downloads/win32/7za1701.exe"
  226. expectfileordirectory="$(BaseDir)libs/win32/7za1701.exe"
  227. outputfolder="$(BaseDir)libs/win32/"
  228. outputfilename="7za1701.exe"
  229. extractto=""
  230. />
  231. </Target>
  232. <PropertyGroup>
  233. <downloadpackagetask_Imported>true</downloadpackagetask_Imported>
  234. </PropertyGroup>
  235. </Project>