25
26
27
28
29
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
|
# File 'lib/project_store/editing.rb', line 25
def edit(file_or_entity, &block)
file = case file_or_entity
when String
if File.exists? file_or_entity and File.readable? file_or_entity
file_or_entity
else
raise PSE, "Invalid file to edit '#{file_or_entity}'"
end
when ProjectStore::Entity::Base
file_or_entity.backing_store.path
end
tmp_file = Tempfile.new([self.class.name, '.yaml']).path
begin
retry_count ||= 1
FileUtils.copy file, tmp_file if retry_count == 1
edit_file tmp_file
store = YAML::Store.new(tmp_file)
store.transaction do
store.roots.each do |entity_name|
entity = store[entity_name]
setup_entity! entity_name, entity, &block
entity.valid_to_save? raise_exception: true
end
end
FileUtils.copy tmp_file, file
logger.info "File '#{file}' updated successfully."
file
rescue StandardError => e
retry_count += 1
retry unless retry_count > nb_max_edit_retries
raise e
ensure
if retry_count > nb_max_edit_retries
logger.error "Keeping file '#{ERROR_FILE_KEPT}' which was invalid !"
FileUtils.copy tmp_file, ERROR_FILE_KEPT
end
File.unlink tmp_file
end
end
|