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.
- #unindent_common_whitespace(string) ⇒ Object
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
94 95 96 97 98 |
# File 'lib/baker/bakeractions.rb', line 94 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
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 |
# File 'lib/baker/bakeractions.rb', line 109 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
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/baker/bakeractions.rb', line 6 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.
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 |
# File 'lib/baker/bakeractions.rb', line 30 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 success = content.gsub!(/#{regex}/, replacement) # 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 |
#unindent_common_whitespace(string) ⇒ Object
101 102 103 104 |
# File 'lib/baker/bakeractions.rb', line 101 def unindent_common_whitespace(string) common_whitespace = " " * (string.scan(/^[ ]+/).map { |s| s.length }.min || 0) string.gsub(/^#{common_whitespace}/, "") end |