Class: Geordi::Util
Class Method Summary
collapse
announce, fail, note, note_cmd, prompt, strip_heredoc, success, warn
Class Method Details
.capistrano3? ⇒ Boolean
115
116
117
|
# File 'lib/geordi/util.rb', line 115
def capistrano3?
Util.file_containing? 'Capfile', 'capistrano/setup'
end
|
.cmd_exists?(cmd) ⇒ Boolean
check if given cmd is executable. Absolute path or command in $PATH allowed.
95
96
97
98
|
# File 'lib/geordi/util.rb', line 95
def cmd_exists? cmd
system("which #{cmd} > /dev/null")
return $?.exitstatus.zero?
end
|
.console_command(environment) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/geordi/util.rb', line 57
def console_command(environment)
if File.exists?('script/console')
'script/console ' + environment
else
'bundle exec rails console ' + environment
end
end
|
.current_branch ⇒ Object
73
74
75
|
# File 'lib/geordi/util.rb', line 73
def current_branch
`git rev-parse --abbrev-ref HEAD`.strip
end
|
.decide_texteditor ⇒ Object
try to guess user’s favorite cli text editor
84
85
86
87
88
89
90
91
92
|
# File 'lib/geordi/util.rb', line 84
def decide_texteditor
%w[$VISUAL $EDITOR /usr/bin/editor vi].each do |texteditor|
if cmd_exists? texteditor and texteditor.start_with? '$'
return ENV[texteditor[1..-1]]
elsif cmd_exists? texteditor
return texteditor
end
end
end
|
.deploy_targets ⇒ Object
77
78
79
80
81
|
# File 'lib/geordi/util.rb', line 77
def deploy_targets
Dir['config/deploy/*'].map do |f|
File.basename f, '.rb'
end
end
|
.file_containing?(file, regex) ⇒ Boolean
119
120
121
|
# File 'lib/geordi/util.rb', line 119
def file_containing?(file, regex)
File.exists?(file) and File.read(file).scan(regex).any?
end
|
.installing_missing_gems(&block) ⇒ Object
Geordi commands sometimes require external gems. However, we don’t want all employed gems as runtime dependencies because that would unnecessarily slow down all commands. Thus, we have this handy method here.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/geordi/util.rb', line 13
def installing_missing_gems(&block)
yield
rescue LoadError => error
error.message =~ /-- (\S+)\Z/
$1 or raise
gem_name = $1.strip.split('/').first
install_command = 'gem install ' + gem_name
warn 'Probably missing gem: ' + gem_name
prompt('Install it now?', 'y', /y|yes/) or fail 'Missing Gems.'
system! install_command, :show_cmd => true
Gem.clear_paths
note 'Retrying ...'
require gem_name
retry
end
|
.is_port_open?(port) ⇒ Boolean
100
101
102
103
104
105
106
107
108
|
# File 'lib/geordi/util.rb', line 100
def is_port_open?(port)
begin
socket = TCPSocket.new('127.0.0.1', port)
socket.close
return true
rescue Errno::ECONNREFUSED
return false
end
end
|
.server_command ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/geordi/util.rb', line 65
def server_command
if File.exists?('script/server')
'script/server ""'
else
'bundle exec rails server'
end
end
|
.stripped_lines(input_string) ⇒ Object
splint lines e.g. read from a file into lines and clean those up
111
112
113
|
# File 'lib/geordi/util.rb', line 111
def stripped_lines(input_string)
input_string.lines.map(&:chomp).map(&:strip)
end
|
.system!(*commands) ⇒ Object
Run a command with a clean environment. Print an error message and exit if the command fails.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/geordi/util.rb', line 40
def system!(*commands)
options = commands.last.is_a?(Hash) ? commands.pop : {}
note_cmd commands.join(' ') if options[:show_cmd]
if options[:confirm]
prompt('Run this now?', 'n', /y|yes/) or fail('Cancelled.')
end
if ENV['GEORDI_TESTING']
puts "Util.system! #{ commands.join ', ' }"
else
success = defined?(Bundler) ? Bundler.clean_system(*commands) : system(*commands)
success or fail(options[:fail_message] || 'Something went wrong.')
end
end
|