Skip to content

Commit cea06de

Browse files
committed
refactor: extract common platform detection logic into PlatformDetectorBase class
1 parent f6441a0 commit cea06de

File tree

4 files changed

+188
-321
lines changed

4 files changed

+188
-321
lines changed

UnityMcpBridge/Editor/Dependencies/PlatformDetectors/LinuxPlatformDetector.cs

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace MCPForUnity.Editor.Dependencies.PlatformDetectors
1010
/// <summary>
1111
/// Linux-specific dependency detection
1212
/// </summary>
13-
public class LinuxPlatformDetector : IPlatformDetector
13+
public class LinuxPlatformDetector : PlatformDetectorBase
1414
{
15-
public string PlatformName => "Linux";
15+
public override string PlatformName => "Linux";
1616

17-
public bool CanDetect => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
17+
public override bool CanDetect => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
1818

1919
public DependencyStatus DetectPython()
2020
{
@@ -73,89 +73,17 @@ public DependencyStatus DetectPython()
7373
return status;
7474
}
7575

76-
public DependencyStatus DetectUV()
76+
public override string GetPythonInstallUrl()
7777
{
78-
var status = new DependencyStatus("UV Package Manager", isRequired: true)
79-
{
80-
InstallationHint = GetUVInstallUrl()
81-
};
82-
83-
try
84-
{
85-
// Use existing UV detection from ServerInstaller
86-
string uvPath = ServerInstaller.FindUvPath();
87-
if (!string.IsNullOrEmpty(uvPath))
88-
{
89-
if (TryValidateUV(uvPath, out string version))
90-
{
91-
status.IsAvailable = true;
92-
status.Version = version;
93-
status.Path = uvPath;
94-
status.Details = $"Found UV {version} at {uvPath}";
95-
return status;
96-
}
97-
}
98-
99-
status.ErrorMessage = "UV package manager not found. Please install UV.";
100-
status.Details = "UV is required for managing Python dependencies.";
101-
}
102-
catch (Exception ex)
103-
{
104-
status.ErrorMessage = $"Error detecting UV: {ex.Message}";
105-
}
106-
107-
return status;
78+
return "https://www.python.org/downloads/source/";
10879
}
10980

110-
public DependencyStatus DetectMCPServer()
81+
public override string GetUVInstallUrl()
11182
{
112-
var status = new DependencyStatus("MCP Server", isRequired: false);
113-
114-
try
115-
{
116-
// Check if server is installed
117-
string serverPath = ServerInstaller.GetServerPath();
118-
string serverPy = Path.Combine(serverPath, "server.py");
119-
120-
if (File.Exists(serverPy))
121-
{
122-
status.IsAvailable = true;
123-
status.Path = serverPath;
124-
125-
// Try to get version
126-
string versionFile = Path.Combine(serverPath, "server_version.txt");
127-
if (File.Exists(versionFile))
128-
{
129-
status.Version = File.ReadAllText(versionFile).Trim();
130-
}
131-
132-
status.Details = $"MCP Server found at {serverPath}";
133-
}
134-
else
135-
{
136-
// Check for embedded server
137-
if (ServerPathResolver.TryFindEmbeddedServerSource(out string embeddedPath))
138-
{
139-
status.IsAvailable = true;
140-
status.Path = embeddedPath;
141-
status.Details = "MCP Server available (embedded in package)";
142-
}
143-
else
144-
{
145-
status.ErrorMessage = "MCP Server not found";
146-
status.Details = "Server will be installed automatically when needed";
147-
}
148-
}
149-
}
150-
catch (Exception ex)
151-
{
152-
status.ErrorMessage = $"Error detecting MCP Server: {ex.Message}";
153-
}
154-
155-
return status;
83+
return "https://docs.astral.sh/uv/getting-started/installation/#linux";
15684
}
15785

158-
public string GetInstallationRecommendations()
86+
public override string GetInstallationRecommendations()
15987
{
16088
return @"Linux Installation Recommendations:
16189
@@ -174,16 +102,6 @@ public string GetInstallationRecommendations()
174102
Note: Make sure ~/.local/bin is in your PATH for user-local installations.";
175103
}
176104

177-
public string GetPythonInstallUrl()
178-
{
179-
return "https://www.python.org/downloads/source/";
180-
}
181-
182-
public string GetUVInstallUrl()
183-
{
184-
return "https://docs.astral.sh/uv/getting-started/installation/#linux";
185-
}
186-
187105
private bool TryValidatePython(string pythonPath, out string version, out string fullPath)
188106
{
189107
version = null;
@@ -329,23 +247,7 @@ private bool TryFindInPath(string executable, out string fullPath)
329247

330248
private bool TryParseVersion(string version, out int major, out int minor)
331249
{
332-
major = 0;
333-
minor = 0;
334-
335-
try
336-
{
337-
var parts = version.Split('.');
338-
if (parts.Length >= 2)
339-
{
340-
return int.TryParse(parts[0], out major) && int.TryParse(parts[1], out minor);
341-
}
342-
}
343-
catch
344-
{
345-
// Ignore parsing errors
346-
}
347-
348-
return false;
250+
return base.TryParseVersion(version, out major, out minor);
349251
}
350252
}
351253
}

UnityMcpBridge/Editor/Dependencies/PlatformDetectors/MacOSPlatformDetector.cs

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace MCPForUnity.Editor.Dependencies.PlatformDetectors
1010
/// <summary>
1111
/// macOS-specific dependency detection
1212
/// </summary>
13-
public class MacOSPlatformDetector : IPlatformDetector
13+
public class MacOSPlatformDetector : PlatformDetectorBase
1414
{
15-
public string PlatformName => "macOS";
15+
public override string PlatformName => "macOS";
1616

17-
public bool CanDetect => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
17+
public override bool CanDetect => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
1818

1919
public DependencyStatus DetectPython()
2020
{
@@ -76,89 +76,17 @@ public DependencyStatus DetectPython()
7676
return status;
7777
}
7878

79-
public DependencyStatus DetectUV()
79+
public override string GetPythonInstallUrl()
8080
{
81-
var status = new DependencyStatus("UV Package Manager", isRequired: true)
82-
{
83-
InstallationHint = GetUVInstallUrl()
84-
};
85-
86-
try
87-
{
88-
// Use existing UV detection from ServerInstaller
89-
string uvPath = ServerInstaller.FindUvPath();
90-
if (!string.IsNullOrEmpty(uvPath))
91-
{
92-
if (TryValidateUV(uvPath, out string version))
93-
{
94-
status.IsAvailable = true;
95-
status.Version = version;
96-
status.Path = uvPath;
97-
status.Details = $"Found UV {version} at {uvPath}";
98-
return status;
99-
}
100-
}
101-
102-
status.ErrorMessage = "UV package manager not found. Please install UV.";
103-
status.Details = "UV is required for managing Python dependencies.";
104-
}
105-
catch (Exception ex)
106-
{
107-
status.ErrorMessage = $"Error detecting UV: {ex.Message}";
108-
}
109-
110-
return status;
81+
return "https://www.python.org/downloads/macos/";
11182
}
11283

113-
public DependencyStatus DetectMCPServer()
84+
public override string GetUVInstallUrl()
11485
{
115-
var status = new DependencyStatus("MCP Server", isRequired: false);
116-
117-
try
118-
{
119-
// Check if server is installed
120-
string serverPath = ServerInstaller.GetServerPath();
121-
string serverPy = Path.Combine(serverPath, "server.py");
122-
123-
if (File.Exists(serverPy))
124-
{
125-
status.IsAvailable = true;
126-
status.Path = serverPath;
127-
128-
// Try to get version
129-
string versionFile = Path.Combine(serverPath, "server_version.txt");
130-
if (File.Exists(versionFile))
131-
{
132-
status.Version = File.ReadAllText(versionFile).Trim();
133-
}
134-
135-
status.Details = $"MCP Server found at {serverPath}";
136-
}
137-
else
138-
{
139-
// Check for embedded server
140-
if (ServerPathResolver.TryFindEmbeddedServerSource(out string embeddedPath))
141-
{
142-
status.IsAvailable = true;
143-
status.Path = embeddedPath;
144-
status.Details = "MCP Server available (embedded in package)";
145-
}
146-
else
147-
{
148-
status.ErrorMessage = "MCP Server not found";
149-
status.Details = "Server will be installed automatically when needed";
150-
}
151-
}
152-
}
153-
catch (Exception ex)
154-
{
155-
status.ErrorMessage = $"Error detecting MCP Server: {ex.Message}";
156-
}
157-
158-
return status;
86+
return "https://docs.astral.sh/uv/getting-started/installation/#macos";
15987
}
16088

161-
public string GetInstallationRecommendations()
89+
public override string GetInstallationRecommendations()
16290
{
16391
return @"macOS Installation Recommendations:
16492
@@ -175,16 +103,6 @@ public string GetInstallationRecommendations()
175103
Note: If using Homebrew, make sure /opt/homebrew/bin is in your PATH.";
176104
}
177105

178-
public string GetPythonInstallUrl()
179-
{
180-
return "https://www.python.org/downloads/macos/";
181-
}
182-
183-
public string GetUVInstallUrl()
184-
{
185-
return "https://docs.astral.sh/uv/getting-started/installation/#macos";
186-
}
187-
188106
private bool TryValidatePython(string pythonPath, out string version, out string fullPath)
189107
{
190108
version = null;
@@ -329,23 +247,7 @@ private bool TryFindInPath(string executable, out string fullPath)
329247

330248
private bool TryParseVersion(string version, out int major, out int minor)
331249
{
332-
major = 0;
333-
minor = 0;
334-
335-
try
336-
{
337-
var parts = version.Split('.');
338-
if (parts.Length >= 2)
339-
{
340-
return int.TryParse(parts[0], out major) && int.TryParse(parts[1], out minor);
341-
}
342-
}
343-
catch
344-
{
345-
// Ignore parsing errors
346-
}
347-
348-
return false;
250+
return base.TryParseVersion(version, out major, out minor);
349251
}
350252
}
351253
}

0 commit comments

Comments
 (0)