PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/task/RunEmulatorConsoleTask.cs

https://github.com/moscrif/ide
C# | 500 lines | 294 code | 130 blank | 76 comment | 52 complexity | d3f3745a818742442e25e093f5d19e6f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using Moscrif.IDE.Workspace;
  7. using Moscrif.IDE.Extensions;
  8. namespace Moscrif.IDE.Task
  9. {
  10. public class RunEmulatorConsoleTask : ITask
  11. {
  12. List<TaskMessage> output = new List<TaskMessage>();
  13. StateEnum stateTask = StateEnum.OK;
  14. public RunEmulatorConsoleTask()
  15. {
  16. }
  17. #region ITask implementation
  18. public void Initialize (object dataObject)
  19. {
  20. }
  21. public bool ExecuteTask ()
  22. {
  23. if (MainClass.Settings.ClearConsoleBeforRuning)
  24. MainClass.MainWindow.OutputConsole.Clear();
  25. if(MainClass.MainWindow.RunningEmulator){
  26. output.Add( new TaskMessage(MainClass.Languages.Translate("emulator_is_running"),null,null));
  27. stateTask = StateEnum.ERROR;
  28. return false;
  29. }
  30. if(String.IsNullOrEmpty(MainClass.Workspace.ActualResolution)){
  31. output.Add(new TaskMessage(MainClass.Languages.Translate("pleas_select_resolution"),null,null));
  32. stateTask = StateEnum.ERROR;
  33. return false;
  34. }
  35. if(MainClass.Workspace.ActualProject == null){
  36. output.Add(new TaskMessage(MainClass.Languages.Translate("no_project_selected"),null,null));
  37. stateTask = StateEnum.ERROR;
  38. return false;
  39. }
  40. string newPath =System.IO.Path.Combine(MainClass.Settings.EmulatorDirectory,"generic.ini");
  41. try{
  42. if (File.Exists(newPath))
  43. File.Delete(newPath);
  44. File.Copy(MainClass.Workspace.ActualResolution,newPath);
  45. }catch (Exception ex){
  46. //output.Add("Create generic.ini failed!");
  47. output.Add(new TaskMessage(ex.Message));
  48. stateTask = StateEnum.ERROR;
  49. return false;
  50. }
  51. string cmd = Path.Combine(MainClass.Settings.EmulatorDirectory, "moscrif.exe");
  52. if(MainClass.Platform.IsMac){
  53. //Console.WriteLine("EmulatorDirectory --> {0}",MainClass.Settings.EmulatorDirectory);
  54. //cmd = "open";// + MainClass.Settings.EmulatorDirectory, "moscrif.app");
  55. string file = System.IO.Path.Combine( MainClass.Settings.EmulatorDirectory, "Moscrif.app");//.app
  56. file = System.IO.Path.Combine(file, "Contents");
  57. file = System.IO.Path.Combine(file, "MacOS");
  58. file = System.IO.Path.Combine(file, "Moscrif");
  59. cmd = file;
  60. //Moscrif.app/Contents/MacOS
  61. } else {
  62. if(!System.IO.File.Exists(cmd) ){
  63. output.Add(new TaskMessage(MainClass.Languages.Translate("emulator_not_found")));
  64. stateTask = StateEnum.ERROR;
  65. return false;
  66. }
  67. }
  68. AppFile appFile = MainClass.Workspace.ActualProject.AppFile;
  69. string projDir = Path.GetDirectoryName(appFile.ApplicationFile);
  70. if (!projDir.EndsWith(Path.DirectorySeparatorChar.ToString())) projDir += Path.DirectorySeparatorChar;
  71. string args = String.Format("/f {0} /d {1} /o console /w nowindow", Path.GetFileName(appFile.ApplicationFile), projDir);
  72. if(MainClass.Platform.IsMac){
  73. /*string file = System.IO.Path.Combine( MainClass.Settings.EmulatorDirectory, "Moscrif.app");//.app
  74. //file = System.IO.Path.Combine(file, "Contents");
  75. //file = System.IO.Path.Combine(file, "MacOS");
  76. //file = System.IO.Path.Combine(file, "Moscrif");
  77. //Moscrif.app/Contents/MacOS
  78. args = String.Format(" -a {0} --args -f {1} -d {2} -o console",file, Path.GetFileName(appFile.ApplicationFile), projDir);
  79. */
  80. args = String.Format(" -f {0} -d {1} -o console -w nowindow", Path.GetFileName(appFile.ApplicationFile), projDir);
  81. }
  82. try{
  83. Console.WriteLine("cmd ->> {0}",cmd);
  84. Console.WriteLine("args ->> {0}",args);
  85. Tool.Logger.Info( String.Format("cmd ->> {0}",cmd) );
  86. Tool.Logger.Info( String.Format("args ->> {0}",args) );
  87. MainClass.MainWindow.RunEmulator(cmd, args, MainClass.Settings.EmulatorDirectory,ProcessOutputChange);
  88. }catch (Exception ex){
  89. output.Add(new TaskMessage(ex.Message));
  90. stateTask = StateEnum.ERROR;
  91. return false;
  92. }
  93. return true;
  94. }
  95. void ProcessOutputChange(object sender, string message)
  96. {
  97. //Gtk.Application.Invoke(delegate
  98. //{
  99. //this.output.Add( new TaskMessage(">> " + message.Trim() + "<<"));
  100. MainClass.MainWindow.OutputConsole.WriteText(message);
  101. //return;
  102. /*string msg = message.Trim();
  103. if (String.IsNullOrEmpty(msg))
  104. return;*/
  105. /*string fileDitr = "";
  106. if (sender.GetType() == typeof(ProcessWrapper)) {
  107. ProcessWrapper pw = (ProcessWrapper)sender;
  108. if (pw.StartInfo != null) {
  109. fileDitr = pw.StartInfo.WorkingDirectory;
  110. }
  111. //(sender as ProcessWrapper).
  112. }*/
  113. ParseOutput(message);
  114. //Console.WriteLine(">> " + message.Trim() + "<<");
  115. //});
  116. }
  117. public void OnEndTaskWrite(object sender, string name, string status, List<TaskMessage> errors){
  118. if(EndTaskWrite!= null)
  119. EndTaskWrite(sender, name,status, errors);
  120. }
  121. // not use
  122. public void OnTaskOutputChanged(object sender, string name, string status, List<TaskMessage> errors){
  123. if(TaskOutputChanged!= null)
  124. TaskOutputChanged(sender, name,status, errors);
  125. }
  126. public event ProcessTaskHandler TaskOutputChanged;
  127. public event ProcessErrorHandler ErrorWrite;
  128. public event ProcessErrorHandler LogWrite;
  129. public event ProcessTaskHandler EndTaskWrite;
  130. void ITask.StopTask()
  131. {
  132. }
  133. //Error: Bad syntax : Expecting '<identifier>' or 'assert', found 'class'
  134. //  D:\Work\moscrift.Ide\bin\Debug\Workspace4\x000001\aaa.ms  11  14
  135. private string messageError;
  136. private bool isLog; // novy text
  137. private bool isOldLog; // predchadzajuci text
  138. private void ParseOutput(string message)
  139. {
  140. //Console.WriteLine("message->>>>>> {0} <<<<<<<<",message );
  141. //string[] messageSeparators = new string[] {"\n\n"};
  142. //string[] stringSeparators = new string[] {"\n"};//\n\n
  143. //string[] allMessage =message.Split(messageSeparators,StringSplitOptions.RemoveEmptyEntries);
  144. //foreach(string b in allMessage){
  145. string endMessage ="\n\n";//"\n\n"
  146. message = message.Replace("\r\n","\n");
  147. message = message.Replace("\n\r","\n");
  148. message = message.Replace("\r\r","\n");
  149. message = message.Replace("\r","\n");
  150. //string[] allLine = message.Split(messageSeparators,StringSplitOptions.RemoveEmptyEntries);
  151. //IEnumerable<string> allLine = message.SplitAndKeep (endMessage);
  152. //string[] allLine = message.Split (stringSeparators,StringSplitOptions.None);
  153. IEnumerable<string> allLine = message.SplitAndKeep ("\n");
  154. //string temp = message.Replace("\r","*");
  155. //temp = temp.Replace("\n","»");
  156. //Console.WriteLine("temp ->{0}",temp);
  157. /*foreach (string a in allLine)
  158. Console.WriteLine("------>"+a);*/
  159. foreach (string a in allLine){
  160. message = a;
  161. //Console.WriteLine("------>"+a+"<------");
  162. //MatchCollection match = Regex.Matches( dataBlock, @"(ERROR:.*?\t.*?\t.*?\t)", RegexOptions.Singleline );
  163. int indx = message.IndexOf("Error:"); // start erroru
  164. //Console.WriteLine("indx ->"+indx);
  165. if (indx >-1 ) isLog = false;
  166. if (indx <0){
  167. indx = message.IndexOf("Log-I:");
  168. if (indx >-1 ) isLog = true;
  169. }
  170. if (indx <0){
  171. indx = message.IndexOf("Log-E:");
  172. if (indx >-1 ) isLog = true;
  173. }
  174. if (indx <0){
  175. indx = message.IndexOf("Log-W:");
  176. if (indx >-1 ) isLog = true;
  177. }
  178. if (indx > -1) {
  179. // pred touto spravou existuje stara v buffry tak ju spracujem.
  180. if (!String.IsNullOrEmpty(messageError)){
  181. if(isOldLog)
  182. GetLog(messageError);
  183. else GetError(messageError);
  184. messageError = "";
  185. }
  186. if(!message.Contains(endMessage)){
  187. messageError = messageError +message;
  188. continue; // neukonceny error
  189. }
  190. isOldLog = isLog;
  191. messageError = message; //.Remove(0, indx + 6);
  192. // odoberem staru spravu
  193. if (indx>0) messageError = messageError.Remove(0,indx);
  194. if(isLog)
  195. GetLog(messageError);
  196. else GetError(messageError);
  197. messageError = "";
  198. continue;
  199. } else {
  200. messageError = messageError +message;
  201. if(!messageError.Contains(endMessage)){
  202. continue; // neukonceny error
  203. }
  204. indx = messageError.IndexOf("Error:");
  205. if (indx <0){
  206. indx = messageError.IndexOf("Log-I:");
  207. }
  208. if (indx <0){
  209. indx = messageError.IndexOf("Log-E:");
  210. }
  211. if (indx <0){
  212. indx = messageError.IndexOf("Log-W:");
  213. }
  214. if (indx>0) messageError = messageError.Remove(0,indx);
  215. if(isOldLog)
  216. GetLog(messageError);
  217. else GetError(messageError);
  218. messageError = "";
  219. }
  220. }
  221. //}
  222. }
  223. private TaskMessage GetError(string message){
  224. TaskMessage tm =new TaskMessage();
  225. if(String.IsNullOrEmpty(message)) return null;
  226. try{
  227. //Console.WriteLine("message_1->"+message+"<-");
  228. //Log-I: Log-W: Log-E:
  229. message= message.Replace("Error:","" );
  230. message =message.Replace("\t","»");
  231. message =message.Replace("\n\r","");
  232. message =message.Replace("\r\n","");
  233. message =message.Replace("\n","");
  234. message =message.Replace("\r","");
  235. //Console.WriteLine("messageError -> "+ message);
  236. //Console.WriteLine("message_1->"+message+"<-");
  237. string[] msg = message.Split('»');
  238. //Console.WriteLine("match.Count -> "+ msg.Length);
  239. if (msg.Length <3){
  240. //Tool.Logger.Error("message ->"+message+"<");
  241. return null;
  242. }
  243. /*foreach(string m in msg){
  244. Console.WriteLine(m);
  245. }*/
  246. //Console.WriteLine("msg.Length ->"+msg.Length);
  247. string error = msg[0];
  248. string filename = msg[1];
  249. string line =msg[2];
  250. if (msg[1].StartsWith("at") ){
  251. if (msg.Length <4){
  252. //Tool.Logger.Error("message ->"+message+"<");
  253. return null;
  254. }
  255. filename = msg[2];
  256. line =msg[3];
  257. //int indx = 4; // spracovanie dalsich vetiev (kazda zacina na at)
  258. if (msg.Length >4)
  259. tm.Child = GetChild(msg,4);
  260. }
  261. else{
  262. filename = msg[1];
  263. line =msg[2];
  264. }
  265. filename = filename.Replace('/',System.IO.Path.DirectorySeparatorChar);
  266. //Console.WriteLine("error -> "+ error);
  267. //Console.WriteLine("filename -> "+ filename);
  268. //Console.WriteLine("line -> "+ line);
  269. //tm =new TaskMessage(error, filename, line);
  270. tm.Message = error;
  271. tm.Line = line;
  272. tm.File = filename;
  273. this.output.Add(tm);
  274. this.stateTask = StateEnum.ERROR;
  275. messageError = null;
  276. if (ErrorWrite!=null){
  277. ErrorWrite(this,this.Name,this.StateTask.ToString(),tm);
  278. }
  279. } catch {
  280. }
  281. return tm;
  282. }
  283. private TaskMessage GetChild(string[] msg, int indx){
  284. TaskMessage tmChild =new TaskMessage();
  285. if (msg[indx].StartsWith("at")){
  286. indx++;
  287. if (indx >= msg.Length) return null;
  288. string fname = msg[indx].Replace('/',System.IO.Path.DirectorySeparatorChar);
  289. tmChild.File = fname;
  290. indx ++;
  291. if (indx < msg.Length) { // line tam byt nemusi
  292. if (!msg[indx].StartsWith("at"))
  293. tmChild.Line = msg[indx];
  294. else indx--; // ak tam line nieje (zacina na at) vratim index vratim index naspet
  295. indx++;
  296. if (indx >= msg.Length) return tmChild;
  297. tmChild.Child =GetChild(msg,indx);
  298. }
  299. }
  300. return tmChild;
  301. }
  302. private TaskMessage GetLog(string message){
  303. TaskMessage tm =new TaskMessage();
  304. try{
  305. //Log-I: Log-W: Log-E:
  306. message= message.Replace("LOG-I","" );
  307. message= message.Replace("LOG-W","" );
  308. message= message.Replace("LOG-E","" );
  309. message =message.Replace("\t","»");
  310. message =message.Replace("\n\r","");
  311. message =message.Replace("\r\n","");
  312. message =message.Replace("\n","");
  313. message =message.Replace("\r","");
  314. //Console.WriteLine("messageError -> "+ message);
  315. string[] msg = message.Split('»');
  316. //Console.WriteLine("match.Count -> "+ msg.Length);
  317. if (msg.Length <3){
  318. //Tool.Logger.Error("message ->"+message+"<");
  319. return null;
  320. }
  321. string filename= msg[0];
  322. string line= msg[1];
  323. string error=msg[2];
  324. filename = filename.Replace('/',System.IO.Path.DirectorySeparatorChar);
  325. //Console.WriteLine("error -> "+ error);
  326. //Console.WriteLine("filename -> "+ filename);
  327. //Console.WriteLine("line -> "+ line);
  328. tm =new TaskMessage(error, filename, line);
  329. //this.output.Add(tm);
  330. this.stateTask = StateEnum.ERROR;
  331. messageError = null;
  332. if(filename.Contains("Log-E:")){
  333. //Console.WriteLine("YES LOGE");
  334. if (ErrorWrite!=null){
  335. TaskMessage tm2 = new TaskMessage(error,"","");
  336. Console.WriteLine("YES ProcessErrorWrite");
  337. ErrorWrite(this,this.Name,this.StateTask.ToString(),tm2);
  338. }
  339. } //else {
  340. if (LogWrite!=null){
  341. LogWrite(this,this.Name,this.StateTask.ToString(),tm);
  342. }
  343. //}
  344. } catch {
  345. }
  346. return tm;
  347. }
  348. public string Name {
  349. get {
  350. return "Running";
  351. }
  352. }
  353. public StateEnum StateTask {
  354. get {
  355. return stateTask;
  356. }
  357. }
  358. public List<TaskMessage> Output {
  359. get {
  360. return output;
  361. }
  362. }
  363. public ITask ChildrenTask {
  364. get {
  365. return null;
  366. }
  367. }
  368. #endregion
  369. }
  370. }