運(yùn)行環(huán)境: visual studio .net
介紹
我們每一個(gè)從原有的開發(fā)環(huán)境轉(zhuǎn)移到vs.net下的用戶,都遭遇到不少的阻礙.我所碰到的一個(gè)障礙就是:我原有的那些macro無法繼續(xù)工作了.
現(xiàn)在的這個(gè)編譯序列號(hào)自動(dòng)增長工具是從很多人的代碼片斷組合而成的,雖然它并不完善,而且缺乏一些特性,但它至少為進(jìn)一步開發(fā)提供了一個(gè)堅(jiān)實(shí)的基礎(chǔ).
目標(biāo)
這里是自動(dòng)編譯序列號(hào)的需求:
1. 在每一次release編譯的時(shí)候,自動(dòng)增加保存在一個(gè)單獨(dú)文件中的編譯序列號(hào),這個(gè)文件將被加入到我的項(xiàng)目中.
2. 自動(dòng)記錄編譯的日期和序列號(hào).
代碼分析
我的第一個(gè)任務(wù)是找到一個(gè)能夠替代vs 6中.application_beforebuildstart()的事件,vs.net的設(shè)計(jì)者為我們提供了非常容易使用的編譯事件:
onbuildbegin
onbuilddone
onbuildprojconfigbegin
onbuildprojconfigdone
這些事件句柄的命名規(guī)則有一點(diǎn)誤導(dǎo).我們不希望使用onbuildbegin,因?yàn)榧词刮覀兺瑫r(shí)針對(duì)多種配置(release, debug等等)進(jìn)行編譯,它也只會(huì)被執(zhí)行一次.這使得難以只在編譯release版本的時(shí)候增加編譯序列號(hào).onbuildprojconfigbegin就能夠在針對(duì)每一種配置編譯的時(shí)候被執(zhí)行,并且還提供一個(gè)名為projectconfig的字符串參數(shù)來描述當(dāng)前所進(jìn)行的編譯使用的是哪一種配置.
大部分的任務(wù)是在onbuildprojconfigbegin事件處理函數(shù)中完成的, 它還使用了兩個(gè)輔助macro:
writetologfile
writetooutputbuildpane
這兩個(gè)宏都可以被寫入到onbuildprojconfigbegin事件處理函數(shù)中,或者在不用時(shí)刪除.
代碼
' ------------------------------------
' 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
集成
注意! 如果你還沒有一個(gè)macro project,那么先要?jiǎng)?chuàng)建一個(gè).你需要打開一個(gè)macro project并顯示在vs macros ide. (譯者注:你可以通過快捷鍵alt + f8,或者點(diǎn)擊view -> other windows -> macro explorer打開宏瀏覽窗口.雙擊其中一個(gè)宏,進(jìn)行編輯.)
1. 每一個(gè)宏工程擁有一個(gè)名為environmentevents的頁面,雙擊打開它.
2. 從類名稱下拉列表中選擇buildevents.
3. 從方法名下拉列表中選擇onbuildprojconfigbegin.
4. 增加兩個(gè)引用申明(import).
異常類的申明需要引用system名稱空間. 文件的操作類申明需要引用system.io.
5. 在onbuildprojconfigbegin方法中添加.
6. 雙擊打開module1頁面.
module1是ide使用的一個(gè)默認(rèn)頁面名稱.如果你使用了不同的名稱, 那么一定要修改onbuildprojconfigbegin中的對(duì)應(yīng)名字空間.
添加兩個(gè)輔助方法.
添加對(duì) .system和.systemio.的引用申明.
運(yùn)行
保存編輯結(jié)果,然后編譯檢查是否有錯(cuò)誤.編譯成功后,就可以直接使用了.使用很簡單,這段代碼會(huì)在每次編譯時(shí)被執(zhí)行.
新聞熱點(diǎn)
疑難解答
圖片精選