1. 程式人生 > >【unity拓展】在unity3d中整合SVN命令(非cmd方式而是開啟svn介面方式)

【unity拓展】在unity3d中整合SVN命令(非cmd方式而是開啟svn介面方式)

先看看最終效果:



原理是unity通過呼叫命令列工具執行命令,執行時傳遞對應引數,文章後面會列出常用的svn命令~~~~~

首先,先在unity封裝好呼叫命令列工具的函式,傳命令和引數即可進行呼叫。

public static void ProcessCommand(string command,string argument){
		System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(command);
		info.Arguments = argument;
		info.CreateNoWindow = false;
		info.ErrorDialog = true;
		info.UseShellExecute = true;
		
		if(info.UseShellExecute){
			info.RedirectStandardOutput = false;
			info.RedirectStandardError = false;
			info.RedirectStandardInput = false;
		} else{
			info.RedirectStandardOutput = true;
			info.RedirectStandardError = true;
			info.RedirectStandardInput = true;
			info.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
			info.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
		}
		
		System.Diagnostics.Process process = System.Diagnostics.Process.Start(info);
		
		if(!info.UseShellExecute){
			Debug.Log(process.StandardOutput);
			Debug.Log(process.StandardError);
		}
		
		process.WaitForExit();
		process.Close();
	}

然後,呼叫對應的命令和引數。

[MenuItem("SVN/Commit",false,1)]
	static void SVNCommit(){
		List<string> pathList = new List<string> ();
		string basePath = SVNProjectPath + "/Assets";
		pathList.Add (basePath);
		pathList.Add (SVNProjectPath+"/ProjectSettings");
		
		string commitPath = string.Join ("*", pathList.ToArray ());
		ProcessCommand ("TortoiseProc.exe", "/command:commit /path:"+ commitPath);
	}
	
	[MenuItem("SVN/Update",false,2)]
	static void SVNUpdate(){
		ProcessCommand ("TortoiseProc.exe", "/command:update /path:" + SVNProjectPath + " /closeonend:0");
	}
	
	[MenuItem("SVN/", false, 3)]
	static void Breaker () { }
	
	[MenuItem("SVN/CleanUp",false,4)]
	static void SVNCleanUp(){
		ProcessCommand ("TortoiseProc.exe", "/command:cleanup /path:" + SVNProjectPath);
	}
	
	[MenuItem("SVN/Log",false,5)]
	static void SVNLog(){
		ProcessCommand ("TortoiseProc.exe", "/command:log /path:" + SVNProjectPath);
	}

	static string SVNProjectPath{
		get{
			System.IO.DirectoryInfo parent = System.IO.Directory.GetParent(Application.dataPath);
			return parent.ToString();
		}
	}



以下是常用的命令:

TortoiseSVN是一個GUI客戶端,這個自動化指導為你展示了讓TortoiseSVN對話方塊顯示並收集客戶輸入,如果你希望編寫不需要輸入的指令碼,你應該使用官方的Subversion命令列客戶端。
TortoiseSVN的GUI程式叫做TortoiseProc.exe。所有的命令通過引數/command:asdf指定,其中asdf是必須的命令名(命令名詳見”表 1. 有效命令及選項列表“)。大多數此類命令至少需要一個路徑引數,使用/path:"some\path"指定。在下面的命令表格中,命令引用的是/command:asdf引數,餘下的代表了/path:"some\path"引數。


因為一些命令需要一個目標路徑的列表(例如提交一些特定的檔案),/path引數可以接收多個路徑,使用*分割
TortoiseSVN 使用臨時檔案在 shell 擴充套件和主程式之間傳遞多個引數。從 TortoiseSVN 1.5.0 開始,廢棄/notempfile引數,不再需要增加此引數。
The progress dialog which is used for commits, updates and many more commands usually stays open after the command has finished until the user presses theOK button. This can be changed by checking the corresponding option in the settings dialog. But using that setting will close the progress dialog, no matter if you start the command from your batch file or from the TortoiseSVN context menu.
To specify a different location of the configuration file, use the parameter /configdir:"path\to\config\directory". This will override the default path, including any registry setting.
如果想在進度對話方塊執行完畢後自動關閉,而又不必設定永久性的引數,可以傳遞/closeonend引數。
/closeonend:0 不自動關閉對話方塊
/closeonend:1 如果沒發生錯誤則自動關閉對話方塊
/closeonend:2 如果沒發生錯誤和衝突則自動關閉對話方塊
/closeonend:3如果沒有錯誤、衝突和合並,會自動關閉

下面的列表列出了所有可以使用TortoiseProc.exe訪問的命令,就像上面的描述,必須使用/command:asdf的形式,在列表中,因為節省空間的關係省略了/command的字首。

表 1.有效命令及選項列表
命令和描述
:about 顯示關於對話方塊。如果沒有給命令也會顯示。 
:log 開啟日誌對話方塊,/path 指定了顯示日誌的檔案或目錄,另外還有三個選項可以設定: /startrev:xxx、/endrev:xxx和/strict 
:checkout開啟檢出對話方塊,/path指定了目標路徑,而/url制定了檢出的URL。 
:import 開啟匯入對話方塊,/path 指定了資料匯入路徑。 
:update 將工作副本的/path更新到HEAD,如果給定引數/rev,就會彈出一個對話方塊詢問使用者需要更新到哪個修訂版本。為了防止指定修訂版本號/rev:1234的對話方塊,需要選項/nonrecursive和/ignoreexternals。 
:commit開啟提交對話方塊,/path 指定了目標路徑或需要提交的檔案列表,你也可以使用引數 /logmsg 給提交視窗傳遞預定義的日誌資訊,或者你不希望將日誌傳遞給命令列,你也可以使用/logmsgfile:path,path 指向了儲存日誌資訊的檔案。為了預先填入bug的ID(如果你設定了整合bug追蹤屬性),你可以使用/bugid:"the bug id here"完成這個任務。 
:add 將/path的檔案新增到版本控制 。 
:revert恢復工作副本的本地修改,/path說明恢復哪些條目。 
:cleanup 清理中斷和終止的操作,將工作副本的/path解鎖。 
:resolve 將/path指定檔案的衝突標示為解決,如果給定/noquestion,解決不會向用戶確認操作。 
:repocreate 在/path建立一個版本庫。 
:switch開啟選項對話方塊。/path 指定目標目錄。 
:export將/path的工作副本匯出到另一個目錄,如果/path指向另一個未版本控制目錄,對話方塊會詢問要匯出到/path的URL。 
:mergeOpens the merge dialog. The /path specifies the target directory. For merging a revision range, the following options are available:/fromurl:URL, /revrange:string. For merging two repository trees, the following options are available: /fromurl:URL, /tourl:URL, /fromrev:xxx and/torev:xxx. These pre-fill the relevant fields in the merge dialog. 
:mergeall Opens the merge all dialog. The /path specifies the target directory. 
:copy Brings up the branch/tag dialog. The /path is the working copy to branch/tag from. And the /url is the target URL. You can also specify the/logmsg switch to pass a predefined log message to the branch/tag dialog. Or, if you don't want to pass the log message on the commandline, use /logmsgfile:path, where path points to a file containing the log message. 
:settings 開啟設定對話方塊。 
:remove 從版本控制裡移除/path中的檔案。 
:rename 重新命名/path的檔案,會在對話方塊中詢問新檔案,為了防止一個步驟中詢問相似檔案,傳遞/noquestion。 
:diff Starts the external diff program specified in the TortoiseSVN settings. The /path specifies the first file. If the option /path2 is set, then the diffprogram is started with those two files. If /path2 is omitted, then the diff is done between the file in /path and its BASE. To explicitly set therevision numbers use /startrev:xxx and /endrev:xxx. If/blame is set and /path2 is not set, then the diff is done by first blaming the files with thegiven revisions. 
:showcompare Depending on the URLs and revisions to compare, this either shows a unified diff (if the option unified is set), a dialog with a list offiles thathave changed or if the URLs point to files starts the diff viewer for those two files.
The options url1, url2, revision1 and revision2 must be specified. The options pegrevision, ignoreancestry, blame and unified are optional.
:conflicteditor Starts the conflict editor specified in the TortoiseSVN settings with the correct files for the conflicted file in /path. 
:relocate 開啟重定位對話方塊,/path指定了重定位的工作副本路徑。 
:help 開啟幫助檔案 
:repostatus開啟為修改檢出對話方塊,/path 指定了工作副本目錄。 
:repobrowser Starts the repository browser dialog, pointing to the URL of the working copy given in /path or /path points directly to an URL. An additional option /rev:xxx can be used to specify the revision which the repository browser should show. If the /rev:xxx is omitted, it defaults to HEAD. If /path points to an URL, the /projectpropertiespath:path/to/wcspecifies the path from where to read and use the project properties. 
:ignore 將/path中的物件加入到忽略列表,也就是將這些檔案新增到 svn:ignore 屬性。 
:blame為 /path 選項指定的檔案開啟追溯對話方塊。
如果設定了 /startrev 和 /endrev 選項,不會顯示詢問追溯範圍對話方塊,直接使用這些選項中的版本號。
如果設定了 /line:nnn 選項,TortoiseBlame 會顯示指定行數。
也支援 /ignoreeol,/ignorespaces 和 /ignoreallspaces 選項。
:cat將/path指定的工作副本或URL的檔案儲存到/savepath:path,修訂版本號在/revision:xxx,這樣可以得到特定修訂版本的檔案。 
:createpatch 建立/path下的補丁檔案。 
:revisiongraph 顯示/path目錄下的版本變化圖。 
:lock Locks a file or all files in a directory given in /path. The 'lock' dialog is shown so the user can enter a comment for the lock. 
:unlock Unlocks a file or all files in a directory given in /path. 
:rebuildiconcache Rebuilds the windows icon cache. Only use this in case the windows icons are corrupted. A side effect of this (which can't beavoided) is that the icons on the desktop get rearranged. To suppress the message box, pass /noquestion. 
:properties 顯示 /path 給出的路徑之屬性對話方塊。

使用示例(which should be entered on one line):

TortoiseProc.exe /command:commit
                 /path:"c:\svn_wc\file1.txt*c:\svn_wc\file2.txt"
                 /logmsg:"test log message" /closeonend:0

TortoiseProc.exe /command:update /path:"c:\svn_wc\" /closeonend:0

TortoiseProc.exe /command:log /path:"c:\svn_wc\file1.txt"
                 /startrev:50 /endrev:60 /closeonend:0