forked from amarshall/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rbrc
213 lines (174 loc) · 4.99 KB
/
rbrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'rubygems/requirement'
require 'rubygems/version'
module Unbundler
def self.require_external_gem gem_name
require gem_name
rescue LoadError => ex
try_external_require(gem_name) || raise(ex)
end
def self.try_external_require gem_name
gem_path = path_for_gem(gem_name)
return false unless gem_path
$LOAD_PATH << "#{gem_path}/lib"
require gem_name
end
def self.gem_dir
regex = %r((.*?/ruby/(?:gems/)?\d+\.\d+\.\d+/gems/))
some_gem_dir = $LOAD_PATH.grep(regex).first
return unless some_gem_dir
some_gem_dir.match(regex).captures[0]
end
def self.path_for_gem gem_name
gem_dir = self.gem_dir
return nil unless gem_dir
gem_path = Dir["#{gem_dir}/*"].to_a.detect do |gem_path|
File.basename(gem_path).gsub(/-(\d\.?)+$/, '') == gem_name
end
end
end
def command_exists? command
system({ 'PATH' => ENV['PATH'] }, "bash --login --noprofile -c 'type #{command} > /dev/null 2> /dev/null'")
end
def current_ruby_version
if Gem::Requirement.new('>= 2.1.0').satisfied_by?(Gem::Version.new(RUBY_VERSION))
RUBY_VERSION
else
"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
end
end
ruby_version_detector = Class.new do
chruby = Module.new do
define_singleton_method(:match?) { command_exists? 'chruby' }
define_singleton_method(:name) { 'chruby' }
define_singleton_method(:version) { current_ruby_version }
end
rbenv = Module.new do
define_singleton_method(:match?) { command_exists? 'rbenv' }
define_singleton_method(:name) { 'rbenv' }
define_singleton_method(:version) { `rbenv version`.strip }
end
rvm = Module.new do
define_singleton_method(:match?) { command_exists? 'rvm' }
define_singleton_method(:name) { 'RVM' }
define_singleton_method(:version) { `rvm current`.split("\n").last }
end
version_managers = [chruby, rbenv, rvm]
define_method(:real_version) { current_ruby_version }
define_method(:manager) { version_managers.detect(&:match?) }
define_method(:bypassed?) do
manager.version.match(/\d+\.\d+\.\d+(-?p\d+)?/).to_s != real_version
end
end
def print_ruby_version version_detector
begin
Unbundler.require_external_gem 'paint'
rescue LoadError
end
manager = version_detector.manager.name
version = version_detector.manager.version
if version =~ /system/
system_raw_version = `$(whereis ruby) --version`
system_major = system_raw_version.match(/ruby (\d+\.\d+\.\d+)/).captures.first
system_patch = system_raw_version.match(/patchlevel (\d+)/)
system_patch &&= system_patch.captures.first
system_combined = system_patch ? "system (#{system_major}-p#{system_patch})" : "system (#{system_major})"
version = version.sub(/system/, system_combined)
end
if version_detector.bypassed?
manager ||= 'built-in ruby'
manager = "(#{manager} bypassed)"
version = version_detector.real_version
end
version_str = "#{manager} using #{version}"
if defined?(Paint)
puts Paint[version_str, :bold, :underline]
else
puts version_str
end
end
def cli?
is_pry_cli = defined?(Pry) && Pry.cli
is_rails_console = defined?(Rails::Console)
is_other_console = $PROGRAM_NAME =~ /console/
is_irb = $PROGRAM_NAME == 'irb'
is_pry_cli || is_rails_console || is_other_console || is_irb
end
print_ruby_version(ruby_version_detector.new) if cli?
class Array
def self.toy n = 10
Array.new(n) { |i| i }.shuffle
end
end
class Hash
def self.toy n = 5
keys = ('a'..'z').to_a.take(n).shuffle
values = Array.toy(n)
Hash[keys.zip(values)]
end
end
module Clipboard
module Pasteboard
def self.match?
command_exists?('pbcopy') && command_exists?('pbpaste')
end
def copy object
IO.popen('pbcopy', 'w') { |f| f << object.to_s }
$?.exited?
end
def paste
`pbpaste`
end
end
module Xclip
def self.match?
command_exists? 'xclip'
end
def copy object
IO.popen('xclip -selection clipboard', 'w') { |f| f << object.to_s }
$?.exited?
end
def paste
`xclip -selection clipboard -o`
end
end
module Xsel
def self.match?
command_exists? 'xsel'
end
def copy object
IO.popen('xsel --clipboard --input', 'w') { |f| f << object.to_s }
$?.exited?
end
def paste
`xsel --clipboard --output`
end
end
STRATEGIES = [Pasteboard, Xclip, Xsel]
def self.included klass
return unless capable?
klass.send :include, strategy
end
def self.capable?
!!strategy
end
def self.strategy
@strategy ||= STRATEGIES.detect(&:match?)
end
end
singleton_class.send :include, Clipboard if Clipboard.capable?
# Load if in Rails console
if ($0 == 'irb' && ENV['RAILS_ENV']) || ($0 == 'script/rails' && Rails.env) || (defined?(Rails) && Rails.env)
def change_log(stream)
if defined? ActiveRecord::Base
ActiveRecord::Base.logger = Logger.new stream
ActiveRecord::Base.clear_active_connections!
end
end
def show_log
change_log STDOUT
end
def hide_log
change_log nil
end
change_log STDOUT
end