在Unity中通过调用SVN的命令行来执行操作
工具代码
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| using System; using System.Threading; using System.Diagnostics; using UnityEngine; using UnityEditor;
public class SVNTool { private static readonly string SVN_APP_NAME = "TortoiseProc.exe";
private static string projectPath = Application.dataPath;
private static string cmdCommandModule = SVN_APP_NAME + " " + "/command:{0} /path:{1} /closeonend:0";
[MenuItem("Tools/SVN Tool/SVN Update All")] public static void UpdateAll() { string cmdCommand = string.Format(cmdCommandModule, "update", projectPath); InvokeCmd(cmdCommand); }
[MenuItem("Tools/SVN Tool/SVN Commit All")] public static void CommitAll() { string cmdCommand = string.Format(cmdCommandModule, "commit", projectPath); InvokeCmd(cmdCommand); }
[MenuItem("Tools/SVN Tool/SVN Revert All")] public static void RevertAll() { string cmdCommand = string.Format(cmdCommandModule, "revert", projectPath); InvokeCmd(cmdCommand); }
[MenuItem("Tools/SVN Tool/SVN ShowLog All")] public static void ShowLogAll() { string cmdCommand = string.Format(cmdCommandModule, "log", projectPath); InvokeCmd(cmdCommand); }
[MenuItem("Assets/SVN Tool/SVN Update")] public static void UpdateSelect() { string cmdCommand = string.Format(cmdCommandModule, "update", GetSVNCommand()); InvokeCmd(cmdCommand); }
[MenuItem("Assets/SVN Tool/SVN Commit")] public static void CommitSelect() { string cmdCommand = string.Format(cmdCommandModule, "commit", GetSVNCommand()); InvokeCmd(cmdCommand); }
[MenuItem("Assets/SVN Tool/SVN Revert")] public static void RevertSelect() { string cmdCommand = string.Format(cmdCommandModule, "revert", GetSVNCommand()); InvokeCmd(cmdCommand); }
public static string GetSVNCommand() { string[] selectFilePath = GetSelectFilePath(); string temp = string.Empty; for(int i = 0; i < selectFilePath.Length; i++) { temp += selectFilePath[i]; temp += "*"; } return temp; }
public static string[] GetSelectFilePath() { string[] guidArray = Selection.assetGUIDs; string[] selectFilePath = new string[guidArray.Length]; for(int i = 0; i < guidArray.Length; i++) { selectFilePath[i] = AssetDatabase.GUIDToAssetPath(guidArray[i]); } return selectFilePath; }
private static void InvokeCmd(string cmdCommand) { new Thread(new ThreadStart(() => { try { Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c " + cmdCommand + "&exit"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true;
p.Start(); p.WaitForExit(); p.Close(); } catch(Exception e) { Console.WriteLine(e.Message); } })).Start(); } }
|
该工具实现了SVN
的update
、commit
、revert
、showlog
这四个最常用的功能。
补充:
1、除了以上工具中的SVN
命令外,SVN
还有其他几种cmd
命令:checkout
、diff
、add
2、在Visual Studio
中,可直接在拓展中安装VisualSVN for Visual Studio
插件实现SVN
操作。