PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/windows-phone-8/templates/standalone/cordova/lib/CordovaDeploy/CordovaDeploy/Program.cs

https://bitbucket.org/leispire/phonegap
C# | 424 lines | 314 code | 57 blank | 53 comment | 36 complexity | 2227e22c029090bdc1c1f6ab459d72e0 MD5 | raw file
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Collections.ObjectModel;
  22. using System.Diagnostics;
  23. using System.IO;
  24. using System.Xml.XPath;
  25. using System.Xml;
  26. using System.Xml.Linq;
  27. using System.Globalization;
  28. // Windows Phone Emulator Libraries
  29. using Microsoft.SmartDevice.Connectivity;
  30. using Microsoft.SmartDevice.Connectivity.Interface;
  31. using Microsoft.SmartDevice.MultiTargeting.Connectivity;
  32. namespace CordovaDeploy
  33. {
  34. class DeployTool
  35. {
  36. static void Usage()
  37. {
  38. Log("Usage: CordovaDeploy [ -devices BuildOutputPath -d:DeviceIndex ]");
  39. Log(" -devices : lists the devices and exits");
  40. Log(" BuildOutputPath : path to the built application, typically Bin/Debug/ or Bin/Release/");
  41. Log(" -d : index of the device to deploy, default is 0 ");
  42. Log("examples:");
  43. Log(" CordovaDeploy -devices");
  44. Log(" CordovaDeploy Bin/Debug");
  45. Log(" CordovaDeploy Bin/Release -d:1");
  46. }
  47. static void ReadWait()
  48. {
  49. // This is used when running in Visual Studio, the Command Window is created at launch, and disappears at the
  50. // end of the program run, this let's us see the output before the window is closed.
  51. /*
  52. Console.WriteLine("\nPress ENTER to continue...");
  53. Console.Read();
  54. */
  55. }
  56. static void Log(string msg, bool error = false)
  57. {
  58. Debug.WriteLine(msg);
  59. if (error)
  60. {
  61. Console.Error.WriteLine(msg);
  62. }
  63. else
  64. {
  65. Console.Out.WriteLine(msg);
  66. }
  67. }
  68. static Guid ReadAppId(string root)
  69. {
  70. Guid appID = Guid.Empty;
  71. string manifestFilePath = root + @"\Properties\WMAppManifest.xml";
  72. if (File.Exists(manifestFilePath))
  73. {
  74. XDocument xdoc = XDocument.Load(manifestFilePath);
  75. var appNode = xdoc.Root.Descendants("App").FirstOrDefault();
  76. if (appNode != null)
  77. {
  78. string guidStr = appNode.Attribute("ProductID").Value;
  79. appID = new Guid(guidStr);
  80. }
  81. else
  82. {
  83. Log(string.Format("Unable to find appID, expected to find an App.ProductID property defined in the file {0}", manifestFilePath), true);
  84. }
  85. }
  86. else
  87. {
  88. Log(string.Format("Error: the file {0} does not exist", manifestFilePath), true);
  89. }
  90. return appID;
  91. }
  92. static void ListDevices()
  93. {
  94. MultiTargetingConnectivity mtConn = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);
  95. Collection<ConnectableDevice> deviceList = mtConn.GetConnectableDevices();
  96. for (int index = 0; index < deviceList.Count; index++)
  97. {
  98. ConnectableDevice d = deviceList[index];
  99. string info = string.Format("{0} : {1} : {2}", index.ToString(), d.Id, d.Name);
  100. Log(info);
  101. }
  102. }
  103. static ConnectableDevice GetDeviceAtIndex(int index)
  104. {
  105. MultiTargetingConnectivity mtConn = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);
  106. Collection<ConnectableDevice> deviceList = mtConn.GetConnectableDevices();
  107. return deviceList[index];
  108. }
  109. static void Main(string[] args)
  110. {
  111. int deviceIndex = 0;
  112. string iconFilePath = "";
  113. string xapFilePath = "";
  114. Guid appID = Guid.Empty;
  115. string root = Directory.GetCurrentDirectory();
  116. if (args.Length < 1)
  117. {
  118. Usage();
  119. ReadWait();
  120. return;
  121. }
  122. else if (args[0] == "-devices")
  123. {
  124. ListDevices();
  125. ReadWait();
  126. return;
  127. }
  128. else if (args.Length > 1 && args[1].StartsWith("-d:"))
  129. {
  130. deviceIndex = int.Parse(args[1].Substring(3));
  131. }
  132. if (Directory.Exists(args[0]))
  133. {
  134. DirectoryInfo info = new DirectoryInfo(args[0]);
  135. root = info.FullName;
  136. }
  137. appID = ReadAppId(root);
  138. if (appID == Guid.Empty)
  139. {
  140. return; // Logging of errors is done in ReadAppId
  141. }
  142. if (File.Exists(root + @"\ApplicationIcon.png"))
  143. {
  144. iconFilePath = root + @"\ApplicationIcon.png";
  145. }
  146. else
  147. {
  148. Log(string.Format("Error: could not find application icon at {0}", root + @"\ApplicationIcon.png"), true);
  149. ReadWait();
  150. return;
  151. }
  152. try {
  153. xapFilePath = Directory.GetFiles(root + @"\Bin\Debug", "*.xap").FirstOrDefault();
  154. } catch (DirectoryNotFoundException e) {
  155. try {
  156. xapFilePath = Directory.GetFiles(root + @"\Bin\Release", "*.xap").FirstOrDefault();
  157. } catch (DirectoryNotFoundException ex) {
  158. Log(string.Format("Error: could not find project build directoy in {0}", root), true);
  159. Log("make sure your app has been successfully built before deploying.", true);
  160. }
  161. }
  162. if (string.IsNullOrEmpty(xapFilePath))
  163. {
  164. Log(string.Format("Error: could not find application .xap in folder {0}", root), true);
  165. ReadWait();
  166. return;
  167. }
  168. ConnectableDevice deviceConn = GetDeviceAtIndex(deviceIndex);
  169. Log("Connecting to device :: " + deviceConn.Id + " : " + deviceConn.Name);
  170. try
  171. {
  172. IDevice device = deviceConn.Connect();
  173. IRemoteApplication app = null;
  174. if (device.IsApplicationInstalled(appID))
  175. {
  176. Log("Uninstalling XAP from " + deviceConn.Name);
  177. app = device.GetApplication(appID);
  178. app.Uninstall();
  179. }
  180. Log("Installing app on " + deviceConn.Name);
  181. app = device.InstallApplication(appID, appID, "NormalApp", iconFilePath, xapFilePath);
  182. Log("Launching app on " + deviceConn.Name);
  183. app.Launch();
  184. // To Stop :
  185. //app.TerminateRunningInstances();
  186. device.Disconnect();
  187. ReadWait();
  188. }
  189. catch (Exception ex)
  190. {
  191. Log("Error :: " + ex.Message, true);
  192. }
  193. }
  194. // To read and write ISO storage files!! :
  195. /*
  196. try
  197. {
  198. IRemoteIsolatedStorageFile isoStore = app.GetIsolatedStore();
  199. remoteIsolatedStorageFile.ReceiveFile("sourcePath", "destPath", true);
  200. }
  201. catch (Exception ex) { }
  202. */
  203. }
  204. class Program
  205. {
  206. static void Usage()
  207. {
  208. Log("Usage: CordovaDeploy [ -devices BuildOutputPath -d:DeviceIndex ]");
  209. Log(" -devices : lists the devices and exits");
  210. Log(" BuildOutputPath : path to the built application, typically Bin/Debug/ or Bin/Release/");
  211. Log(" -d : index of the device to deploy, default is 0 ");
  212. Log("examples:");
  213. Log(" CordovaDeploy -devices");
  214. Log(" CordovaDeploy Bin/Debug");
  215. Log(" CordovaDeploy Bin/Release -d:1");
  216. }
  217. static void ReadWait()
  218. {
  219. // This is used when running in Visual Studio, the Command Window is created at launch, and disappears at the
  220. // end of the program run, this let's us see the output before the window is closed.
  221. /*
  222. Console.WriteLine("\nPress ENTER to continue...");
  223. Console.Read();
  224. */
  225. }
  226. static void Log(string msg, bool error = false)
  227. {
  228. Debug.WriteLine(msg);
  229. if (error)
  230. {
  231. Console.Error.WriteLine(msg);
  232. }
  233. else
  234. {
  235. Console.Out.WriteLine(msg);
  236. }
  237. }
  238. static Guid ReadAppId(string root)
  239. {
  240. Guid appID = Guid.Empty;
  241. string manifestFilePath = root + @"\Properties\WMAppManifest.xml";
  242. if (File.Exists(manifestFilePath))
  243. {
  244. XDocument xdoc = XDocument.Load(manifestFilePath);
  245. var appNode = xdoc.Root.Descendants("App").FirstOrDefault();
  246. if (appNode != null)
  247. {
  248. string guidStr = appNode.Attribute("ProductID").Value;
  249. appID = new Guid(guidStr);
  250. }
  251. else
  252. {
  253. Log(string.Format("Unable to find appID, expected to find an App.ProductID property defined in the file {0}", manifestFilePath), true);
  254. }
  255. }
  256. else
  257. {
  258. Log(string.Format("Error: the file {0} does not exist", manifestFilePath), true);
  259. }
  260. return appID;
  261. }
  262. static void ListDevices()
  263. {
  264. MultiTargetingConnectivity mtConn = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);
  265. Collection<ConnectableDevice> deviceList = mtConn.GetConnectableDevices();
  266. for (int index = 0; index < deviceList.Count; index++)
  267. {
  268. ConnectableDevice d = deviceList[index];
  269. string info = string.Format("{0} : {1} : {2}", index.ToString(), d.Id, d.Name);
  270. Log(info);
  271. }
  272. }
  273. static ConnectableDevice GetDeviceAtIndex(int index)
  274. {
  275. MultiTargetingConnectivity mtConn = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);
  276. Collection<ConnectableDevice> deviceList = mtConn.GetConnectableDevices();
  277. return deviceList[index];
  278. }
  279. static void Main(string[] args)
  280. {
  281. int deviceIndex = 0;
  282. string iconFilePath = "";
  283. string xapFilePath = "";
  284. Guid appID = Guid.Empty;
  285. string root = Directory.GetCurrentDirectory();
  286. if (args.Length < 1)
  287. {
  288. Usage();
  289. ReadWait();
  290. return;
  291. }
  292. else if (args[0] == "-devices")
  293. {
  294. ListDevices();
  295. ReadWait();
  296. return;
  297. }
  298. else if (args.Length > 1 && args[1].StartsWith("-d:"))
  299. {
  300. deviceIndex = int.Parse(args[1].Substring(3));
  301. }
  302. if (Directory.Exists(args[0]))
  303. {
  304. DirectoryInfo info = new DirectoryInfo(args[0]);
  305. root = info.FullName;
  306. }
  307. appID = ReadAppId(root);
  308. if (appID == Guid.Empty)
  309. {
  310. return; // Logging of errors is done in ReadAppId
  311. }
  312. if (File.Exists(root + @"\ApplicationIcon.png"))
  313. {
  314. iconFilePath = root + @"\ApplicationIcon.png";
  315. }
  316. else
  317. {
  318. Log(string.Format("Error: could not find application icon at {0}", root + @"\ApplicationIcon.png"), true);
  319. ReadWait();
  320. return;
  321. }
  322. xapFilePath = Directory.GetFiles(root + @"\Bin\Debug", "*.xap").FirstOrDefault();
  323. if (string.IsNullOrEmpty(xapFilePath))
  324. {
  325. Log(string.Format("Error: could not find application .xap in folder {0}", root), true);
  326. ReadWait();
  327. return;
  328. }
  329. ConnectableDevice deviceConn = GetDeviceAtIndex(deviceIndex);
  330. Log("Connecting to device :: " + deviceConn.Id + " : " + deviceConn.Name);
  331. try
  332. {
  333. IDevice device = deviceConn.Connect();
  334. IRemoteApplication app = null;
  335. if (device.IsApplicationInstalled(appID))
  336. {
  337. Log("Uninstalling XAP from " + deviceConn.Name);
  338. app = device.GetApplication(appID);
  339. app.Uninstall();
  340. }
  341. Log("Installing app on " + deviceConn.Name);
  342. app = device.InstallApplication(appID, appID, "NormalApp", iconFilePath, xapFilePath);
  343. Log("Launching app on " + deviceConn.Name);
  344. app.Launch();
  345. // To Stop :
  346. //app.TerminateRunningInstances();
  347. device.Disconnect();
  348. ReadWait();
  349. }
  350. catch (Exception ex)
  351. {
  352. Log("Error :: " + ex.Message, true);
  353. }
  354. }
  355. // To read and write ISO storage files!! :
  356. /*
  357. try
  358. {
  359. IRemoteIsolatedStorageFile isoStore = app.GetIsolatedStore();
  360. remoteIsolatedStorageFile.ReceiveFile("sourcePath", "destPath", true);
  361. }
  362. catch (Exception ex) { }
  363. */
  364. }
  365. }