Ruby
Un article de MonWiki.
[modifier]
Exemple de script Ruby
Note : script original en Perl, porté en Ruby.
#! /usr/bin/ruby require "set" #### # Conf. prefix = ARGV[0] or fail "Please indicate a prefix, you don't want to get all the strings!" codeDir = "/cygdrive/c/Scripts/States" codeFile = "PlayerStates.lua" codeRegex = Regexp.new( "'(#{prefix}[\\w_]*)'" ) specDir = "/cygdrive/c/Base_Documentaire/StateMachine" specFile = "States.vdx" specRegex = Regexp.new( "<Text>(<.*>)*(#{prefix}.*)</Text>" ) #### # Subroutines def WriteSetToFile( set, fileName ) puts "> Wrinting #{fileName}..." File.open( fileName, "w" ) do |aFile| set.each { |string| aFile.puts( string ) } end end def Diff( name1, set1, name2, set2 ) if set1.empty? then puts "> No string found in #{name1}." return end found = false set1.each do |string| if not set2.include? string then if not found then puts "> String(s) in #{name1} & not in #{name2}:" found = true end puts " - '#{string}'" end end if not found then puts "> Strings form #{name1} are all in #{name2}." end end def FilePath( dir, file ) if dir.empty? then return file else return "#{dir}/#{file}" end end #### # Classes class StringReader def initialize( filePath, regex ) @filePath = filePath @regex = regex end def getStrings() set = Set.new File.open( @filePath ) do |aFile| aFile.each do |line| matches = @regex.match( line ) if matches then AddMatchesTo( matches, set ) end end end return set end end class CodeStringReader < StringReader private def AddMatchesTo( matches, aSet ) matches.captures.each { |match| aSet.add match } end end class SpecStringReader < StringReader private def AddMatchesTo( matches, aSet ) aSet.add matches[2] end end #### # Starting... puts "= Looking for '#{prefix}' =" puts "Reading:" puts "> Reading strings from Code..." reader = CodeStringReader.new( FilePath( codeDir, codeFile ), codeRegex ) codeStrings = reader.getStrings() puts "> Reading strings from Specs..." reader = SpecStringReader.new( FilePath( specDir, specFile ), specRegex ) specStrings = reader.getStrings() puts "Diffing:" Diff( "Code", codeStrings, "Specs", specStrings ) Diff( "Specs", specStrings , "Code", codeStrings ) #WriteSetToFile( codeStrings, "StringsFrom#{codeFile}.txt" ) #WriteSetToFile( specStrings, "StringsFrom#{specFile}.txt" ) puts "All done."