Module: Babushka::RunHelpers
- Includes:
- LogHelpers, PathHelpers, ShellHelpers
- Included in:
- DepDefiner, DepDefiner, Renderable
- Defined in:
- lib/babushka/helpers/run_helpers.rb
Instance Method Summary
(collapse)
-
- (Object) _by_babushka
-
- (Object) added_by_babushka(nlines)
-
- (Object) append_to_file(text, file, opts = {})
-
- (Boolean) babushka_config?(path)
-
- (Object) bundle_rake(cmd, &block)
-
- (Object) change_line(line, replacement, filename)
-
- (Object) change_with_sed(keyword, from, to, file)
-
- (Object) check_file(file_name, method_name)
-
- (Object) edited_by_babushka
-
- (Object) erb_path_for(erb)
-
- (Object) grep(pattern, file)
-
- (Object) hostname
-
- (Object) insert_into_file(insert_before, path, lines, opts = {})
-
- (Object) log_and_open(message, url)
-
- (Object) mysql(cmd, username = 'root', include_password = true)
-
- (Object) rake(cmd, &block)
-
- (Object) render_erb(erb, opts = {})
-
- (Object) sed
-
- (Object) yaml(path)
#cd, #in_build_dir, #in_dir, #in_download_dir
#cmd_dir, #failable_shell, #log_shell, #login_shell, #raw_shell, #shell, #shell!, #shell?, #sudo, #which
Methods included from LogHelpers
#debug, #log, #log_block, #log_error, #log_ok, #log_verbose, #log_warn
Instance Method Details
- (Object) _by_babushka
81
82
83
|
# File 'lib/babushka/helpers/run_helpers.rb', line 81
def _by_babushka
"by babushka-#{VERSION} at #{Time.now}"
end
|
- (Object) added_by_babushka(nlines)
87
88
89
90
91
92
93
|
# File 'lib/babushka/helpers/run_helpers.rb', line 87
def added_by_babushka nlines
if nlines == 1
"This line added #{_by_babushka}"
else
"These #{nlines} lines added #{_by_babushka}"
end
end
|
- (Object) append_to_file(text, file, opts = {})
76
77
78
79
|
# File 'lib/babushka/helpers/run_helpers.rb', line 76
def append_to_file text, file, opts = {}
text = text.to_s
shell %Q{echo "\n# #{added_by_babushka(text.split("\n").length)}\n#{text.gsub('"', '\"')}" >> #{file}}, opts
end
|
- (Boolean) babushka_config?(path)
95
96
97
98
99
100
101
102
103
|
# File 'lib/babushka/helpers/run_helpers.rb', line 95
def babushka_config? path
if !path.p.exists?
unmet "the config hasn't been generated yet"
elsif !grep(/Generated by babushka/, path)
unmet "the config needs to be regenerated"
else
true
end
end
|
- (Object) bundle_rake(cmd, &block)
15
16
17
18
19
|
# File 'lib/babushka/helpers/run_helpers.rb', line 15
def bundle_rake cmd, &block
cd var(:rails_root) do
shell "bundle exec rake #{cmd} --trace RAILS_ENV=#{var :app_env}", :as => var(:username), :log => true, &block
end
end
|
- (Object) change_line(line, replacement, filename)
38
39
40
41
42
43
44
45
|
# File 'lib/babushka/helpers/run_helpers.rb', line 38
def change_line line, replacement, filename
path = filename.p
log "Patching #{path}"
shell "cat > #{path}", :as => path.owner, :input => path.readlines.map {|l|
l.gsub(/^(\s*)(#{Regexp.escape(line)})/, "\\1# #{edited_by_babushka}\n\\1# was: \\2\n\\1#{replacement}")
}.join("")
end
|
- (Object) change_with_sed(keyword, from, to, file)
65
66
67
68
69
70
|
# File 'lib/babushka/helpers/run_helpers.rb', line 65
def change_with_sed keyword, from, to, file
shell("#{sed} -ri 's/^#{keyword}\s+#{from}//' #{file}", :sudo => !File.writable?(file))
grep(/^#{keyword}\s+#{to}/, file) or shell("echo '#{keyword} #{to}' >> #{file}", :sudo => !File.writable?(file))
end
|
- (Object) check_file(file_name, method_name)
21
22
23
24
25
|
# File 'lib/babushka/helpers/run_helpers.rb', line 21
def check_file file_name, method_name
File.send(method_name, file_name).tap {|result|
log_error "#{file_name} failed #{method_name.to_s.sub(/[?!]$/, '')} check." unless result
}
end
|
- (Object) edited_by_babushka
84
85
86
|
# File 'lib/babushka/helpers/run_helpers.rb', line 84
def edited_by_babushka
"This line edited #{_by_babushka}"
end
|
- (Object) erb_path_for(erb)
126
127
128
129
130
131
132
|
# File 'lib/babushka/helpers/run_helpers.rb', line 126
def erb_path_for erb
if erb.to_s.starts_with? '/'
erb elsif load_path
File.dirname(load_path) / erb end
end
|
- (Object) grep(pattern, file)
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/babushka/helpers/run_helpers.rb', line 27
def grep pattern, file
if (path = file.p).exists?
output = if pattern.is_a? String
path.readlines.select {|l| l[pattern] }
elsif pattern.is_a? Regexp
path.readlines.grep pattern
end
output unless output.blank?
end
end
|
- (Object) hostname
7
8
9
|
# File 'lib/babushka/helpers/run_helpers.rb', line 7
def hostname
shell 'hostname -f'
end
|
- (Object) insert_into_file(insert_before, path, lines, opts = {})
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/babushka/helpers/run_helpers.rb', line 47
def insert_into_file insert_before, path, lines, opts = {}
opts.defaults! :comment_char => '#', :insert_after => nil
nlines = lines.split("\n").length
before, after = path.p.readlines.cut {|l| l.strip == insert_before.strip }
log "Patching #{path}"
if after.empty? || (opts[:insert_after] && before.last.strip != opts[:insert_after].strip)
log_error "Couldn't find the spot to write to in #{path}."
else
shell "cat > #{path}", :as => path.owner, :sudo => !File.writable?(path), :input => [
before,
added_by_babushka(nlines).start_with(opts[:comment_char] + ' ').end_with("\n"),
lines.end_with("\n"),
after
].join
end
end
|
- (Object) log_and_open(message, url)
134
135
136
137
138
|
# File 'lib/babushka/helpers/run_helpers.rb', line 134
def log_and_open message, url
log "#{message} Hit Enter to open the download page.", :newline => false
read_from_prompt ' '
shell "open #{url}"
end
|
- (Object) mysql(cmd, username = 'root', include_password = true)
140
141
142
143
|
# File 'lib/babushka/helpers/run_helpers.rb', line 140
def mysql cmd, username = 'root', include_password = true
password_segment = "--password='#{var :db_password}'" if include_password
shell "echo \"#{cmd.gsub('"', '\"').end_with(';')}\" | mysql -u #{username} #{password_segment}"
end
|
- (Object) rake(cmd, &block)
11
12
13
|
# File 'lib/babushka/helpers/run_helpers.rb', line 11
def rake cmd, &block
sudo "rake #{cmd} RAILS_ENV=#{var :app_env}", :as => var(:username), &block
end
|
- (Object) render_erb(erb, opts = {})
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/babushka/helpers/run_helpers.rb', line 110
def render_erb erb, opts = {}
if (path = erb_path_for(erb)).nil?
log_error "If you use #render_erb within a dynamically defined dep, you have to give the full path to the erb template."
elsif !File.exists?(path) && !opts[:optional]
log_error "Couldn't find erb to render at #{path}."
elsif File.exists?(path)
Renderable.new(opts[:to]).render(path, opts.merge(:context => self)).tap {|result|
if result
log "Rendered #{opts[:to]}."
else
log_error "Couldn't render #{opts[:to]}."
end
}
end
end
|
72
73
74
|
# File 'lib/babushka/helpers/run_helpers.rb', line 72
def sed
Base.host.linux? ? 'sed' : 'gsed'
end
|
- (Object) yaml(path)
105
106
107
108
|
# File 'lib/babushka/helpers/run_helpers.rb', line 105
def yaml path
require 'yaml'
YAML.load_file path.p
end
|