Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int deviceIndex = 0;

            string iconFilePath   = "";
            string xapFilePath    = "";
            Guid   appID          = Guid.Empty;
            bool   uninstallFirst = args.Contains("-uninstall");
            bool   awaitAppClose  = args.Contains("-wait");

            string root = Directory.GetCurrentDirectory();

            if (args.Length < 1)
            {
                Usage();
                ReadWait();
                return;
            }
            else if (args.Contains("-devices"))
            {
                ListDevices();
                ReadWait();
                return;
            }
            else if (args.Length > 1 && args[1].StartsWith("-d:"))
            {
                deviceIndex = int.Parse(args[1].Substring(3));
            }

            if (Directory.Exists(args[0]))
            {
                var info = new DirectoryInfo(args[0]);
                root = info.FullName;

                try
                {
                    xapFilePath = Directory.GetFiles(root + @"\Bin\Debug", "*.xap").FirstOrDefault();
                }
                catch (DirectoryNotFoundException)
                {
                    try
                    {
                        xapFilePath = Directory.GetFiles(root + @"\Bin\Release", "*.xap").FirstOrDefault();
                    }
                    catch (DirectoryNotFoundException)
                    {
                        Log(string.Format("Error: could not find project build directoy in {0}", root), true);
                        Log("make sure your app has been successfully built before deploying.", true);
                    }
                }
            }

            if (File.Exists(args[0]))
            {
                var info = new FileInfo(args[0]);
                if (info.Extension == ".xap")
                {
                    root        = info.DirectoryName;
                    xapFilePath = info.FullName;
                }
            }

            appID = ReadAppId(root);
            if (appID == Guid.Empty)
            {
                return;    // Logging of errors is done in ReadAppId
            }

            if (File.Exists(root + @"\ApplicationIcon.png"))
            {
                iconFilePath = root + @"\ApplicationIcon.png";
            }
            else
            {
                Log(string.Format("Error: could not find application icon at {0}", root + @"\ApplicationIcon.png"), true);
                ReadWait();
                return;
            }

            if (string.IsNullOrEmpty(xapFilePath))
            {
                Log(string.Format("Error: could not find application .xap in folder {0}", root), true);
                ReadWait();
                return;
            }

            ConnectableDevice deviceConn = GetDeviceAtIndex(deviceIndex);

            Log("Connecting to device :: " + deviceConn.Id + " : " + deviceConn.Name);
            try
            {
                IDevice            device = deviceConn.Connect();
                IRemoteApplication app    = null;
                if (device.IsApplicationInstalled(appID))
                {
                    app = device.GetApplication(appID);
                    if (uninstallFirst)
                    {
                        Log("Uninstalling app on " + deviceConn.Name);
                        app.Uninstall();
                        Log("Installing app on " + deviceConn.Name);
                        app = device.InstallApplication(appID, appID, "NormalApp", iconFilePath, xapFilePath);
                    }
                    else
                    {
                        Log("Updating app on " + deviceConn.Name);
                        app.UpdateApplication("NormalApp", iconFilePath, xapFilePath);
                    }
                }
                else
                {
                    Log("Installing app on " + deviceConn.Name);
                    app = device.InstallApplication(appID, appID, "NormalApp", iconFilePath, xapFilePath);
                }

                Log("Launching app on " + deviceConn.Name);
                app.Launch();

                if (awaitAppClose)
                {
                    // wait for the app to launch
                    Thread.Sleep(4000);

                    bool isExiting = false;

                    string tempFileName = Path.GetTempFileName();

                    try
                    {
                        IRemoteIsolatedStorageFile isoFile = app.GetIsolatedStore();
                        int index = 0;
                        while (!isExiting) //app.IsRunning()) // not implemented ... wtf?
                        {
                            char[] buffer = new char[1000];

                            isoFile.ReceiveFile((object)Path.DirectorySeparatorChar + "debugOutput.txt", tempFileName, true);
                            using (StreamReader reader = System.IO.File.OpenText(tempFileName))
                            {
                                try
                                {
                                    int newLinesRead = 0;
                                    for (int lineNum = 0; ; lineNum++)
                                    {
                                        if (reader.Peek() > -1)
                                        {
                                            string str = reader.ReadLine();
                                            if (lineNum >= index)
                                            {
                                                newLinesRead++;
                                                if (str == "EXIT")
                                                {
                                                    isExiting = true;
                                                }
                                                Log(str);
                                            }
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    index += newLinesRead;
                                }
                                catch (Exception)
                                {
                                    // at end of stream most likely, no worries, ... move along.
                                }
                            }

                            Thread.Sleep(1000);
                        }

                        System.IO.File.Delete(tempFileName);
                    }
                    catch (Exception ex)
                    {
                        Log(ex.Message);
                    }
                }

                // To Stop :
                //app.TerminateRunningInstances();

                device.Disconnect();

                ReadWait();
            }
            catch (Exception ex)
            {
                Log("Error :: " + ex.Message, true);
            }
        }