private string Build()
        {
            ClearStates();
            var report = TrailBuild.Build();

            return(report != null && report.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded ? report.summary.outputPath : "");
        }
        private void UploadLatestBuild()
        {
            ClearStates();
            var hasBuild = HasBuild();

            if (!hasBuild)
            {
                return;
            }
            if (uploadBuildRequest != null && !uploadBuildRequest.IsComplete)
            {
                SDK.Log(LogLevel.Error, "Trail CLI", "Already uploading build, cancel build before attempting to upload a new build!");
                return;
            }
            uploadBuildRequest = TrailBuild.Upload();
            uploadBuildRequest.AddCallback((success, request) =>
            {
                if (success)
                {
                    EditorApplication.delayCall += () =>
                                                   Application.OpenURL(string.Format(MANAGE_DEV_AREA_URL_GAME, cache.selectedGameId));
                }
            });
        }
        void DrawLatestBuild()
        {
            var hasBuild = HasBuild();

            GUILayout.BeginVertical(EditorStyles.helpBox);
            GUILayout.Space(4f);
            GUILayout.BeginHorizontal();
            GUILayout.Label("LATEST BUILD");
            GUILayout.FlexibleSpace();
            if (hasBuild)
            {
                var time = BuildTime();
                GUILayout.Label(string.Format("{0} {1}", time.ToShortDateString(), time.ToShortTimeString()), uploadBuildStyle); // Date
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();

            var iconArea = GUILayoutUtility.GetRect(16, 14);

            iconArea.width -= 4;
            iconArea.x     += 4;
            iconArea.y     += 2;
            GUI.DrawTexture(iconArea, hasBuild ? buildIcon : buildNotFoundIcon);
            if (hasBuild)
            {
                GUILayout.Label(TrailBuild.BuildLocationRelativePath, buildPathStyle);
                var pathArea = GUILayoutUtility.GetLastRect();
                var ev       = Event.current;
                if (ev.type == EventType.MouseDown && ev.button == 0 && pathArea.Contains(ev.mousePosition) && TrailBuild.HasBuild() && System.IO.Directory.Exists(TrailBuild.BuildLocationPath))
                {
                    try
                    {
                        EditorApplication.delayCall += () => System.Diagnostics.Process.Start(TrailBuild.BuildLocationPath);
                    }
                    catch (System.Exception)
                    {
                    }
                }

                GUILayout.FlexibleSpace();

                if (runBuildRequest != null)
                {
                    if (GUILayout.Button("Stop Running"))
                    {
                        EditorApplication.delayCall += () =>
                        {
                            runBuildRequest.Cancel();
                            runBuildRequest = null;
                        };
                    }
                }
                else if (uploadBuildRequest == null || uploadBuildRequest.IsComplete)
                {
                    if (GUILayout.Button("Run"))
                    {
                        EditorApplication.delayCall += RunLatestBuild;
                    }
                    if (GUILayout.Button("Upload"))
                    {
                        EditorApplication.delayCall += () => { UploadLatestBuild(); Repaint(); };
                    }
                }
                else
                {
                    if (GUILayout.Button("Cancel"))
                    {
                        EditorApplication.delayCall += uploadBuildRequest.Cancel;
                    }
                }
            }
            else
            {
                GUILayout.Label("No Trail build found", EditorStyles.boldLabel);
                GUILayout.FlexibleSpace();
            }

            GUILayout.EndHorizontal();

            if (runBuildRequest != null)
            {
                GUILayout.Space(8f);
                GUILayout.BeginHorizontal();
                GUILayout.Space(4f);
                var icon = GUILayoutUtility.GetRect(14f, 14f);
                icon.y += 2f;
                // Draw Spinner

                DrawLoadingIcon(new Rect(icon.position, new Vector2(13, 13)), loadingIcon, 4, 4, 1f);

                // RUnning build
                GUILayout.Label("Running build...", uploadBuildStyle);
                GUILayout.FlexibleSpace();

                if (GUILayout.Button("Open in browser"))
                {
                    Application.OpenURL(string.Format(RUN_BUILD_URL, CLI.DevServerRoomId));
                }

                GUILayout.EndHorizontal();

                if (runBuildRequest.IsComplete || runBuildRequest.IsCanceled)
                {
                    runBuildRequest = null;
                }
                else
                {
                    Repaint();
                }
            }
            if (uploadBuildRequest != null)
            {
                //Uploading Requests
                GUILayout.Space(8f);
                GUILayout.BeginHorizontal();
                GUILayout.Space(4f);
                if (uploadBuildRequest.IsComplete)
                {
                    var icon = GUILayoutUtility.GetRect(14f, 14f, GUILayout.Width(14f));
                    icon.y += 2f;
                    if (!string.IsNullOrEmpty(uploadBuildRequest.ErrorMessage))
                    {
                        GUI.DrawTexture(icon, errorIcon);
                        GUILayout.Label("Failed to upload to Trail - " + uploadBuildRequest.ErrorMessage, uploadBuildStyle);
                    }
                    else
                    {
                        if (!TrailBuild.IsUploaded)
                        {
                            // To clear if new build has been done and upload request still exists.
                            EditorApplication.delayCall += () => uploadBuildRequest = null;
                        }
                        GUI.DrawTexture(icon, checkmarkIcon);
                        GUILayout.Label("Uploaded to Trail", uploadBuildStyle);
                        GUILayout.FlexibleSpace();
                        var time = TrailBuild.UploadTime;
                        GUILayout.Label(string.Format("{0} {1}", time.ToShortDateString(), time.ToShortTimeString()), uploadBuildStyle);
                    }
                }
                else
                {
                    // Currently uploading
                    var icon = GUILayoutUtility.GetRect(14f, 14f, GUILayout.Width(14f));
                    icon.y += 2f;
                    DrawLoadingIcon(icon, this.loadingIcon, 4, 4, 1f);
                    GUILayout.Label(uploadBuildRequest.Progress >= 1f ? "Processing..." : "Uploading build...", uploadBuildStyle, GUILayout.Width(100f));
                    // TODO: Implement upload speed.
                    //  GUILayout.Label(string.Format("{0:0.0} Mb/s", uploadBuildRequest.MbPerSecond), uploadBuildStyle, GUILayout.Width(50f));
                    GUILayout.FlexibleSpace();

                    var loadingBarArea = GUILayoutUtility.GetRect(40f, 200f, 4f, 12f);
                    loadingBarArea.height = 4f;
                    loadingBarArea.y     += 8f;
                    loadingBarArea.x     += 20f;
                    loadingBarArea.width -= 24f;

                    GUI.DrawTexture(loadingBarArea, uploadBuildBackgroundTexture);
                    loadingBarArea.width *= Mathf.Abs(uploadBuildRequest.Progress);
                    GUI.DrawTexture(loadingBarArea, trailPurpleTexture);

                    Repaint();
                }
                GUILayout.EndHorizontal();
            }
            else if (hasBuild && TrailBuild.IsUploaded)
            {
                GUILayout.Space(8f);
                GUILayout.BeginHorizontal();
                GUILayout.Space(4f);
                var icon = GUILayoutUtility.GetRect(14f, 14f);
                icon.y += 2f;
                GUI.DrawTexture(icon, checkmarkIcon);
                GUILayout.Label("Uploaded to Trail", uploadBuildStyle);
                GUILayout.FlexibleSpace();
                var time = TrailBuild.UploadTime;
                GUILayout.Label(string.Format("{0} {1}", time.ToShortDateString(), time.ToShortTimeString()), uploadBuildStyle);
                GUILayout.EndHorizontal();
            }

            GUILayout.Space(8);
            GUILayout.EndVertical();
        }
 private bool HasBuild()
 {
     ClearStates();
     return(TrailBuild.HasBuild());
 }