VB.NET 自动打包程序

由于每次将编译好的程序提交时都要花费不少时间用来打包,所以我须要有一个让程序自动完成这些琐碎的工做。小程序

首先说一下个人目标。个人C#程序(在本文中暂时称做Example.exe)编译后暂时存放在Debug目录中,该程序有两种形态:平台端和客户端,它们分别给不一样的用户使用,这两个客户端的不一样之处仅在与App.config(能够被看作一个XML文件)中的配置不一样。打包时须要先使用NSIS脚本对Debug目录下的内容制做成安装包,再使用WinRAR将安装包进行压缩。除了两个安装包外,还须要提供一些文件放置到自动更新的目录下,这些文件须要单独打成安装包。缓存

为此我使用VB.NET写了一个小程序(暂时取名叫SoftwareRelease),来实现这个功能。app

配置文件以下:工具

<?xml version="1.0" encoding="gb2312"?>
<!--SoftwareRelease配置文件-->
<Config Software="SoftwareRelease">
  <!--Debug目录地址-->
  <DebugDir>D:\Example\bin\Debug</DebugDir>
  <!--打包完毕后软件包所在目录地址-->
  <ObjectDir>D:\ObjectPath</ObjectDir>
  <!--编译器路径 注意用 makensis.exe 而不是 makensisw.exe -->
  <CompilerPath>D:\NSIS\makensis.exe</CompilerPath>
  <!--"RAR压缩工具路径-->
  <WinRARPath>D:\WinRAR\rar.exe</WinRARPath>
  <!--编译脚本路径-->
  <ScriptList>
    <Script Name="platform" 
            Directory="D:\PackageTools\打包脚本" 
            Script="Platform.nsi" />
    <Script Name="client" 
            Directory="D:\PackageTools\打包脚本" 
            Script="Client.nsi" />
  </ScriptList>
</Config>

将这个文件放置在编译好的EXE文件相同路径下便可。命令行

由于程序中要使用WinRAR和NSIS编译工具,所以在配置文件中要写明这两个程序可执行文件的绝对路径。debug

程序代码以下:code

Imports System.Xml
Imports System.IO

''' <summary>
''' SoftwareRelease 软件自动打包工具
''' </summary>
''' <remarks></remarks>
Module ModuleMain

    ''' <summary>
    ''' SoftwareRelease 软件自动打包工具
    ''' </summary>
    ''' <remarks></remarks>
    Sub Main()

        '设置控制台缓冲区大小
        Console.BufferHeight = 5000
        '控制台文字颜色 - 青色
        Console.ForegroundColor = ConsoleColor.Cyan
        '程序使用的标准时间
        Dim dateTimeUni As DateTime = DateTime.Now

        Console.WriteLine("SoftwareRelease 软件自动打包工具")
        Console.WriteLine("版本号:" & System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString())
        Console.WriteLine(New String("=", 20))
        Console.WriteLine(dateTimeUni.ToString)
        Console.WriteLine()

        ' ----------- 一、读入参数即相关准备工做 ----------- '
        Dim Args As String() = System.Environment.GetCommandLineArgs()

        Dim bNeedPlatform As Boolean = False '打包平台端
        Dim bNeedClient As Boolean = False '打包客户端
        Dim bNeedUpdate As Boolean = False '打包自动更新文件

        '分析传入参数
        If Args.Contains("/?") Or Args.Length = 1 Then '查看帮助
            PrintUsage()
            Pause()
            Exit Sub
        ElseIf Args.Contains("/A") Or Args.Contains("/a") Then '全量包
            bNeedPlatform = True
            bNeedClient = True
            bNeedUpdate = True
        Else
            If Args.Contains("/P") Or Args.Contains("/p") Then '打包平台端
                bNeedPlatform = True
            End If
            If Args.Contains("/C") Or Args.Contains("/c") Then '打包客户端
                bNeedClient = True
            End If
            If Args.Contains("/U") Or Args.Contains("/u") Then '打包自动更新文件
                bNeedUpdate = True
            End If
        End If

        Console.WriteLine("本次执行任务:")
        If bNeedPlatform Then
            Console.WriteLine("打包平台端")
        End If
        If bNeedClient Then
            Console.WriteLine("打包客户端")
        End If
        If bNeedUpdate Then
            Console.WriteLine("打包自动更新文件")
        End If
        Console.WriteLine()

        '编译完毕后程序目录地址
        Dim debugDir As String = ""
        '打包完毕后软件包所在目录地址
        Dim objectDir As String = ""
        '编译器路径
        Dim compilerPath As String = ""
        'WinRAR压缩工具路径
        Dim winRARPath As String = ""
        '编译脚本所在目录:平台端
        Dim scriptDirOfPlatform As String = ""
        '编译脚本名称:平台端
        Dim scriptFileOfPlatform As String = ""
        '编译脚本所在目录:客户端
        Dim scriptDirOfClient As String = ""
        '编译脚本路径:客户端
        Dim scriptFileOfClient As String = ""

        '读取配置
        Console.WriteLine("正在读取配置...")
        Dim xmlDoc As XmlDocument = New XmlDocument()
        xmlDoc.Load("Config.xml")
        Dim xmlRoot As XmlNode = xmlDoc.SelectSingleNode("Config")

        For Each xmlObj In xmlRoot.ChildNodes
            If xmlObj.GetType().ToString() = "System.Xml.XmlElement" Then
                Dim xmlEle As XmlElement = DirectCast(xmlObj, XmlElement)
                Select Case xmlEle.Name
                    Case "DebugDir"
                        debugDir = xmlEle.InnerText.ToString.Trim
                        Console.WriteLine("编译完毕后程序目录地址:" & debugDir)
                    Case "ObjectDir"
                        objectDir = xmlEle.InnerText.ToString.Trim
                        Console.WriteLine("打包完毕后软件包所在目录地址:" & objectDir)
                    Case "CompilerPath"
                        compilerPath = xmlEle.InnerText.ToString.Trim
                        Console.WriteLine("编译器路径:" & compilerPath)
                    Case "WinRARPath"
                        winRARPath = xmlEle.InnerText.ToString.Trim
                        Console.WriteLine("WinRAR压缩工具路径:" & winRARPath)
                    Case "ScriptList"
                        For Each xmlObj2 In xmlEle.ChildNodes
                            If xmlObj2.GetType().ToString() = "System.Xml.XmlElement" Then
                                Dim xmlEle2 As XmlElement = DirectCast(xmlObj2, XmlElement)
                                If xmlEle2.Name = "Script" And xmlEle2.GetAttribute("Name") = "platform" Then
                                    scriptDirOfPlatform = xmlEle2.GetAttribute("Directory").ToString
                                    Console.WriteLine("编译脚本所在目录(平台端):" & scriptDirOfPlatform)
                                    scriptFileOfPlatform = xmlEle2.GetAttribute("Script").ToString
                                    Console.WriteLine("编译脚本名称(平台端):" & scriptFileOfPlatform)
                                End If
                                If xmlEle2.Name = "Script" And xmlEle2.GetAttribute("Name") = "client" Then
                                    scriptDirOfClient = xmlEle2.GetAttribute("Directory").ToString
                                    Console.WriteLine("编译脚本所在目录(客户端):" & scriptDirOfClient)
                                    scriptFileOfClient = xmlEle2.GetAttribute("Script").ToString
                                    Console.WriteLine("编译脚本名称(客户端):" & scriptFileOfClient)
                                End If
                            End If
                        Next
                    Case Else
                End Select
            End If
        Next

        '释放资源
        xmlDoc.RemoveAll()
        xmlDoc = Nothing

        '判断配置合法性
        Dim isConfigLegal As Boolean = True
        If String.IsNullOrWhiteSpace(debugDir) Then
            Console.WriteLine("编译完毕后程序目录地址缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(objectDir) Then
            Console.WriteLine("打包完毕后软件包所在目录地址缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(compilerPath) Then
            Console.WriteLine("编译器路径缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(winRARPath) Then
            Console.WriteLine("RAR压缩工具路径缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(scriptDirOfPlatform) Then
            Console.WriteLine("编译脚本所在目录(平台端)缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(scriptFileOfPlatform) Then
            Console.WriteLine("编译脚本名称(平台端)缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(scriptDirOfClient) Then
            Console.WriteLine("编译脚本所在目录(客户端)缺失")
            isConfigLegal = False
        End If
        If String.IsNullOrWhiteSpace(scriptFileOfClient) Then
            Console.WriteLine("编译脚本名称(客户端)缺失")
            isConfigLegal = False
        End If
        If Not isConfigLegal Then
            Pause()
            Exit Sub
        End If

        Console.WriteLine("配置读取完毕!")
        Console.WriteLine()

        ' ----------- 二、检查Debug目录 ----------- '
        If Not Directory.Exists(debugDir) Then
            Console.WriteLine("待打包程序目录不存在!")
            Pause()
            Exit Sub
        End If
        If Not File.Exists(debugDir + "\Example.exe") Then
            Console.WriteLine("待打包程序不存在!")
            Pause()
            Exit Sub
        End If
        If Not File.Exists(debugDir + "\Example.exe.config") Then
            Console.WriteLine("待打包程序关键配置文件缺失!")
            Pause()
            Exit Sub
        End If
        '清理掉全部pdb文件,这些文件是不须要的
        Console.WriteLine("正在清理PDB文件")
        DeleteAllPdb(New DirectoryInfo(debugDir))
        Console.WriteLine("正在清理登陆信息")
        '清理登陆信息,打包后的登陆信息必须是干净的
        If File.Exists(debugDir + "\Config\LoginData.xml") Then
            'TODO:清理登录信息
        End If
        Console.WriteLine("待打包程序目录检查完毕")
        Console.WriteLine()

        ' ----------- 三、将Debug目录中的程序配置成平台端 ----------- '
        If bNeedPlatform Or bNeedUpdate Then
            Console.WriteLine("将程序设置为平台端")
            If (SetAppConfig(debugDir + "\Example.exe.config", "platform")) Then
                Console.WriteLine("设置完毕")
                Console.WriteLine()
            Else
                Console.WriteLine("设置失败!")
                Pause()
                Exit Sub
            End If
        End If

        ' ----------- 四、复制一份,用于以后自动更新 ----------- '
        If bNeedUpdate Then
            If (Directory.Exists(objectDir & "\platform")) Then
                Console.WriteLine("目录【" & objectDir & "\platform】已存在,正在删除该目录")
                Directory.Delete(objectDir & "\platform", True)
                Console.WriteLine("删除完毕!")
            End If
            Console.WriteLine("正在复制文件到目录【" & objectDir & "\platform】")
            My.Computer.FileSystem.CopyDirectory(debugDir, objectDir & "\platform", True)
            Console.WriteLine("复制完毕!")
            Console.WriteLine("正在删除冗余文件")
            'TODO:删除冗余文件(即不用自动更新下来的文件)
            Console.WriteLine("冗余文件删除完毕!")
            Console.WriteLine()
        End If

        ' ----------- 五、为Debug目录中内容打包 ----------- '
        If bNeedPlatform Then

            Console.WriteLine("正在打包:平台端")
            Shell(compilerPath & " " & scriptDirOfPlatform & "\" & scriptFileOfPlatform)
            Console.WriteLine("平台端打包完毕")

            '压缩到RAR文件
            Dim packageName = scriptDirOfPlatform & "\平台端打包后文件.exe" '这里要作对应修改!
            If File.Exists(packageName) Then
                Dim rarName As String = dateTimeUni.ToString("yyyyMMdd") & "_PLATFORM.rar"
                'a表示压缩文件,-r表示递归,-ep表示忽略路径信息
                Shell(winRARPath & " a -ep " & objectDir & "\" & rarName & " " & packageName, AppWinStyle.NormalFocus, True)
            End If

        End If

        ' ----------- 六、将Debug目录中的程序配置成客户端 ----------- '
        If bNeedClient Or bNeedUpdate Then
            Console.WriteLine("将程序设置为客户端")
            If (SetAppConfig(debugDir + "\Example.exe.config", "client")) Then
                Console.WriteLine("设置完毕")
                Console.WriteLine()
            Else
                Console.WriteLine("设置失败!")
                Pause()
                Exit Sub
            End If
        End If

        ' ----------- 七、复制一份,用于以后自动更新 ----------- '
        If bNeedUpdate Then
            If (Directory.Exists(objectDir & "\client")) Then
                Console.WriteLine("目录【" & objectDir & "\client】已存在,正在删除该目录")
                Directory.Delete(objectDir & "\client", True)
                Console.WriteLine("删除完毕!")
            End If
            Console.WriteLine("正在复制文件到目录【" & objectDir & "\client】")
            My.Computer.FileSystem.CopyDirectory(debugDir, objectDir & "\client", True)
            Console.WriteLine("复制完毕!")
            Console.WriteLine("正在删除冗余文件")
            'TODO:删除冗余文件(即不用自动更新下来的文件)
            Console.WriteLine("冗余文件删除完毕!")
            Console.WriteLine()
        End If

        ' ----------- 八、为Debug目录中内容打包 ----------- '
        If bNeedClient Then

            Console.WriteLine("正在打包:客户端")
            Shell(compilerPath & " " & scriptDirOfClient & "\" & scriptFileOfClient)
            Console.WriteLine("客户端打包完毕")

            '压缩到RAR文件
            Dim packageName = scriptDirOfPlatform & "\客户端打包后文件.exe" '这里要作对应修改!
            If File.Exists(packageName) Then
                Dim rarName As String = dateTimeUni.ToString("yyyyMMdd") & "_CLIENT.rar"
                'a表示压缩文件,-r表示递归,-ep表示忽略路径信息
                Shell(winRARPath & " a -ep " & objectDir & "\" & rarName & " " & packageName, AppWinStyle.NormalFocus, True)
            End If

        End If

        ' ----------- 九、打包自动更新包 ----------- '
        If bNeedUpdate Then
            Dim rarNameUpdate As String = dateTimeUni.ToString("yyyyMMdd") & "_UPDATE.rar"
            'a表示压缩文件,-r表示递归,-ep1表示忽略被压缩的根文件夹
            Dim command As String =
                winRARPath & " a -r -ep1 " & objectDir & "\" & rarNameUpdate & " " &
                objectDir & "\platform" & " " & objectDir & "\client"
            Shell(command, AppWinStyle.NormalFocus, True)
        End If

        Console.WriteLine("打包工做所有完成!")

        Pause()

    End Sub

    ''' <summary>
    ''' 配置AppConfig中的客户端类型
    ''' </summary>
    ''' <param name="appConfigPath">AppConfig文件路径</param>
    ''' <param name="clientType">客户端类型字符串</param>
    ''' <returns>true:修改为功,false:修改失败</returns>
    ''' <remarks></remarks>
    Function SetAppConfig(appConfigPath As String, clientType As String)
        Dim xmlAppConfig As XmlDocument = New XmlDocument
        xmlAppConfig.Load(appConfigPath)
        Dim xmlRoot As XmlNode = xmlAppConfig.SelectSingleNode("configuration")
        'TODO:更改App.config中配置,更改为功则 GoTo 到 END_CONFIG
        Return False
END_CONFIG:
        xmlAppConfig.Save(appConfigPath)
        xmlAppConfig.RemoveAll()
        xmlAppConfig = Nothing
        Return True
    End Function

    ''' <summary>
    ''' 删除全部PDB文件
    ''' </summary>
    ''' <param name="dif"></param>
    ''' <remarks></remarks>
    Private Sub DeleteAllPdb(dif As DirectoryInfo)
        '遍历各个子文件夹
        For Each di As IO.DirectoryInfo In dif.GetDirectories
            DeleteAllPdb(di)
        Next
        For Each f As System.IO.FileInfo In dif.GetFiles
            If f.Extension.ToLower = ".pdb" Then
                f.Delete()
            End If
        Next
    End Sub

    ''' <summary>
    ''' 打印程序用法
    ''' </summary>
    ''' <remarks></remarks>
    Sub PrintUsage()

        Dim Usage(7) As String
        Usage(0) = "程序使用方法"
        Usage(1) = "SoftwareRelease [ /? | /p | /c | /u | /a ]"
        Usage(2) = "没有参数:显示帮助,这与键入 /? 是同样的"
        Usage(3) = "/p:打包平台端"
        Usage(4) = "/c:打包客户端"
        Usage(5) = "/u:打包自动更新包"
        Usage(6) = "/a:打包所有内容,至关于 /p /m /u 同时存在"
        Console.WriteLine(Join(Usage, vbCrLf))

    End Sub

    ''' <summary>
    ''' 按任意键继续
    ''' </summary>
    ''' <remarks></remarks>
    Sub Pause()

        Console.WriteLine("按任意键继续")
        System.Console.ReadKey()
        System.Console.Write(Chr(8) + " ") '删除按下的“任意键”字符

    End Sub

End Module

以上代码须要注意的方面有:orm

一、打包前要先删除冗余文件,如*.pdb文件。对于自动更新端,还要删除那些可能会随着每次登陆发生改变的文件和目录(如本地缓存数据用的文件等)xml

二、WinRAR程序(rar.exe)和NSIS编译工具(makensis.exe)都提供了功能强大的命令,能够利用这些命令完成不少自动化操做内容。注意NSIS编译工具使用makensis.exe而不是makensisw.exe,后者是一个具有GUI的工具,前者是为命令行提供的工具。递归

三、本程序接收的命令行参数为 /p(打包平台端)、/c(打包客户端)、/u(打包自动更新)、/a(打包所有)和/?(帮助),若是无参数则默认显示帮助

四、控制台缓冲区大小要设置得大一些,不然Windows的控制台默认只支持保留最后的300行内容

五、为了更方便使用本程序,能够写一个bat脚本直接调用本程序。内容以下:

SoftwareRelease.exe /a

END

相关文章
相关标签/搜索