哈哈,我一直是个懒人。这次分享一个批处理脚本,有好几个版本。
第一个:自动修改版本号签入,获取最新,编译,部署到服务器。
把xxx改成自己对应的就可以了。
r程序,是一个自己写的修改版本号的控制台程序。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Text.RegularExpressions; 7 using System.Threading.Tasks; 8 9 namespace ResetVersionNum 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 if(args.Length >= 1) 16 { 17 var path = args[0]; 18 var version = "0.0.*"; 19 if (args.Length >= 2 && !String.IsNullOrEmpty(args[1]) && IsMatchRule(args[1])) 20 { 21 version = args[1]; 22 } 23 24 var streamRead = new FileStream(path, FileMode.Open); 25 var streamWrite = new FileStream("_temp.txt", FileMode.Create); 26 var reader = new StreamReader(streamRead); 27 var writer = new StreamWriter(streamWrite); 28 29 while (!reader.EndOfStream) 30 { 31 var str = reader.ReadLine(); 32 if(String.IsNullOrEmpty(str)) continue; 33 if (str.Contains("AssemblyFileVersion")) continue; 34 if (!str.StartsWith("//") && str.Contains("AssemblyVersion")) 35 { 36 str = "[assembly: AssemblyVersion(\"" + version + "\")]"; 37 } 38 39 writer.WriteLine(str); 40 } 41 42 reader.Close(); 43 streamRead.Close(); 44 45 reader.Dispose(); 46 streamRead.Dispose(); 47 48 writer.Close(); 49 streamWrite.Close(); 50 51 writer.Dispose(); 52 streamWrite.Dispose(); 53 54 File.Copy("_temp.txt", path, true); 55 } 56 } 57 58 static Boolean IsMatchRule(String version) 59 { 60 var regex = new Regex(@"\d.\d.\d|/*.\d|/*"); 61 return regex.IsMatch(version); 62 } 63 } 64 }
@echo off set/p version=请输入版本号: set vsPath=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ set tfsUserId=xxx set tfsUserPwd=xxx set deplaypath=D:\website set svrIp=10.xxxx set svrUserId=xxx set svrUserPwd=xxxx set svrDeplayPath=%svrIp%\xxxxxxx :: 从tfs获取最新版本 "%vsPath%\TF.exe" get /all /login:%tfsUserId%,%tfsUserPwd% :: 设置版本号 setlocal enabledelayedexpansion for /f %%i in ('dir/s/b "AssemblyInfo.cs"') do ( r %%i %version% ) :: 编译发布 rd %deplaypath% \s \q C:\Windows\Microsoft.NET\Framework\v4.0.30319\Msbuild.exe xxxx\xxxx.csproj /t:ResolveReferences;Compile /t:_WPPCopyWebApplication /p:Configuration=Release /p:WebProjectOutputDir=%deplaypath% del /f /q %deplaypath%\bin\*.pdb :: 复制到服务器 net use \\%svrIp% /user:%svrUserId% %svrUserPwd% xcopy %deplaypath%\*.* %svrDeplayPath%\ /e /h /d /y /r /v /f /k :: 删除发布数据 rd %deplaypath% \s \q
第二个版本,简化了,不同步tfs,编译复制完,做一个rar压缩包,往正式环境复制的时候用,不修改版本号。。。。
1 @echo off 2 set path=%ProgramFiles%\WinRAR 3 set path=%ProgramFiles% (x86)\WinRAR 4 5 set deplaypath=F:\publish-release-sso 6 7 set svrIp=10.xxxx 8 set svrUserId=wanda\xxx 9 set svrUserPwd=xxx 10 11 set svrDeplayPath=%svrIp%\xxxx\ 12 13 :: 编译发布 14 rd %deplaypath% \s \q 15 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Msbuild.exe xxxx\xxxxx.csproj /t:ResolveReferences;Compile /t:_WPPCopyWebApplication /p:Configuration=Release /p:WebProjectOutputDir=%deplaypath% 16 del /f /q %deplaypath%\bin\*.pdb 17 18 del /f /q %deplaypath%\Web.config 19 del /f /q %deplaypath%\Setting.xml 20 del /f /q %deplaypath%\*.exe 21 del /f /q %deplaypath%\*.rar 22 23 :: 复制到服务器 24 net use \\%svrIp% /user:%svrUserId% %svrUserPwd% 25 xcopy %deplaypath%\*.* \\%svrDeplayPath% /e /h /d /y /r /v /f /k 26 27 copy zip.cmd %deplaypath%\zip.cmd 28 29 :: 打包rar 30 winrar a -as -r -k -ed -ep1 -y -agYYYYMMDD-NN -xUpFiles %deplaypath%\publish- %deplaypath%\ 31 32 :: 删除发布数据 33 rd %deplaypath% \s \q
第三个版本,自解压格式版本,跟上一版的区别就是,打包成自解压,解压之前,会把正式环境目录给打包备份了
1 @echo off 2 3 set deplaypath=F:\publish-release-sso 4 5 set svrIp=10.xxx 6 set svrUserId=wanda\xxxx 7 set svrUserPwd=xxx 8 9 set svrDeplayPath=%svrIp%\xxxx\ 10 11 :: 编译发布 12 rd %deplaypath% \s \q 13 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Msbuild.exe xxxx\xxxx.csproj /t:ResolveReferences;Compile /t:_WPPCopyWebApplication /p:Configuration=Release /p:WebProjectOutputDir=%deplaypath% 14 del /f /q %deplaypath%\bin\*.pdb 15 16 del /f /q %deplaypath%\Web.config 17 del /f /q %deplaypath%\Setting.xml 18 del /f /q %deplaypath%\*.exe 19 20 :: 复制到服务器 21 net use \\%svrIp% /user:%svrUserId% %svrUserPwd% 22 xcopy %deplaypath%\*.* \\%svrDeplayPath% /e /h /d /y /r /v /f /k 23 24 copy zip.cmd %deplaypath%\zip.cmd 25 copy sfxpara.txt %deplaypath%\sfxpara.txt 26 27 :: 打包exe 28 set path=E:\Program Files\WinRAR 29 winrar a -as -r -k -ed -ep1 -y -agYYYYMMDD-NN -sfx -zsfxpara.txt -xUpFiles %deplaypath%\publish- %deplaypath%\ 30 31 :: 删除发布数据 32 rd %deplaypath% \s \q
Silent=1 ;Setup=文件名 解压后执行 Presetup=zip.cmd SetupCode
上边的是sfxpara.txt的内容。不知道咋拿下来了。。
zip.cmd文件内容
1 :: 备份上一版本 2 set path=%ProgramFiles%\WinRAR 3 set path=%ProgramFiles% (x86)\WinRAR 4 mkdir xxx 5 winrar a -as -r -k -ed -ep1 -agYYYYMMDD-NN -x*.wmv -x*.zip -x*.exe -x*.rar -xzip.cmd -xUpFiles xxx\xxx-bak- *.* 6 move xxx ..\bak\