运行环境: Visual Studio
NET
介绍 我们每一个从原有的开发环境转移到VSNET下的用户都遭遇到不少的阻碍我所碰到的一个障碍就是:我原有的那些macro无法继续工作了
现在的这个编译序列号自动增长工具是从很多人的代码片断组合而成的虽然它并不完善而且缺乏一些特性但它至少为进一步开发提供了一个坚实的基础
目标
这里是自动编译序列号的需求:
在每一次release编译的时候自动增加保存在一个单独文件中的编译序列号这个文件将被加入到我的项目中
自动记录编译的日期和序列号
代码分析
我的第一个任务是找到一个能够替代VS 中Application_BeforeBuildStart()的事件VSNET的设计者为我们提供了非常容易使用的编译事件:
OnBuildBegin
OnBuildDone
OnBuildProjConfigBegin
OnBuildProjConfigDone
这些事件句柄的命名规则有一点误导我们不希望使用OnBuildBegin因为即使我们同时针对多种配置(release debug等等)进行编译它也只会被执行一次这使得难以只在编译release版本的时候增加编译序列号OnBuildProjConfigBegin就能够在针对每一种配置编译的时候被执行并且还提供一个名为ProjectConfig的字符串参数来描述当前所进行的编译使用的是哪一种配置
大部分的任务是在OnBuildProjConfigBegin事件处理函数中完成的 它还使用了两个辅助macro:
WriteToLogFile
WriteToOutputBuildPane
这两个宏都可以被写入到OnBuildProjConfigBegin事件处理函数中或者在不用时删除
代码
OnBuildProjConfigBegin event handler
Private Sub BuildEvents_OnBuildProjConfigBegin(
ByVal Project As String
ByVal ProjectConfig As String
ByVal Platform As String
ByVal SolutionConfig As String)
Handles BuildEventsOnBuildProjConfigBegin
abort if build type is debug
If InStr( ProjectConfig Debug ) Then Exit Sub
get ver filename
Dim res_filename As String
res_filename = DTESolutionFullName
res_filename = PathChangeExtension(res_filename ver)
open VERSION FILE and increment build number
Dim msg_text As String
If FileExists(res_filename) Then
Dim line As String
Try
Dim sr As StreamReader = New StreamReader(res_filename)
line = srReadLine()
srClose()
Catch ex As Exception
ModuleWriteToOutputBuildPane(vbCrLf & _
Version file read failed : &
exMessage & vbCrLf)
End Try
line = Right(line lineLength )
Try
Dim sw As StreamWriter = FileCreateText(res_filename)
swWriteLine(#define BUILD_NUM {} line + )
swClose()
Catch ex As Exception
ModuleWriteToOutputBuildPane(vbCrLf & _
Version file write failed : &
exMessage & vbCrLf)
End Try
msg_text = Build number : & line + & & Now
ModuleWriteToOutputBuildPane(vbCrLf & msg_text & vbCrLf)
ModuleWriteToLogFile(msg_text)
Else
Try
Dim sw As StreamWriter = FileCreateText(res_filename)
swWriteLine(#define BUILD_NUM )
swClose()
Catch ex As Exception
ModuleWriteToOutputBuildPane(vbCrLf & _
Version file write failed : &
exMessage & vbCrLf)
End Try
msg_text = Build number : & Now
ModuleWriteToOutputBuildPane(vbCrLf & msg_text & vbCrLf)
ModuleWriteToLogFile(msg_text)
End If
End Sub
write text message to a log file
Sub WriteToLogFile(ByVal msg_text As String)
Dim log_filename As String
log_filename = DTESolutionFullName
log_filename = PathChangeExtension(log_filename logtxt)
Try
Dim sw As StreamWriter = FileAppendText(log_filename)
swWriteLine(msg_text)
swClose()
Catch ex As Exception
MsgBox(Log file write failed : & exMessage)
End Try
End Sub
write a text message to the build pane of the output tool window
Sub WriteToOutputBuildPane(ByVal msg_text As String)
Create a tool window handle for the Output window
Dim win As Window = DTEWindowsItem(EnvDTEConstants _
qvsWindowKindOutput)
Create handles to the Output window and its build pane
Dim OW As OutputWindow = winObject
Dim OWp As OutputWindowPane
OWp = OWOutputWindowPanesItem(Build)
Add a line of text to the output pane
OWpOutputString(msg_text & vbCrLf)
End Sub
集成
注意如果你还没有一个Macro Project那么先要创建一个你需要打开一个Macro Project并显示在VS Macros IDE (译者注:你可以通过快捷键Alt + F或者点击View > Other Windows > Macro Explorer打开宏浏览窗口双击其中一个宏进行编辑)
每一个宏工程拥有一个名为EnvironmentEvents的页面双击打开它
从类名称下拉列表中选择BuildEvents
从方法名下拉列表中选择OnBuildProjConfigBegin
增加两个引用申明(import)
异常类的申明需要引用System名称空间 文件的操作类申明需要引用SystemIO
在OnBuildProjConfigBegin方法中添加
双击打开Module页面
Module是IDE使用的一个默认页面名称如果你使用了不同的名称 那么一定要修改OnBuildProjConfigBegin中的对应名字空间
添加两个辅助方法添加对 System和SystemIO的引用申明
运行
保存编辑结果然后编译检查是否有错误编译成功后就可以直接使用了使用很简单这段代码会在每次编译时被执行