扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
这些事件句柄的命名规则有一点误导.我们不希望使用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 BuildEvents.OnBuildProjConfigBegin
' abort if build type is debug
If InStr(1, ProjectConfig, "Debug", 1) Then Exit Sub
' get ver filename
Dim res_filename As String
res_filename = DTE.Solution.FullName
res_filename = Path.ChangeExtension(res_filename, ".ver")
' open VERSION FILE and increment build number
Dim msg_text As String
If File.Exists(res_filename) Then
Dim line As String
Try
Dim sr As StreamReader = New StreamReader(res_filename)
line = sr.ReadLine()
sr.Close()
Catch ex As Exception
Module1.WriteToOutputBuildPane(vbCrLf & _
"Version file read failed : " &
ex.Message & vbCrLf)
End Try
line = Right(line, line.Length - 18)
Try
Dim sw As StreamWriter = File.CreateText(res_filename)
sw.WriteLine("#define BUILD_NUM {0}", line + 1)
sw.Close()
Catch ex As Exception
Module1.WriteToOutputBuildPane(vbCrLf & _
"Version file write failed : " &
ex.Message & vbCrLf)
End Try
msg_text = "Build number : " & line + 1 & ", " & Now
Module1.WriteToOutputBuildPane(vbCrLf & msg_text & vbCrLf)
Module1.WriteToLogFile(msg_text)
Else
Try
Dim sw As StreamWriter = File.CreateText(res_filename)
sw.WriteLine("#define BUILD_NUM 1")
sw.Close()
Catch ex As Exception
Module1.WriteToOutputBuildPane(vbCrLf & _
"Version file write failed : " &
ex.Message & vbCrLf)
End Try
msg_text = "Build number : 1, " & Now
Module1.WriteToOutputBuildPane(vbCrLf & msg_text & vbCrLf)
Module1.WriteToLogFile(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 = DTE.Solution.FullName
log_filename = Path.ChangeExtension(log_filename, ".log.txt")
Try
Dim sw As StreamWriter = File.AppendText(log_filename)
sw.WriteLine(msg_text)
sw.Close()
Catch ex As Exception
MsgBox("Log file write failed : " & ex.Message)
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 = DTE.Windows.Item(EnvDTE.Constants. _
qvsWindowKindOutput)
' Create handles to the Output window and its build pane.
Dim OW As OutputWindow = win.Object
Dim OWp As OutputWindowPane
OWp = OW.OutputWindowPanes.Item("Build")
' Add a line of text to the output pane.
OWp.OutputString(msg_text & vbCrLf)
End Sub
集成
注意:如果你还没有一个Macro Project,那么先要创建一个.你需要打开一个Macro Project并显示在VS Macros IDE. (译者注:你可以通过快捷键Alt + F8,或者点击View -> Other Windows -> Macro Explorer打开宏浏览窗口.双击其中一个宏,进行编辑.)
1. 每一个宏工程拥有一个名为EnvironmentEvents的页面,双击打开它.
2. 从类名称下拉列表中选择BuildEvents.
3. 从方法名下拉列表中选择OnBuildProjConfigBegin.
4. 增加两个引用申明(import).
异常类的申明需要引用System名称空间. 文件的操作类申明需要引用System.IO.
5. 在OnBuildProjConfigBegin方法中添加.
6. 双击打开Module1页面.
Module1是IDE使用的一个默认页面名称.如果你使用了不同的名称, 那么一定要修改OnBuildProjConfigBegin中的对应名字空间.
添加两个辅助方法.:添加对 .System和.SystemIO.的引用申明.
运行
保存编辑结果,然后编译检查是否有错误.编译成功后,就可以直接使用了.使用很简单,这段代码会在每次编译时被执行.
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者