在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;

/// <summary>
/// Unity SVN工具
/// </summary>
public class SVNTool
{
/// <summary>
/// svn的GUI程序名
/// </summary>
private static readonly string SVN_APP_NAME = "TortoiseProc.exe";

/// <summary>
/// 项目路径,即Assets文件夹
/// </summary>
private static string projectPath = Application.dataPath;

/// <summary>
/// cmd命令模板
/// </summary>
private static string cmdCommandModule = SVN_APP_NAME + " " + "/command:{0} /path:{1} /closeonend:0";

/// <summary>
/// svn update assets
/// </summary>
/// update整个Assets文件夹
[MenuItem("Tools/SVN Tool/SVN Update All")]
public static void UpdateAll()
{
string cmdCommand = string.Format(cmdCommandModule, "update", projectPath);
InvokeCmd(cmdCommand);
}

/// <summary>
/// svn commit assets
/// </summary>
/// commit整个Assets文件夹
[MenuItem("Tools/SVN Tool/SVN Commit All")]
public static void CommitAll()
{
string cmdCommand = string.Format(cmdCommandModule, "commit", projectPath);
InvokeCmd(cmdCommand);
}

/// <summary>
/// svn revert assets
/// </summary>
/// revert整个Assets文件夹
[MenuItem("Tools/SVN Tool/SVN Revert All")]
public static void RevertAll()
{
string cmdCommand = string.Format(cmdCommandModule, "revert", projectPath);
InvokeCmd(cmdCommand);
}

/// <summary>
/// svn showlog assets
/// </summary>
/// showLog整个Assets文件夹
[MenuItem("Tools/SVN Tool/SVN ShowLog All")]
public static void ShowLogAll()
{
string cmdCommand = string.Format(cmdCommandModule, "log", projectPath);
InvokeCmd(cmdCommand);
}

/// <summary>
/// svn update select
/// </summary>
/// update在Assets界面选中的资源
[MenuItem("Assets/SVN Tool/SVN Update")]
public static void UpdateSelect()
{
string cmdCommand = string.Format(cmdCommandModule, "update", GetSVNCommand());
InvokeCmd(cmdCommand);
}

/// <summary>
/// svn commit select
/// </summary>
/// commit在Assets界面选中的资源
[MenuItem("Assets/SVN Tool/SVN Commit")]
public static void CommitSelect()
{
string cmdCommand = string.Format(cmdCommandModule, "commit", GetSVNCommand());
InvokeCmd(cmdCommand);
}

/// <summary>
/// svn revert select
/// </summary>
/// revert在Assets界面选中的资源
[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;
}

/// <summary>
/// 获取选中的资源的路径
/// </summary>
/// <returns>选中资源的路径 string[]</returns>
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;
}

/// <summary>
/// 调用cmd
/// </summary>
/// <param name="cmdCommand">cmd命令</param>
private static void InvokeCmd(string cmdCommand)
{
new Thread(new ThreadStart(() => //新建线程,ThreadStart表示在Thread上执行的方法
{
try
{
Process p = new Process(); //新建系统进程

//Process中的StartInfo为要传递给Process.Start()方法的属性

//设置p.StartInfo属性
p.StartInfo.FileName = "cmd.exe"; //要启动的应用程序
p.StartInfo.Arguments = "/c " + cmdCommand + "&exit"; //启动应用程序时要使用的一组命令行参数
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动进程(如果是从可执行文件创建进程,应设置为false)
p.StartInfo.RedirectStandardInput = true; //指示应用程序的输入是否从StandardInput流中读取的值(StandardInput)
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();
}
}

该工具实现了SVNupdatecommitrevertshowlog这四个最常用的功能。

补充:
 1、除了以上工具中的SVN命令外,SVN还有其他几种cmd命令:checkoutdiffadd
 2、在Visual Studio中,可直接在拓展中安装VisualSVN for Visual Studio插件实现SVN操作。