Module: Aws::Cfn::Dsl::PrettyPrint
- Included in:
- Base
- Defined in:
- lib/aws/cfn/dsl/mixins/prettyprint.rb
Class Method Summary collapse
Instance Method Summary collapse
- #fmt(val) ⇒ Object
- #fmt_key(s) ⇒ Object
- #fmt_string(s) ⇒ Object
-
#is_array_of_strings_hack(val) ⇒ Object
Emo-specific hacks to force the desired output formatting.
-
#is_multi_line_hack(val) ⇒ Object
Emo-specific hacks to force the desired output formatting.
- #is_single_line(val) ⇒ Object
-
#is_single_line_hack(val) ⇒ Object
Emo-specific hacks to force the desired output formatting.
- #pprint(val) ⇒ Object
- #pprint_cfn_resource(name, options, brick = true) ⇒ Object
- #pprint_cfn_section(section, name, options, subdir, brick = true) ⇒ Object
- #pprint_cfn_template(tpl) ⇒ Object
- #pprint_value(val, indent) ⇒ Object
- #prelude_code(indent = ' ') ⇒ Object
- #print_with_wrapper(code, indent = ' ') ⇒ Object
Class Method Details
.included(includer) ⇒ Object
289 290 291 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 289 def self.included(includer) end |
Instance Method Details
#fmt(val) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 253 def fmt(val) if val == {} '{}' elsif val == [] '[]' elsif val.is_a?(Hash) '{ ' + (val.map { |k,v| fmt_key(k) + ' => ' + fmt(v) }).join(', ') + ' }' elsif val.is_a?(Array) && is_array_of_strings_hack(val) '%w(' + val.join(' ') + ')' elsif val.is_a?(Array) '[ ' + (val.map { |v| fmt(v) }).join(', ') + ' ]' elsif val.is_a?(FnCall) && val.arguments.empty? val.name elsif val.is_a?(FnCall) val.name + '(' + (val.arguments.map { |v| fmt(v) }).join(', ') + ')' elsif val.is_a?(String) fmt_string(val) elsif val == nil 'null' else val.to_s # number, boolean end end |
#fmt_key(s) ⇒ Object
277 278 279 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 277 def fmt_key(s) ':' + (/^[a-zA-Z_]\w+$/ =~ s ? s : fmt_string(s)) # returns a symbol like :Foo or :'us-east-1' end |
#fmt_string(s) ⇒ Object
281 282 283 284 285 286 287 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 281 def fmt_string(s) if /[^ -~]/ =~ s s.dump # contains, non-ascii or control char, return double-quoted string else '\'' + s.gsub(/([\\'])/, '\\\\\1') + '\'' # return single-quoted string, escape \ and ' end end |
#is_array_of_strings_hack(val) ⇒ Object
Emo-specific hacks to force the desired output formatting
246 247 248 249 250 251 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 246 def is_array_of_strings_hack(val) val.is_a?(Array) && val.all? { |v| v.is_a?(String) } && val.grep(/\s/).empty? && ( val.include?('autoscaling:EC2_INSTANCE_LAUNCH') || val.include?('m1.small') ) end |
#is_multi_line_hack(val) ⇒ Object
Emo-specific hacks to force the desired output formatting
241 242 243 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 241 def is_multi_line_hack(val) val.is_a?(Hash) && val['email'] end |
#is_single_line(val) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 223 def is_single_line(val) if val.is_a?(Hash) is_single_line(val.values) elsif val.is_a?(Array) val.empty? || (val.length == 1 && is_single_line(val[0]) && !val[0].is_a?(Hash)) || val.all? { |v| v.is_a?(String) } else true end end |
#is_single_line_hack(val) ⇒ Object
Emo-specific hacks to force the desired output formatting
236 237 238 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 236 def is_single_line_hack(val) is_array_of_strings_hack(val) end |
#pprint(val) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 6 def pprint(val) logStep "Pretty print ..." case detect_type(val) when :template pprint_cfn_template(val) when :parameter pprint_cfn_section 'parameter', 'TODO', val, 'Parameters' when :resource pprint_cfn_resource 'TODO', val when :parameters val.each { |k, v| pprint_cfn_section 'parameter', k, v, 'Parameters' } when :resources val.each { |k, v| pprint_cfn_resource k, v } else pprint_value(val, '') end end |
#pprint_cfn_resource(name, options, brick = true) ⇒ Object
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 158 159 160 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 127 def pprint_cfn_resource(name, , brick=true) subdir = 'Resources' filn = rb_file(subdir, name) filn = File.(filn) if @config[:expandedpaths] if open_output(subdir,name) @logger.info "Pretty print resource '#{name}' to '#{filn}'" print_maintainer '' print_with_wrapper "resource #{fmt_string(name)}" indent = ' ' hang = true .each do |k, v| if hang writeln ',' hang = false end case k when /^(Metadata|Properties)$/ write "#{indent}#{fmt_key(k)} => " pprint_value [k], indent hang = true else write "#{indent}#{fmt_key(k)} => " write "#{fmt(v)}" hang = true end end writeln close_output add_brick(subdir,name) if brick else @logger.warn "NOT overwriting existing source file '#{filn}'" end end |
#pprint_cfn_section(section, name, options, subdir, brick = true) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 100 def pprint_cfn_section(section, name, , subdir, brick=true) filn = rb_file(subdir, name) filn = File.(filn) if @config[:expandedpaths] if open_output(subdir,name) @logger.info "Pretty print #{section} '#{name}' to '#{filn}'" print_maintainer '' print_with_wrapper "#{section} #{fmt_string(name)}" indent = ' ' + (' ' * section.length) + ' ' hang = true .each do |k, v| if hang writeln ',' hang = false end write indent, fmt_key(k), " => " pprint_value v, indent hang = true end writeln writeln close_output add_brick(subdir,name) if brick else @logger.warn "NOT overwriting existing source file '#{filn}'" end end |
#pprint_cfn_template(tpl) ⇒ Object
24 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 24 def pprint_cfn_template(tpl) file = File.join(@config[:directory],File.basename(@config[:template].gsub(%r'\.(json|yaml|js|yaml)$'i, '.rb'))) filn = if @config[:expandedpaths] File.(file) else file end file = File.basename(@config[:template].gsub(%r'\.(json|yaml|js|yaml)$'i, '.rb')) # noinspection RubyParenthesesAroundConditionInspection if (iam = open_output('', file.gsub(%r'\.(json|yaml|js|yml|rb)'i, ''))) logStep "Saving #{filn}" writeln "#!/usr/bin/env ruby" print_maintainer('') writeln if @config[:directory] writeln "$:.unshift(File.dirname(__FILE__))" # noinspection RubyExpressionInStringInspection writeln '$:.unshift File.absolute_path("#{File.dirname(__FILE__)}/../lib")' end writeln "require 'bundler/setup'" writeln "require 'aws/cfn/dsl/template'" #writeln "require 'cloudformation-ruby-dsl/spotprice'" #writeln "require 'cloudformation-ruby-dsl/table'" writeln writeln "template do" writeln tpl.each do |section, v| case section when 'Parameters' when 'Mappings' when 'Resources' when 'Outputs' else write " value #{fmt_key(section)} => " pprint_value v, ' ' writeln writeln end end else @logger.warn "Not overwriting template: '#{file}'" end %w(Mappings Parameters Resources Outputs).each do |section| writeln " # #{section}" if iam v = tpl[section] case section when 'Parameters' v.each { |name, | pprint_cfn_section 'parameter', name, , 'Parameters', iam } when 'Mappings' v.each { |name, | pprint_cfn_section 'mapping', name, , 'Mappings', iam } when 'Resources' v.each { |name, | pprint_cfn_resource name, , iam } when 'Outputs' v.each { |name, | pprint_cfn_section 'output', name, , 'Outputs', iam } else abort! "Internal Error: Unexpected section '#{section}'" end writeln if iam end writeln "end.exec!" if iam end |
#pprint_value(val, indent) ⇒ Object
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 162 def pprint_value(val, indent) # Prefer to write the value on a single line if it's reasonable to do so single_line = is_single_line(val) || is_single_line_hack(val) if single_line && !is_multi_line_hack(val) s = fmt(val) if s.length < 120 || is_single_line_hack(val) write s return end end # Print the value across multiple lines if val.is_a?(Hash) writeln "{" val.each do |k, v| write "#{indent} #{fmt_key(k)} => " pprint_value v, indent + ' ' writeln "," end write "#{indent}}" elsif val.is_a?(Array) writeln "[" val.each do |v| write "#{indent} " pprint_value v, indent + ' ' writeln "," end write "#{indent}]" elsif val.is_a?(FnCall) && val.multiline && @config[:functions] != 'raw' write val.name, "(" args = val.arguments sep = '' sub_indent = indent + ' ' if val.name == 'join' && args.length > 1 pprint_value args[0], indent + ' ' args = args[1..-1] sep = ',' sub_indent = indent + ' ' end unless args.empty? args.each do |v| writeln sep write sub_indent pprint_value v, sub_indent sep = ',' end if val.name == 'join' && args.length > 1 write "," end writeln write indent end write ")" else write fmt(val) end end |
#prelude_code(indent = ' ') ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 87 def prelude_code(indent=' ') "scope = Aws::Cfn::Compiler.binding[File.basename(File.dirname(__FILE__))][File.basename(__FILE__, '.rb')]\n"+ "template = scope[:template]\n"+ "\n"+ "# noinspection RubyStringKeysInHashInspection\n"+ "template." + "" end |
#print_with_wrapper(code, indent = ' ') ⇒ Object
96 97 98 |
# File 'lib/aws/cfn/dsl/mixins/prettyprint.rb', line 96 def print_with_wrapper(code,indent=' ') write prelude_code(indent)+code.gsub(%r'^\s+','') end |