[转载]SVN自动更新测试服务器工作副本(C#写winform程序实现) – Machine Lee – 博客园.
根据工作需要,项目将采用SVN做版本控制,于是乎就安装了如下软件:
1、TortoiseSVN Version:1.6.7
2、Subversion Version:1.6.5
3、VisualSVN Version:2.0.6
其中1是SVN客户端,2是服务器,3是用于与VS .Net framework集成的组件。
具体安装步骤就不多讲了,网上很多帖子都详细描述过了,本文主要讲的是如何实现最新提交自动更新到测试服务器工作副本。
背景:
为什么要实现SVN自动更新呢?因为实际开发过程中,程序员一般都是在本地开发机上 开发,本地验证无误后上传至测试服务器验证生产环境正确性,修改代码多的时候,上传文件也是一件累人的活,还浪费时间,所以就有了实现SVN自动更新到测 试服务器工作副本的需求,既省时,又能保证文件不遗漏。
过程:
要实现SVN自动更新,无非就是使用SVN的钩子,网络上不少帖子都是讲如何通过版 本库hooks文件夹下post-commit文件实现自动更新的,有的是写成.bat文件,有的是shell脚本。笔者开始是借鉴网上的方法,写成了 post-commit.bat文件,实现了自动更新。但是,由于我们的项目比较大,写成.bat文件的话,就只能在根目录下执行update操作,速度 非常的慢,大概是2分钟。是可忍孰不可忍,于是上网查找,发现.exe文件也可以作为钩子程序嘛,这不就简单了,于是用C#写了个Winform程 序,commit+update瞬间完成!下面是C#代码,有详细的备注,供大家参考!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace SVNGetTheLastRes
{
public partial class Form1 : Form
{
/// <summary>
///
/// </summary>
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//查找最近更新文件,并将命令返回结果输出至txt文件
Execute("svnlook changed D:/subversion/project1 > D:/Subversion/project1/hooks/test.txt");
//读取生成的文件
string strPath = ResumeTxt("D:/Subversion/project1/hooks/test.txt");
//文件内容处理:按换行符将读取的字符串转换成字符串数组
string[] aryPath = strPath.Split('\n');
//循环更新文件
for (int i = 0; i < aryPath.Length; i++)
{
//处理掉回车符
aryPath[i].Replace('\r', ' ');
//经测试,文件中最后一行是空行,但为了避免遗漏,用非空判断,而不是循环的length-1
if (!aryPath[i].Trim().Equals(""))
{
//根据文件中的数据格式,从第五个字符开始才是文件路径
string strFile = aryPath[i].Trim().Substring(4);
//组织命令并执行,其中D:/是项目所在文件夹,根据自己的情况组织
string strCmd = "svn update D:/" + strFile + " --username *** --password ***";
Execute(strCmd);
}
}
}
catch (Exception ex)
{
}
finally
{
this.Close();
}
}
public string ResumeTxt(string path)
{
string str = string.Empty;
StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);
str = reader.ReadToEnd();
//再通过查询解析出来的的字符串有没有GB2312的字段,来判断是否是GB2312格式的,如果是,则重新以GB2312的格式解析
Regex reGB = new Regex("GB2312", RegexOptions.IgnoreCase);
Match mcGB = reGB.Match(str);
if (mcGB.Success)
{
StreamReader reader2 = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
str = reader2.ReadToEnd();
}
return str;
}
/// <summary>
/// 执行DOS命令并返回结果
/// </summary>
/// <param name="dosCommand">Dos命令语句</param>
/// <returns>DOS命令返回值</returns>
public string Execute(string dosCommand)
{
return Execute(dosCommand, 0);
}
/// <summary>
/// 执行DOS命令,返回DOS命令的输出
/// </summary>
/// <param name="dosCommand">dos命令</param>
/// <param name="milliseconds">等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待</param>
/// <returns>返回DOS命令的输出</returns>
public static string Execute(string dosCommand, int seconds)
{
string output = ""; //输出字符串
if (dosCommand != null && dosCommand != "")
{
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";//设定需要执行的命令
startInfo.Arguments = "/C " + dosCommand;//设定参数,其中的“/C”表示执行完命令后马上退出
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
startInfo.RedirectStandardInput = false;//不重定向输入
startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
try
{
if (process.Start())//开始进程
{
if (seconds == 0)
{
process.WaitForExit();//这里无限等待进程结束
}
else
{
process.WaitForExit(seconds); //这里等待进程结束,等待时间为指定的毫秒
}
output = process.StandardOutput.ReadToEnd();//读取进程的输出
}
}
catch
{
}
finally
{
if (process != null)
process.Close();
}
}
return output;
}
}
}
需要注意的是,用update命令更新时,要求测试服务器工作副本必须是受控的,否则,应该改用export命令,export命令用法请查看”SVN export –help”。
结果:
实现了SVN自动更新的功能。实际上,既然能用exe程序作为SVN钩子使用,那就可以扩展很多功能了,包括每次更新的邮件提醒,甚至是重要文件更新时的短信提醒,还能做文件更新日志等等。
Mikel