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
78
79
80
|
# File 'lib/aws/cfn/compiler/mixins/save.rb', line 37
def save_section(dir, file, format, section, hash, join='/', brick=nil)
brick ||= "brick #{hash.keys[0]}"
path = File.join(File.expand_path(dir), file)
filn = if @config[:expandedpaths]
path
else
File.join(dir, file)
end
logStep "Saving #{brick} to #{filn} "
if match = file.match(%r'\.([^\.]*)$')
if (section != '') and (not formats_compatible?(format, match[1]))
msg = "The file extension (#{match[1]}) does not match the chosen output format (#{format})!"
if @config[:force]
logger.warn msg
else
abort! "#{msg}\n\tIf you are SURE this is what you want then use the --force option!"
end
end
end
if i_am_maintainer(path) or @config[:force]
begin
File.open path, File::CREAT|File::TRUNC|File::RDWR, 0644 do |f|
case format
when /ruby|rb|yaml|yml/
f.write maintainer_comment('')
f.write hash.to_yaml line_width: 1024, indentation: 4, canonical: false
when /json|js/
f.write JSON.pretty_generate(hash, { indent: "\t", space: ' '})
else
abort! "Internal: Unsupported format #{format}. Should have noticed this earlier!"
end
f.close
end
@logger.info " saved #{filn}."
rescue
abort! "!!! Could not write file #{path}: \nException: #{$!}\nParent directory exists? #{File.directory?(File.dirname(path))}\n"
end
else
@logger.warn " Did not overwrite #{filn}."
end
end
|