-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(editor): add GitHub-based skill sync window with safe incremental mirroring #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6a036b
Add Unity MCP skill sync installer
BaronCyrus 49901a3
fix namespace
BaronCyrus a031b57
fix: harden skill installer sync safety (由codex生成)
BaronCyrus d1eca2e
meta update
BaronCyrus 5f833dc
fix: freeze sync inputs and validate install path (由codex生成)
BaronCyrus b5d8e2a
Update to incorporate the skill installation into current config
Scriptwonder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using UnityEditor; | ||
| using UnityEngine; | ||
|
|
||
| namespace MCPForUnity.Editor.Setup | ||
| { | ||
| public class McpForUnitySkillInstaller : EditorWindow | ||
| { | ||
| private const string RepoUrlKey = "UnityMcpSkillSync.RepoUrl"; | ||
| private const string BranchKey = "UnityMcpSkillSync.Branch"; | ||
| private const string CliKey = "UnityMcpSkillSync.Cli"; | ||
| private const string InstallDirKey = "UnityMcpSkillSync.InstallDir"; | ||
| private const string CodexCli = "codex"; | ||
| private const string ClaudeCli = "claude"; | ||
| private static readonly string[] BranchOptions = { "beta", "main" }; | ||
| private static readonly string[] CliOptions = { CodexCli, ClaudeCli }; | ||
|
|
||
| private string _repoUrl; | ||
| private string _targetBranch; | ||
| private string _cliType; | ||
| private string _installDir; | ||
| private Vector2 _scroll; | ||
| private volatile bool _isRunning; | ||
| private readonly ConcurrentQueue<string> _pendingLogs = new(); | ||
| private readonly StringBuilder _logBuilder = new(4096); | ||
|
|
||
| [MenuItem("Window/MCP For Unity/Install(Sync) MCP Skill")] | ||
| public static void OpenWindow() | ||
| { | ||
| GetWindow<McpForUnitySkillInstaller>("Unity MCP Skill Install(Sync)"); | ||
| } | ||
|
|
||
| private void OnEnable() | ||
| { | ||
| var userHome = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
| _repoUrl = EditorPrefs.GetString(RepoUrlKey, "https://github.com/CoplayDev/unity-mcp"); | ||
| _targetBranch = EditorPrefs.GetString(BranchKey, "beta"); | ||
| if (!BranchOptions.Contains(_targetBranch)) | ||
| { | ||
| _targetBranch = "beta"; | ||
| } | ||
| _cliType = EditorPrefs.GetString(CliKey, CodexCli); | ||
| if (!CliOptions.Contains(_cliType)) | ||
| { | ||
| _cliType = CodexCli; | ||
| } | ||
| _installDir = EditorPrefs.GetString(InstallDirKey, GetDefaultInstallDir(userHome, _cliType)); | ||
| EditorApplication.update += OnEditorUpdate; | ||
| } | ||
|
|
||
| private void OnDisable() | ||
| { | ||
| EditorApplication.update -= OnEditorUpdate; | ||
| EditorPrefs.SetString(RepoUrlKey, _repoUrl); | ||
| EditorPrefs.SetString(BranchKey, _targetBranch); | ||
| EditorPrefs.SetString(CliKey, _cliType); | ||
| EditorPrefs.SetString(InstallDirKey, _installDir); | ||
| } | ||
|
|
||
| private void OnGUI() | ||
| { | ||
| FlushPendingLogs(); | ||
| EditorGUILayout.HelpBox("Sync Unity MCP Skill to the latest on the selected branch and output the changed file list.", MessageType.Info); | ||
| EditorGUILayout.Space(4f); | ||
|
|
||
| EditorGUILayout.LabelField("Config", EditorStyles.boldLabel); | ||
| using (new EditorGUI.DisabledScope(_isRunning)) | ||
| { | ||
| _repoUrl = EditorGUILayout.TextField("Repo URL", _repoUrl); | ||
| var branchIndex = Array.IndexOf(BranchOptions, _targetBranch); | ||
| if (branchIndex < 0) | ||
| { | ||
| branchIndex = 0; | ||
| } | ||
|
|
||
| var selectedBranchIndex = EditorGUILayout.Popup("Branch", branchIndex, BranchOptions); | ||
| _targetBranch = BranchOptions[selectedBranchIndex]; | ||
|
|
||
| var cliIndex = Array.IndexOf(CliOptions, _cliType); | ||
| if (cliIndex < 0) | ||
| { | ||
| cliIndex = 0; | ||
| } | ||
|
|
||
| var selectedCliIndex = EditorGUILayout.Popup("CLI", cliIndex, CliOptions); | ||
| if (selectedCliIndex != cliIndex) | ||
| { | ||
| var previousCli = _cliType; | ||
| _cliType = CliOptions[selectedCliIndex]; | ||
| TryApplyCliDefaultInstallPath(previousCli, _cliType); | ||
| } | ||
|
|
||
| _installDir = EditorGUILayout.TextField("Install Dir", _installDir); | ||
| } | ||
|
|
||
| EditorGUILayout.Space(8f); | ||
| EditorGUILayout.BeginHorizontal(); | ||
| using (new EditorGUI.DisabledScope(_isRunning)) | ||
| { | ||
| if (GUILayout.Button($"Sync Latest ({_targetBranch})", GUILayout.Height(32f))) | ||
| { | ||
| AppendLineImmediate("Sync task queued..."); | ||
| AppendLineImmediate("Will use GitHub API to read the remote directory tree and perform incremental sync (no repository clone)."); | ||
| RunSyncLatest(); | ||
| } | ||
| } | ||
|
|
||
| if (GUILayout.Button("Clear Log", GUILayout.Width(100f), GUILayout.Height(32f))) | ||
| { | ||
| _logBuilder.Clear(); | ||
| while (_pendingLogs.TryDequeue(out _)) | ||
| { | ||
| } | ||
| } | ||
| EditorGUILayout.EndHorizontal(); | ||
|
|
||
| EditorGUILayout.Space(8f); | ||
| EditorGUILayout.LabelField("Output", EditorStyles.boldLabel); | ||
| _scroll = EditorGUILayout.BeginScrollView(_scroll); | ||
| EditorGUILayout.TextArea(_logBuilder.ToString(), GUILayout.ExpandHeight(true)); | ||
| EditorGUILayout.EndScrollView(); | ||
| } | ||
|
|
||
| private void OnEditorUpdate() | ||
| { | ||
| var changed = FlushPendingLogs(); | ||
| if (_isRunning || changed) | ||
| { | ||
| Repaint(); | ||
| } | ||
| } | ||
|
|
||
| private void RunSyncLatest() | ||
| { | ||
| if (_isRunning) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _isRunning = true; | ||
| SkillSyncService.SyncAsync(_repoUrl, _installDir, _targetBranch, | ||
| line => _pendingLogs.Enqueue($"[{DateTime.Now:HH:mm:ss}] {SanitizeLogLine(line)}"), | ||
| result => | ||
| { | ||
| _isRunning = false; | ||
| if (result.Success) | ||
| { | ||
| _pendingLogs.Enqueue($"[{DateTime.Now:HH:mm:ss}] Sync complete: +{result.Added} ~{result.Updated} -{result.Deleted}"); | ||
| } | ||
| else | ||
| { | ||
| _pendingLogs.Enqueue($"[{DateTime.Now:HH:mm:ss}] [ERROR] {result.Error}"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void TryApplyCliDefaultInstallPath(string previousCli, string currentCli) | ||
| { | ||
| var userHome = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
| var previousDefaultInstall = GetDefaultInstallDir(userHome, previousCli); | ||
| var currentDefaultInstall = GetDefaultInstallDir(userHome, currentCli); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(_installDir) || PathsEqual(_installDir, previousDefaultInstall)) | ||
| { | ||
| _installDir = currentDefaultInstall; | ||
| } | ||
| } | ||
|
|
||
| private static string GetDefaultInstallDir(string userHome, string cliType) | ||
| { | ||
| var baseDir = IsClaudeCli(cliType) ? ".claude" : ".codex"; | ||
| return Path.Combine(userHome, baseDir, "skills/unity-mcp-skill"); | ||
| } | ||
|
|
||
| private static bool IsClaudeCli(string cliType) | ||
| { | ||
| return string.Equals(cliType, ClaudeCli, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| private static bool PathsEqual(string left, string right) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(left) || string.IsNullOrWhiteSpace(right)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| return string.Equals( | ||
| SkillSyncService.ExpandPath(left), | ||
| SkillSyncService.ExpandPath(right), | ||
| StringComparison.Ordinal); | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private void AppendLineImmediate(string line) | ||
| { | ||
| var sanitized = SanitizeLogLine(line); | ||
| if (string.IsNullOrWhiteSpace(sanitized)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _logBuilder.AppendLine($"[{DateTime.Now:HH:mm:ss}] {sanitized}"); | ||
| _scroll.y = float.MaxValue; | ||
| Repaint(); | ||
| } | ||
|
|
||
| private bool FlushPendingLogs() | ||
| { | ||
| var hasNewLine = false; | ||
| while (_pendingLogs.TryDequeue(out var line)) | ||
| { | ||
| _logBuilder.AppendLine(line); | ||
| hasNewLine = true; | ||
| } | ||
|
|
||
| if (hasNewLine) | ||
| { | ||
| _scroll.y = float.MaxValue; | ||
| } | ||
|
|
||
| return hasNewLine; | ||
| } | ||
|
|
||
| private static string SanitizeLogLine(string line) | ||
| { | ||
| if (string.IsNullOrEmpty(line)) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| var sb = new StringBuilder(line.Length); | ||
| var inEscape = false; | ||
| foreach (var ch in line) | ||
| { | ||
| if (inEscape) | ||
| { | ||
| if (ch >= '@' && ch <= '~') | ||
| { | ||
| inEscape = false; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (ch == '\u001b') | ||
| { | ||
| inEscape = true; | ||
| continue; | ||
| } | ||
|
|
||
| if (ch == '\t' || (ch >= ' ' && ch != 127)) | ||
| { | ||
| sb.Append(ch); | ||
| } | ||
| } | ||
|
|
||
| return sb.ToString().Trim(); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
MCPForUnity/Editor/Setup/McpForUnitySkillInstaller.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.