Skip to content

Instantly share code, notes, and snippets.

@codekitchen
Created March 11, 2016 17:12
Show Gist options
  • Save codekitchen/cb90d9b8ca0519ae80bd to your computer and use it in GitHub Desktop.
Save codekitchen/cb90d9b8ca0519ae80bd to your computer and use it in GitHub Desktop.
rule '.json' => '.yml' do |t|
doc = YAML.load_file(t.source)
doc = cfn_strings(doc)
File.open(t.name, 'wb') { |f| f.write(JSON.pretty_generate(doc)) }
end
# finds any %{Ref: Thing} or %{Fn::Name: [Args]} inside strings,
# and turns the string into a Fn::Join with those objects interpolated
def cfn_strings(object)
case object
when Hash
object.each_with_object({}) { |(k,v),h| h[k] = cfn_strings(v) }
when Array
object.map { |v| cfn_strings(v) }
when String
if object =~ /%{/
parts = object.split(/(%{[^}]+})/)
parts = parts.map { |p| p.start_with?('%{') ? YAML.load(p[2..-2]) : p }
{ "Fn::Join" => ["", parts] }
else
object
end
else
object
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment