module PowerStencil::Utils::FileEdit
Constants
- EDITOR_ENVIRONMENT_VARIABLE
- LAST_EDITED_FILE
Attributes
editor[W]
nb_max_edit_retries[W]
Public Instance Methods
editor()
click to toggle source
# File lib/power_stencil/utils/file_edit.rb, line 16 def editor default = config[:editor] || ENV[EDITOR_ENVIRONMENT_VARIABLE] @editor ||= default end
nb_max_edit_retries()
click to toggle source
# File lib/power_stencil/utils/file_edit.rb, line 21 def nb_max_edit_retries @nb_max_edit_retries ||= config[:max_file_edit_retry_times] end
securely_edit_entity(entity, &modifications_validation_block)
click to toggle source
# File lib/power_stencil/utils/file_edit.rb, line 25 def securely_edit_entity(entity, &modifications_validation_block) initial_uri = entity.source_uri modified_entity = nil tmpfile = Tempfile.create(["#{entity.type.to_s}_#{entity.name.to_s}", '.yaml']) tmpfile_path = tmpfile.path begin tmpfile.puts entity.to_yaml tmpfile.flush ensure tmpfile.close end securely_edit_file(tmpfile_path, &modifications_validation_block) modified_entity = UniverseCompiler::Entity::Persistence.load tmpfile_path modified_entity.source_uri = initial_uri return modified_entity ensure tmpfile.close File.unlink tmpfile end
securely_edit_file(file, &modifications_validation_block)
click to toggle source
# File lib/power_stencil/utils/file_edit.rb, line 45 def securely_edit_file(file, &modifications_validation_block) unless File.exists? file and File.writable? file raise PowerStencil::Error, "Cannot edit file '#{file}' !" end ext = File.extname file base = File.basename file, ext tmp_file = Tempfile.create([base, ext]) tmp_file_path = tmp_file.path tmp_file.close retry_count = 1 begin FileUtils.copy file, tmp_file_path if retry_count == 1 edit_file tmp_file_path modifications_validation_block.call(tmp_file_path, file) if block_given? FileUtils.copy tmp_file_path, file logger.info "File '#{file}' updated successfully." file rescue StandardError => e remaining_attempts = nb_max_edit_retries - retry_count retry_count += 1 msg = "Changes invalid: '#{e.message}'." if remaining_attempts > 0 msg << " #{remaining_attempts} remaining attempt" if remaining_attempts == 1 msg << '.' else msg << 's.' end end puts_and_logs msg, logs_as: :error, check_verbose: false if retry_count > nb_max_edit_retries begin FileUtils.copy tmp_file_path, LAST_EDITED_FILE puts_and_logs "Your modifications were kept in '#{LAST_EDITED_FILE}' as it was invalid !", logs_as: :error, check_verbose: false rescue => ue logger.error 'Ooops, something strange happened while saving your modifications. Cannot keep your modifications !' logger.debug PowerStencil::Error.report_error(ue) end else retry end raise e ensure File.unlink tmp_file_path end end
Private Instance Methods
edit_file(file)
click to toggle source
# File lib/power_stencil/utils/file_edit.rb, line 94 def edit_file(file) raise PowerStencil::Error, 'No editor specified' if editor.nil? logger.debug "Editing file '#{file}', using editor '#{editor}'" system "#{editor} '#{file}'" end