Module: BakerActions
- Included in:
- Baker
- Defined in:
- lib/baker/bakeractions.rb
Instance Method Summary collapse
-
#append_to_file(path, *args, &block) ⇒ Object
(also: #append_file)
Append text to end of file.
-
#format_command(command, max_line_length) ⇒ Object
Rebreak given string to fit within max_line_length for display purposes.
- #gsub_file(destination_file_name, regex, *args, &block) ⇒ Object
-
#inject_into_file(destination_file_name, *args, &block) ⇒ Object
(also: #insert_into_file)
Injects content (given either as second parameter or block) into destination file at the position specified by a regex as either :before or :after.
-
#pty_spawn(command) ⇒ Object
Spawn a PTY and run the given command.
- #rails_run(command, environment: nil) ⇒ Object
-
#sub_file(destination_file_name, regex, *args, &block) ⇒ Object
Perform substitution (once!) in a file.
- #unindent_common_whitespace(string) ⇒ Object
-
#user_for_host(host) ⇒ Object
Returns the login-user for the given host or nil if none is defined in ~/.ssh/config.
Instance Method Details
#append_to_file(path, *args, &block) ⇒ Object Also known as: append_file
Append text to end of file
Parameters
- path<String>
-
path of the file to be changed
- data<String>
-
the data to append to the file, can be also given as a block.
Example
append_to_file 'config/environments/test.rb', 'config.gem "rspec"'
append_to_file 'config/environments/test.rb' do
'config.gem "rspec"'
end
139 140 141 142 143 |
# File 'lib/baker/bakeractions.rb', line 139 def append_to_file(path, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config[:before] = /\z/ insert_into_file(path, *(args << config), &block) end |
#format_command(command, max_line_length) ⇒ Object
Rebreak given string to fit within max_line_length for display purposes
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 |
# File 'lib/baker/bakeractions.rb', line 154 def format_command(command, max_line_length) lines = command.split("\n") formatted_lines = [] lines.each do |line| position = 0 line_length = line.length starting_indent = line[/\A\s*/] current_line = '' while position < line_length last_break_position = nil start_position = position while position < line_length && current_line.length < max_line_length char = line[position] current_line += char # Check for acceptable break positions if [' ', "\t", '{', '}', '(', ')', '[', ']', '.', ',', ';', ':'].include?(char) last_break_position = position end position += 1 end if position < line_length # Line is too long, need to break if last_break_position && last_break_position >= start_position # Break at last acceptable position break_position = last_break_position + 1 # Include the break character current_line = line[start_position...break_position] position = break_position else # No acceptable break position, break at max_line_length current_line = line[start_position...position] end # Add right-aligned '\' to indicate continuation current_line = current_line.ljust(max_line_length - 1) + '\\' end formatted_lines << current_line current_line = starting_indent end end formatted_lines.join("\n") end |
#gsub_file(destination_file_name, regex, *args, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/baker/bakeractions.rb', line 26 def gsub_file(destination_file_name, regex, *args, &block) content = File.read(destination_file_name) success = content.gsub!(regex, *args, &block) if success File.open(destination_file_name, "wb") { |file| file.write(content) } puts "gsub_file successfully performed for #{destination_file_name}".green return true else puts "Match not found!".red return false end rescue Errno::ENOENT puts "The file #{destination_file_name} does not exist".red return false end |
#inject_into_file(destination_file_name, *args, &block) ⇒ Object Also known as: insert_into_file
Injects content (given either as second parameter or block) into destination file at the position specified by a regex as either :before or :after.
The data to be inserted may be given as a block, but the block isn’t passed further down to gsub, but evaluated eagerly.
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 |
# File 'lib/baker/bakeractions.rb', line 71 def inject_into_file(destination_file_name, *args, &block) begin to_insert = block_given? ? block : args.shift to_insert = to_insert.is_a?(Proc) ? to_insert.call : to_insert config = args.shift || {} # Assume :after end if no config is given config[:after] = /\z/ unless config.key?(:before) || config.key?(:after) regex = config[:before] || config[:after] regex = Regexp.escape(regex) unless regex.is_a?(Regexp) # Read the content of the file content = File.read(destination_file_name) replacement = if config.key?(:after) '\0' + to_insert else to_insert + '\0' end if config[:once] success = content.sub!(/#{regex}/, replacement) else success = content.gsub!(/#{regex}/, replacement) end # If gsub! was successful (i.e., flag was found and content replaced) if success File.open(destination_file_name, "wb") { |file| file.write(content) } puts "Content successfully inserted into #{destination_file_name}".green return true else if content.include?(to_insert) puts "Match not found, but content already exists in #{destination_file_name}. Please review and manually confirm.".blue return false else puts "Match not found!".red return false end end rescue Errno::ENOENT puts "The file #{destination_file_name} does not exist".red return false rescue => e puts "An error occurred: #{e.}".red return false end end |
#pty_spawn(command) ⇒ Object
Spawn a PTY and run the given command
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/baker/bakeractions.rb', line 205 def pty_spawn(command) require 'pty' begin PTY.spawn(command) do |stdout_and_stderr, stdin, pid| # Input Thread input_thread = Thread.new do STDIN.raw do |io| loop do break if pid.nil? begin if io.wait_readable(0.1) data = io.read_nonblock(1024) stdin.write data end rescue IO::WaitReadable # No input available right now rescue EOFError break rescue Errno::EIO break end end end end # Pipe stdout and stderr to the parser begin begin winsize = $stdout.winsize rescue Errno::ENOTTY winsize = [0, 120] # Default to 120 columns end # Ensure the child process has the proper window size, because # - tools such as yarn use it to identify tty mode # - some tools use it to determine the width of the terminal for formatting stdout_and_stderr.winsize = winsize stdout_and_stderr.each_char do |char| print char end rescue Errno::EIO # End of output end # Wait for the child process to exit Process.wait(pid) pid = nil input_thread.join return exit_status = $?.exitstatus end rescue PTY::ChildExited => e puts "The child process exited: #{e}" return 1 end end |
#rails_run(command, environment: nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/baker/bakeractions.rb', line 53 def rails_run(command, environment: nil) Tempfile.open(["rails_run", '.rb']) do |tempfile| tempfile.write(command) tempfile.flush result = pty_spawn(%Q(rails runner #{tempfile.path} -w #{environment ? "--environment #{environment}" : ""})) return result == 0 end end |
#sub_file(destination_file_name, regex, *args, &block) ⇒ Object
Perform substitution (once!) in a file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/baker/bakeractions.rb', line 7 def sub_file(destination_file_name, regex, *args, &block) content = File.read(destination_file_name) success = content.sub!(regex, *args, &block) if success File.open(destination_file_name, "wb") { |file| file.write(content) } puts "sub_file successfully performed for #{destination_file_name}".green return true else puts "Match not found!".red return false end rescue Errno::ENOENT puts "The file #{destination_file_name} does not exist".red return false end |
#unindent_common_whitespace(string) ⇒ Object
146 147 148 149 |
# File 'lib/baker/bakeractions.rb', line 146 def unindent_common_whitespace(string) common_whitespace = " " * (string.scan(/^[ ]+/).map { |s| s.length }.min || 0) string.gsub(/^#{common_whitespace}/, "") end |
#user_for_host(host) ⇒ Object
Returns the login-user for the given host or nil if none is defined in ~/.ssh/config
48 49 50 51 |
# File 'lib/baker/bakeractions.rb', line 48 def user_for_host(host) require 'net/ssh/config' Net::SSH::Config.for(host)[:user] end |