class Object

Public Instance Methods

assert( condition, msg, error_type = RuntimeError ) click to toggle source
# File lib/ult/assert.rb, line 1
def assert( condition, msg, error_type = RuntimeError )
  raise( error_type, msg, caller( 1 ) ) if ! condition
end
copy( src, dst ) click to toggle source
# File lib/ult/copy.rb, line 4
def copy( src, dst )
  dir?( src ) ? FileUtils.cp_r( src, dst ) : FileUtils.copy( src, dst )
end
dir( path, &block ) click to toggle source
# File lib/ult/dir.rb, line 22
def dir( path, &block )
  Dir.chdir( path, &block )
end
dir?( path ) click to toggle source
# File lib/ult/dir.rb, line 4
def dir?( path )
  Dir.exist?( path )
end
direach( path ) { |name, fullpath( name, path )| ... } click to toggle source
# File lib/ult/dir.rb, line 12
def direach( path )
  Dir::foreach( path ){|name|
    case name
    when ".", ".."
    else
      yield( name, fullpath( name, path ) )
    end
  }
end
dirname( path ) click to toggle source
# File lib/ult/dir.rb, line 8
def dirname( path )
  File.dirname( path )
end
execute( command, input = "", &block ) click to toggle source
# File lib/ult/execute.rb, line 1
def execute( command, input = "", &block )
  status = -1
  block = lambda{|type, io, msg| io.print msg} if ! block_given?
  begin
    i_r, i_w = IO.pipe
    o_r, o_w = IO.pipe
    e_r, e_w = IO.pipe
    
    pid = Process.fork{
      i_w.close
      STDIN.reopen( i_r )
      STDOUT.reopen( o_w )
      STDERR.reopen( e_w )
      exec( command )
    }
    o_w.close
    e_w.close
    i_r.close
    i_w.write input
    i_w.close
    
    # IOブロックによるプロセス停止を防ぐため、スレッドで定期的に読み取る
    block.call( :cmd, $stdout, "#{command}\n" )
    out_thread = Thread.start{
      chars = []
      o_r.each_char{|c|
        chars.push c
        case c
        when /[\r\n]/
          block.call( :out, $stdout, chars.join )
          chars = []
        end
      }
      block.call( :out, $stdout, chars.join ) if ! chars.empty?
    }
    err_thread = Thread.start{
      chars = []
      e_r.each_char{|c|
        chars.push c
        case c
        when /[\r\n]/
          block.call( :err, $stderr, chars.join )
          chars = []
        end
      }
      block.call( :err, $stderr, chars.join ) if ! chars.empty?
    }
    
    Process.waitpid( pid )
    status = $?.exitstatus
    out_thread.join
    err_thread.join
  end
  status
end
extname( path ) click to toggle source
# File lib/ult/file.rb, line 9
def extname( path )
  /^\.?.+?\.(.+)$/ =~ path ? $1 : ""
end
file?( path ) click to toggle source
# File lib/ult/file.rb, line 1
def file?( path )
  File.exist?( path )
end
filename( path ) click to toggle source
# File lib/ult/file.rb, line 5
def filename( path )
  File.basename( path )
end
find( pattern ) click to toggle source
# File lib/ult/find.rb, line 1
def find( pattern )
  Dir.glob( pattern )
end
fullpath( name, dir = "." ) click to toggle source
# File lib/ult/path.rb, line 1
def fullpath( name, dir = "." )
  File.expand_path( name, dir )
end
linux?() click to toggle source
# File lib/ult/platform.rb, line 9
def linux?
  ! platform.match( /linux/ ).nil?
end
mac?() click to toggle source
# File lib/ult/platform.rb, line 5
def mac?
  ! platform.match( /darwin/ ).nil?
end
mkdir( path, &block ) click to toggle source
# File lib/ult/dir.rb, line 30
def mkdir( path, &block )
  FileUtils.mkdir_p( path ) if ! dir?( path )
  dir( path, &block )
end
mkfile( path, mode, &block ) click to toggle source
# File lib/ult/file.rb, line 13
def mkfile( path, mode, &block )
  open( path, mode ){|file|
    block.call( file ) if block_given?
  }
end
option( *args, &block ) click to toggle source
# File lib/ult/option.rb, line 3
def option( *args, &block )
  OptionParser.new( *args ).instance_eval( &block )
end
platform() click to toggle source
# File lib/ult/platform.rb, line 1
def platform
  RUBY_PLATFORM
end
pwd() click to toggle source
# File lib/ult/dir.rb, line 26
def pwd
  Dir.pwd
end
remove( path ) click to toggle source
# File lib/ult/remove.rb, line 4
def remove( path )
  dir?( path ) ? rmdir( path ) : rmfile( path )
end
replica( count, prefix, suffix = "" ) click to toggle source
# File lib/ult/replica.rb, line 6
def replica( count, prefix, suffix = "" )
  src = "#{prefix}#{suffix}"
  if dir?( src )
    count.times{|index|
      dst = "#{prefix}#{index + 1}#{suffix}"
      remove( dst )
      copy( src, dst )
    }
  elsif file?( src )
    count.times{|index|
      dst = "#{prefix}#{index + 1}#{suffix}"
      remove( dst )
      copy( src, dst )
    }
  end
end
rmdir( path ) click to toggle source
# File lib/ult/dir.rb, line 35
def rmdir( path )
  FileUtils.rm_rf( path ) if dir?( path )
end
rmfile( path ) click to toggle source
# File lib/ult/file.rb, line 19
def rmfile( path )
  File.delete( path ) if file?( path )
end
rmkdir( path ) click to toggle source
# File lib/ult/dir.rb, line 39
def rmkdir( path )
  rmdir( path )
  mkdir( path )
end
safety( &block ) click to toggle source
# File lib/ult/safety.rb, line 3
def safety( &block )
  root_dir = pwd
  begin
    block.call
  rescue Exception => e
    dir( root_dir )
    raise e
  rescue StandardError => e
    dir( root_dir )
    raise e
  end
  dir( root_dir )
end
shell( command, &block ) click to toggle source
# File lib/ult/execute.rb, line 57
def shell( command, &block )
  status = execute( command )
  if block_given?
    status = block.call( status )
  else
    raise "#{command} => #{status}" if 0 != status
  end
  status
end
watchdog( command, input = "", &block ) click to toggle source
# File lib/ult/watchdog.rb, line 3
def watchdog( command, input = "", &block )
  loop{
    status, outputs, errors = execute( command, input )
    break if ! block.call( status, outputs, errors )
  }
end
windows?() click to toggle source
# File lib/ult/platform.rb, line 13
def windows?
  mac? ? false : ! platform.match( /win/ ).nil?
end