summaryrefslogtreecommitdiff
path: root/modules/mono/editor/GodotSharpTools/Editor/MonoDevelopInstance.cs
blob: fba4a8f65c4fd77b239ffa131a42d021a9e6e0f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace GodotSharpTools.Editor
{
    public class MonoDevelopInstance
    {
        public enum EditorId
        {
            MonoDevelop = 0,
            VisualStudioForMac = 1
        }

        readonly string solutionFile;
        readonly EditorId editorId;

        Process process;

        public void Execute(string[] files)
        {
            bool newWindow = process == null || process.HasExited;

            List<string> args = new List<string>();

            string command;

            if (Utils.OS.IsOSX())
            {
                string bundleId = codeEditorBundleIds[editorId];

                if (IsApplicationBundleInstalled(bundleId))
                {
                    command = "open";

                    args.Add("-b");
                    args.Add(bundleId);

                    // The 'open' process must wait until the application finishes
                    if (newWindow)
                        args.Add("--wait-apps");

                    args.Add("--args");
                }
                else
                {
                    command = codeEditorPaths[editorId];
                }
            }
            else
            {
                command = codeEditorPaths[editorId];
            }

            args.Add("--ipc-tcp");

            if (newWindow)
                args.Add("\"" + Path.GetFullPath(solutionFile) + "\"");

            foreach (var file in files)
            {
                int semicolonIndex = file.IndexOf(';');

                string filePath = semicolonIndex < 0 ? file : file.Substring(0, semicolonIndex);
                string cursor = semicolonIndex < 0 ? string.Empty : file.Substring(semicolonIndex);

                args.Add("\"" + Path.GetFullPath(filePath.NormalizePath()) + cursor + "\"");
            }

            if (newWindow)
            {
                process = Process.Start(new ProcessStartInfo()
                {
                    FileName = command,
                    Arguments = string.Join(" ", args),
                    UseShellExecute = false
                });
            }
            else
            {
                Process.Start(new ProcessStartInfo()
                {
                    FileName = command,
                    Arguments = string.Join(" ", args),
                    UseShellExecute = false
                });
            }
        }

        public MonoDevelopInstance(string solutionFile, EditorId editorId)
        {
            if (editorId == EditorId.VisualStudioForMac && !Utils.OS.IsOSX())
                throw new InvalidOperationException($"{nameof(EditorId.VisualStudioForMac)} not supported on this platform");

            this.solutionFile = solutionFile;
            this.editorId = editorId;
        }

        [MethodImpl(MethodImplOptions.InternalCall)]
        private extern static bool IsApplicationBundleInstalled(string bundleId);

        static readonly IReadOnlyDictionary<EditorId, string> codeEditorPaths;
        static readonly IReadOnlyDictionary<EditorId, string> codeEditorBundleIds;

        static MonoDevelopInstance()
        {
            if (Utils.OS.IsOSX())
            {
                codeEditorPaths = new Dictionary<EditorId, string>
                {
                    // Rely on PATH
                    { EditorId.MonoDevelop, "monodevelop" },
                    { EditorId.VisualStudioForMac, "VisualStudio" }
                };
                codeEditorBundleIds = new Dictionary<EditorId, string>
                {
                    // TODO EditorId.MonoDevelop
                    { EditorId.VisualStudioForMac, "com.microsoft.visual-studio" }
                };
            }
            else if (Utils.OS.IsWindows())
            {
                codeEditorPaths = new Dictionary<EditorId, string>
                {
                    // XamarinStudio is no longer a thing, and the latest version is quite old
                    // MonoDevelop is available from source only on Windows. The recommendation
                    // is to use Visual Studio instead. Since there are no official builds, we
                    // will rely on custom MonoDevelop builds being added to PATH.
                    { EditorId.MonoDevelop, "MonoDevelop.exe" }
                };
            }
            else if (Utils.OS.IsUnix())
            {
                codeEditorPaths = new Dictionary<EditorId, string>
                {
                    // Rely on PATH
                    { EditorId.MonoDevelop, "monodevelop" }
                };
            }
        }
    }
}