Ruby

Un article de MonWiki.

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."

Warning: main() [function.main]: open_basedir restriction in effect. File(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) is not within the allowed path(s): (/mnt/109/sdb/9/8/sroccaserra) in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) [function.main]: failed to open stream: Operation not permitted in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main() [function.include]: Failed opening '/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt' for inclusion (include_path='/mnt/109/sdb/9/8/sroccaserra/include:.:/usr/php4/lib/php') in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2