public static ProcessStartInfo CreateMtouchSimStartInfo (IPhoneExecutionCommand cmd, bool logSimOutput, 
		                                                         IPhoneSimulatorTarget forceTarget)
		{
			string mtouchPath = cmd.Runtime.GetToolPath (cmd.Framework, "mtouch");
			if (string.IsNullOrEmpty (mtouchPath))
				throw new InvalidOperationException ("Cannot execute iPhone application. mtouch tool is missing.");
			
			var outLog = cmd.OutputLogPath;
			var errLog = cmd.ErrorLogPath;
			try {
				if (File.Exists (errLog))
				    File.Delete (errLog);
				if (File.Exists (outLog))
				    File.Delete (outLog);
			} catch (IOException) {}
			
			var sb = new StringBuilder ();
			sb.AppendFormat ("-launchsim='{0}'", cmd.AppPath);
			if (logSimOutput) 
				sb.AppendFormat (" -stderr='{0}' -stdout='{1}'", errLog, outLog);
			
			if (forceTarget != null) {
				sb.AppendFormat (" -sdk='{0}'", forceTarget.Version.ToString ());
				if (forceTarget.Device == TargetDevice.IPad)
					sb.AppendFormat (" -device=2");
			}
			
			var psi = new ProcessStartInfo (mtouchPath, sb.ToString ()) {
				WorkingDirectory = cmd.LogDirectory,
				UseShellExecute = false
			};
			
			return psi;
		}
        public static ProcessStartInfo CreateMtouchSimStartInfo(IPhoneExecutionCommand cmd, bool logSimOutput,
                                                                IPhoneSimulatorTarget forceTarget)
        {
            string mtouchPath = cmd.Runtime.GetToolPath(cmd.Framework, "mtouch");

            if (string.IsNullOrEmpty(mtouchPath))
            {
                throw new InvalidOperationException("Cannot execute iPhone application. mtouch tool is missing.");
            }

            var outLog = cmd.OutputLogPath;
            var errLog = cmd.ErrorLogPath;

            try {
                if (File.Exists(errLog))
                {
                    File.Delete(errLog);
                }
                if (File.Exists(outLog))
                {
                    File.Delete(outLog);
                }
            } catch (IOException) {}

            var sb = new StringBuilder();

            sb.AppendFormat("-launchsim='{0}'", cmd.AppPath);
            if (logSimOutput)
            {
                sb.AppendFormat(" -stderr='{0}' -stdout='{1}'", errLog, outLog);
            }

            if (forceTarget != null)
            {
                sb.AppendFormat(" -sdk='{0}'", forceTarget.Version.ToString());
                if (forceTarget.Device == TargetDevice.IPad)
                {
                    sb.AppendFormat(" -device=2");
                }
            }

            var psi = new ProcessStartInfo(mtouchPath, sb.ToString())
            {
                WorkingDirectory = cmd.LogDirectory,
                UseShellExecute  = false
            };

            return(psi);
        }
		//FIXME: hook up the app's stdin and stdout, and mtouch's stdin and stdout
		void StartSimulatorProcess (IPhoneExecutionCommand cmd)
		{
			var psi = IPhoneExecutionHandler.CreateMtouchSimStartInfo (cmd, false);
			psi.RedirectStandardInput = true;
			simProcess = System.Diagnostics.Process.Start (psi);
			
			simProcess.Exited += delegate {
				EndSession ();
				simProcess = null;
			};
			
			TargetExited += delegate {
				EndSimProcess ();
			};
		}
		public static ProcessStartInfo CreateMtouchSimStartInfo (IPhoneExecutionCommand cmd, bool logSimOutput, 
		                                                         IPhoneSimulatorTarget forceTarget)
		{
			string mtouchPath = cmd.Runtime.GetToolPath (cmd.Framework, "mtouch");
			if (string.IsNullOrEmpty (mtouchPath))
				throw new InvalidOperationException ("Cannot execute iPhone application. mtouch tool is missing.");
			
			var outLog = cmd.OutputLogPath;
			var errLog = cmd.ErrorLogPath;
			try {
				if (File.Exists (errLog))
				    File.Delete (errLog);
				if (File.Exists (outLog))
				    File.Delete (outLog);
			} catch (IOException) {}
			
			var cb = new ProcessArgumentBuilder ();
			cb.AddQuotedFormat ("-launchsim={0}", cmd.AppPath);
			if (logSimOutput) {
				cb.AddQuotedFormat ("-stderr={0}", errLog);
				cb.AddQuotedFormat ("-stdout={0}", outLog);
			}
			
			if (forceTarget != null) {
				var version = forceTarget.Version;
				
				if (!version.IsUseDefault && !IPhoneFramework.SdkIsInstalled (version, true)) {
					version = IPhoneFramework.GetClosestInstalledSdk (version, true);
					LoggingService.LogWarning ("iOS SDK '{0}' not installed, falling back to simulator '{1}'",
						forceTarget.Version, version);
				}
				
				if (!version.IsUseDefault)
					cb.AddQuotedFormat ("-sdk={0}", forceTarget.Version);
				
				if (forceTarget.Device == TargetDevice.IPad)
					cb.Add ("-device=2");
			}
			
			var psi = new ProcessStartInfo (mtouchPath, cb.ToString ()) {
				WorkingDirectory = cmd.LogDirectory,
				UseShellExecute = false
			};
			
			return psi;
		}
        public IProcessAsyncOperation Execute(ExecutionCommand command, IConsole console)
        {
            IPhoneExecutionCommand cmd = (IPhoneExecutionCommand)command;

            if (!cmd.Simulator)
            {
                if (IPhoneSettings.ShowStartOnDeviceMessage)
                {
                    Gtk.Application.Invoke(delegate {
                        TellUserToStartApplication();
                    });
                }
                return(NullProcessAsyncOperation.Success);
            }

            var psi = CreateMtouchSimStartInfo(cmd, true, SimulatorTarget ?? cmd.SimulatorTarget);;

            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;
            psi.RedirectStandardInput  = true;

            LineInterceptingTextWriter intercepter = null;

            intercepter = new LineInterceptingTextWriter(null, delegate {
                if (intercepter.GetLine().StartsWith("Application launched"))
                {
                    IPhoneUtility.MakeSimulatorGrabFocus();
                    intercepter.FinishedIntercepting = true;
                }
                else if (intercepter.LineCount > 20)
                {
                    intercepter.FinishedIntercepting = true;
                }
            });

            var outTail = new Tail(cmd.OutputLogPath, console.Out.Write);
            var errTail = new Tail(cmd.ErrorLogPath, console.Error.Write);

            outTail.Start();
            errTail.Start();

            var mtouchProcess = Runtime.ProcessService.StartProcess(psi, intercepter, console.Log, null);

            return(new IPhoneProcess(mtouchProcess, outTail, errTail));
        }
 public static ProcessStartInfo CreateMtouchSimStartInfo(IPhoneExecutionCommand cmd, bool logSimOutput)
 {
     return(CreateMtouchSimStartInfo(cmd, logSimOutput, cmd.SimulatorTarget));
 }
		public static ProcessStartInfo CreateMtouchSimStartInfo (IPhoneExecutionCommand cmd, bool logSimOutput)
		{
			return CreateMtouchSimStartInfo (cmd, logSimOutput, cmd.SimulatorTarget);
		}
		public IPhoneDebuggerStartInfo (IPAddress address, int debugPort, int outputPort, IPhoneExecutionCommand cmd)
			: base (cmd.AppPath.FileNameWithoutExtension, address, debugPort, outputPort)
		{
			ExecutionCommand = cmd;
		}
        public static ProcessStartInfo CreateMtouchSimStartInfo(IPhoneExecutionCommand cmd, bool logSimOutput,
                                                                IPhoneSimulatorTarget forceTarget)
        {
            string mtouchPath = cmd.Runtime.GetToolPath(cmd.Framework, "mtouch");

            if (string.IsNullOrEmpty(mtouchPath))
            {
                throw new InvalidOperationException("Cannot execute iPhone application. mtouch tool is missing.");
            }

            var outLog = cmd.OutputLogPath;
            var errLog = cmd.ErrorLogPath;

            try {
                if (File.Exists(errLog))
                {
                    File.Delete(errLog);
                }
                if (File.Exists(outLog))
                {
                    File.Delete(outLog);
                }
            } catch (IOException) {}

            var cb = new ProcessArgumentBuilder();

            cb.AddQuotedFormat("-launchsim={0}", cmd.AppPath);
            if (logSimOutput)
            {
                cb.AddQuotedFormat("-stderr={0}", errLog);
                cb.AddQuotedFormat("-stdout={0}", outLog);
            }

            if (forceTarget != null)
            {
                var version = forceTarget.Version;

                if (!version.IsUseDefault && !IPhoneFramework.SdkIsInstalled(version, true))
                {
                    version = IPhoneFramework.GetClosestInstalledSdk(version, true);
                    LoggingService.LogWarning("iOS SDK '{0}' not installed, falling back to simulator '{1}'",
                                              forceTarget.Version, version);
                }

                if (!version.IsUseDefault)
                {
                    cb.AddQuotedFormat("-sdk={0}", forceTarget.Version);
                }

                if (forceTarget.Device == TargetDevice.IPad)
                {
                    cb.Add("-device=2");
                }
            }

            var psi = new ProcessStartInfo(mtouchPath, cb.ToString())
            {
                WorkingDirectory = cmd.LogDirectory,
                UseShellExecute  = false
            };

            return(psi);
        }