Skip to content

Commit

Permalink
add quix utils
Browse files Browse the repository at this point in the history
  • Loading branch information
quix committed Aug 29, 2008
1 parent b73466f commit f4ee111
Show file tree
Hide file tree
Showing 26 changed files with 1,209 additions and 0 deletions.
16 changes: 16 additions & 0 deletions contrib/quix/Rakefile
@@ -0,0 +1,16 @@
$LOAD_PATH.unshift "./lib"

require 'quix/simple_installer'
require 'quix/config'

task :test do
load './test/all.rb'
end

task :install do
Quix::SimpleInstaller.new.install
end

task :uninstall do
Quix::SimpleInstaller.new.uninstall
end
3 changes: 3 additions & 0 deletions contrib/quix/install.rb
@@ -0,0 +1,3 @@
$LOAD_PATH.unshift "./lib"
require 'quix/simple_installer'
Quix::SimpleInstaller.new.run
15 changes: 15 additions & 0 deletions contrib/quix/lib/quix.rb
@@ -0,0 +1,15 @@

root = File.dirname(__FILE__)
pkgname = File.basename(__FILE__).sub(%r!\.rb\Z!, "")

Dir["#{root}/#{pkgname}/**/*.rb"].map { |file|
# change to relative paths
file.
sub(%r!\A#{root}/!, "").
sub(%r!\.rb\Z!, "")
}.reject { |file|
file =~ %r!builtin! or
(file =~ %r!cygwin! and RUBY_PLATFORM !~ %r!cygwin!)
}.each { |file|
require file
}
7 changes: 7 additions & 0 deletions contrib/quix/lib/quix/builtin/dir/casefold_brackets.rb
@@ -0,0 +1,7 @@

class << Dir
remove_method :[]
def [](pattern)
Dir.glob(pattern, File::FNM_CASEFOLD)
end
end
9 changes: 9 additions & 0 deletions contrib/quix/lib/quix/builtin/kernel/tap.rb
@@ -0,0 +1,9 @@

unless respond_to? :tap
module Kernel
def tap
yield self
self
end
end
end
21 changes: 21 additions & 0 deletions contrib/quix/lib/quix/builtin/module/include.rb
@@ -0,0 +1,21 @@

if $DEBUG and !(defined?($NO_DEBUG_INCLUDE) and $NO_DEBUG_INCLUDE)
class Module
orig_include = instance_method(:include)
remove_method(:include)
define_method(:include) { |*mods|
mods.each { |mod|
if mod.class == Module
mod.instance_methods(true).each { |name|
if self.instance_methods(true).include?(name)
STDERR.puts("Note: replacing #{self.inspect}##{name} " +
"with #{mod.inspect}##{name}")
end
}
end
orig_include.bind(self).call(*mods)
}
}
end
end

41 changes: 41 additions & 0 deletions contrib/quix/lib/quix/builtin/module/private.rb
@@ -0,0 +1,41 @@

class Module
alias_method :private__original, :private
def private(*args, &block)
private__original(*args)
if block
singleton_class = (class << self ; self ; end)
caller_self = block.binding.eval("self")
method_added__original =
if (t = method(:method_added)) and t.owner == singleton_class
t
else
nil
end
begin
singleton_class.instance_eval {
define_method(:method_added) { |name|
caller_self.instance_eval {
private__original(name.to_sym)
}
if t = method_added__original
t.call(name)
end
}
}
block.call
ensure
if t = method_added__original
t.owner.instance_eval {
define_method(:method_added, t)
}
else
singleton_class.instance_eval {
remove_method(:method_added)
}
end
end
end
end
end

37 changes: 37 additions & 0 deletions contrib/quix/lib/quix/config.rb
@@ -0,0 +1,37 @@

require 'rbconfig'

module Quix
module Config
CONFIG = ::Config::CONFIG

def ruby_executable
File.join(CONFIG["bindir"], CONFIG["RUBY_INSTALL_NAME"])
end

def version_gt(version) ; version_compare( :>, version) ; end
def version_lt(version) ; version_compare( :<, version) ; end
def version_eq(version) ; version_compare(:==, version) ; end
def version_ge(version) ; version_compare(:>=, version) ; end
def version_le(version) ; version_compare(:<=, version) ; end
def version_ne(version) ; version_compare(:"!=", version) ; end

def version_compare(op, version)
major, minor, teeny =
version.split(".").map { |n| n.to_i }

this_major, this_minor, this_teeny =
%w(MAJOR MINOR TEENY).map { |v| CONFIG[v].to_i }

if this_major == major and this_minor == minor
this_teeny.send(op, teeny)
elsif this_major == major
this_minor.send(op, minor)
else
this_major.send(op, major)
end
end

extend self
end
end
60 changes: 60 additions & 0 deletions contrib/quix/lib/quix/cygwin.rb
@@ -0,0 +1,60 @@

unless RUBY_PLATFORM =~ %r!cygwin!
raise NotImplementedError, "cygwin-only module"
end

require 'fileutils'
require 'thread'

module Quix
module Cygwin
def run_batchfile(file, *args)
dos_pwd_env {
sh("cmd", "/c", dos_path(file), *args)
}
end

def normalize_path(path)
path.sub(%r!/+\Z!, "")
end

def unix2dos(string)
string.
gsub("\n", "\r\n").
gsub(%r!\r+!, "\r")
end

def dos_path(unix_path)
`cygpath -w #{normalize_path(unix_path)}`.chomp
end

def unix_path(dos_path)
escaped_path = dos_path.sub(%r!\\+\Z!, "").gsub("\\", "\\\\\\\\")
`cygpath #{escaped_path}`.chomp
end

def dos_pwd_env
Thread.exclusive {
orig = ENV["PWD"]
ENV["PWD"] = dos_path(Dir.pwd)
begin
yield
ensure
ENV["PWD"] = orig
end
}
end

def avoid_dll(file)
temp_file = file + ".avoiding-link"
FileUtils.mv(file, temp_file)
begin
yield
ensure
FileUtils.mv(temp_file, file)
end
end

extend self
end
end
44 changes: 44 additions & 0 deletions contrib/quix/lib/quix/diagnostic.rb
@@ -0,0 +1,44 @@

require 'quix/builtin/kernel/tap'

module Quix
module Diagnostic
def show(desc = nil, stream = STDOUT, &block)
if desc
stream.puts(desc)
end
if block
expression = block.call
eval(expression, block.binding).tap { |result|
stream.printf("%-16s => %s\n", expression, result.inspect)
}
end
end

if $DEBUG
def debug
yield
end

def debugging?
true
end

def trace(desc = nil, &block)
if desc
show("#{desc}.".sub(%r!\.\.+\Z!, ""), STDERR, &block)
else
show(nil, STDERR, &block)
end
end
else
# non-$DEBUG
def debug ; end
def debugging? ; end
def trace(*args) ; end
end

extend self
end
end

33 changes: 33 additions & 0 deletions contrib/quix/lib/quix/enumerable.rb
@@ -0,0 +1,33 @@

require 'quix/builtin/kernel/tap'

module Quix
module Enumerable
def inject_with_index(*args)
index = 0
inject(*args) { |acc, elem|
yield(acc, elem, index).tap {
index += 1
}
}
end

def map_with_index
Array.new.tap { |result|
each_with_index { |elem, index|
result << yield(elem, index)
}
}
end

def select_with_index
Array.new.tap { |result|
each_with_index { |elem, index|
if yield(elem, index)
result << elem
end
}
}
end
end
end
44 changes: 44 additions & 0 deletions contrib/quix/lib/quix/fileutils.rb
@@ -0,0 +1,44 @@

require 'tmpdir'
require 'quix/builtin/kernel/tap'

module Quix
module FileUtils
#::FileUtils.methods(false).each { |method|
# begin
# public method.to_sym
# rescue
# end
#}

def rename_file(file, new_name)
#
# For case-insensitive systems, we must move the file elsewhere
# before changing case.
#
temp = File.join(Dir.tmpdir, File.basename(file))
::FileUtils.mv(file, temp)
begin
::FileUtils.mv(temp, new_name)
rescue
::FileUtils.mv(temp, file)
raise
end
end

def replace_file(file)
old_contents = File.read(file)
yield(old_contents).tap { |new_contents|
File.open(file, "w") { |output|
output.print(new_contents)
}
}
end

def stem(file)
file.sub(%r!#{File.extname(file)}\Z!, "")
end

extend self
end
end
27 changes: 27 additions & 0 deletions contrib/quix/lib/quix/hash_struct.rb
@@ -0,0 +1,27 @@

require 'quix/builtin/kernel/tap'
require 'ostruct'

module Quix
class HashStruct < OpenStruct
def method_missing(sym, *args, &block)
if table.respond_to? sym
table.send(sym, *args, &block)
else
super
end
end

class << self
def recursive_new(hash)
new.tap { |s|
hash.each_pair { |key, value|
s.send(
:"#{key}=",
value.is_a?(Hash) ? recursive_new(value) : value)
}
}
end
end
end
end

0 comments on commit f4ee111

Please sign in to comment.