用 Ruby 查找 workspace 里 project 的依赖关系
2008-2-14 21:57 | by 2ndboy用 VC 6 做开发,项目一大起来,workspace 里 project 间的依赖关系就比较复杂,尤其是历史比较长,上手的人比较多的项目,之前用 PHP 实现了一个查找 VC 6 项目依赖关系的程序,但是总感觉别人用起来准备环境不太方便,于是假期里用 Ruby 重新实现了一遍。
- if ARGV.length <= 0
- print 'Please enter filename of DSW: '
- dsw = Kernel.gets.rstrip
- elsif ARGV.length >= 1
- if '-h' == ARGV[0] || 'help' == ARGV[0] || '--help' == ARGV[0]
- puts 'Usage: ruby ' + $0 + ' dsw_filename'
- exit
- else
- dsw = ARGV[0]
- end
- end
- rePath = Regexp.new( '(.*(?:/|\\\\)).*' )
- reProject = Regexp.new( 'Project: "([^"]*)"="?([^"]*)"? - ' )
- reTargType = Regexp.new( '# TARGTYPE ".*" 0x(\d+)' )
- reConfig = Regexp.new( '"\$\(CFG\)" == "[^"]* - Win32 ([^"]*)"' )
- reOut = Regexp.new( '/out:"[^"]*(?:/|\\\\)([^"]*\.[^"]*)"' )
- reLink = Regexp.new( '# ADD LINK32 ([^/]*)' )
- mdPath = rePath.match( dsw )
- if nil != mdPath
- basePath = mdPath[1]
- end
- projects = Hash.new
- if !File.readable_real?( dsw )
- puts "Can't open file - " + dsw
- exit
- end
- fileDsw = open( dsw, 'r' )
- fileDsw.each_line{ |line|
- mdProject = reProject.match( line )
- if nil != mdProject
- projectName = mdProject[1]
- projects[projectName] = Hash.new
- projects[projectName]['dsp'] = mdProject[2]
- projects[projectName]['linkRelease'] = ''
- projects[projectName]['linkDebug'] = ''
- config = ''
- fileDsp = open( basePath + mdProject[2], "r" )
- fileDsp.each_line{ |line|
- mdTargType = reTargType.match( line )
- if nil != mdTargType
- case mdTargType[1]
- when '0101'
- projects[projectName]['extName'] = '.exe' # Win32 (x86) Application
- when '0102'
- projects[projectName]['extName'] = '.dll' # Win32 (x86) Dynamic-Link Library
- when '0103'
- projects[projectName]['extName'] = '.exe' # Win32 (x86) Console Application
- when '0104'
- projects[projectName]['extName'] = '.lib' # Win32 (x86) Static Library
- else
- puts mdTargType
- end
- projects[projectName]['outRelease'] = projectName.downcase + projects[projectName]['extName']
- projects[projectName]['outDebug'] = projects[projectName]['outRelease']
- next
- end
- mdConfig = reConfig.match( line )
- if nil != mdConfig
- config = mdConfig[1]
- next
- end
- mdOut = reOut.match( line )
- if nil != mdOut
- if 'Release' == config
- projects[projectName]['outRelease'] = mdOut[1].downcase
- else
- projects[projectName]['outDebug'] = mdOut[1].downcase
- end
- # next # Can't call next here
- end
- mdLink = reLink.match( line )
- if nil != mdLink
- if 'Release' == config
- projects[projectName]['linkRelease'] = mdLink[1].downcase
- else
- projects[projectName]['linkDebug'] = mdLink[1].downcase
- end
- next
- end
- }
- fileDsp.close()
- end
- }
- fileDsw.close()
- projects.each{ |key, value|
- if '.lib' == value['extName']
- next
- end
- out = ''
- projects.each{ |key2, value2|
- if value['linkDebug'].include?( value2['outDebug'] )
- out += ( ' ' + key2 + ' - ' + value2['outDebug'] + "\n" )
- end
- }
- if !out.empty?
- puts 'Project: ' + key + ' - ' + value['outDebug']
- puts out
- end
- }
核心的部分用正则表达式实现,现在总觉得越是在实用中尝试使用正则表达式就越能感受到它的强大!

