Module: Aws::Cfn::Compiler::Parse

Included in:
Base
Defined in:
lib/aws/cfn/compiler/mixins/parse.rb

Instance Method Summary collapse

Instance Method Details

#parseObject



6
7
8
9
10
11
# File 'lib/aws/cfn/compiler/mixins/parse.rb', line 6

def parse
  @dsl ||= Aws::Cfn::Dsl::Template.new(@config[:directory])
  %w( Mappings Parameters Resources Outputs ).each do |section|
    parse_section(section,@spec)
  end
end

#parse_rb_file(base, section, filename) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aws/cfn/compiler/mixins/parse.rb', line 83

def parse_rb_file(base, section, filename)
  Aws::Cfn::Compiler.binding ||= {}
  Aws::Cfn::Compiler.binding[section] ||= {}
  Aws::Cfn::Compiler.binding[section][base] ||= {
      brick_path: @config[:directory],
      template:   @dsl,
      logger:     @logger,
      compiler:   self
  }
  source_file = File.expand_path(filename)
  # source      = IO.read(source_file)
  eval "require source_file", binding
  unless @dsl.dict[section.to_sym]
    abort! "Unable to compile/expand #{filename} for #{section}/#{base}"
  end
  sym_to_s(@dsl.dict[section.to_sym])
end

#parse_section(section, spec = nil) ⇒ Object

noinspection RubyGlobalVariableNamingConvention



14
15
16
17
18
19
20
21
22
23
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
# File 'lib/aws/cfn/compiler/mixins/parse.rb', line 14

def parse_section(section,spec=nil)
  logStep "Parsing #{section}..."

  if spec and spec[section]
    abort! "No such directory: #{@config[:directory]} (I am here: #{Dir.pwd})" unless File.directory?(@config[:directory])
    @items          ||= {}
    @items[section] ||= {}
    get  = {}
    item = {}
    spec[section].each do |rsrc|
      @logger.info "\tUsing #{section}::#{rsrc}"
      refp,sub,base,rel = map_resource_reference(rsrc)
      if refp.nil?
        path = vet_path(section)
      else
        path = vet_path(sub ? sub : section, refp, rel)
      end
      unless get[path]
        get[path] = get_file_set([".*"], path, @config[:precedence])
      end
      set = get[path]
      if set[base]
        if item.has_key?(base)
          @logger.error "  !! error: Duplicate item: #{section}/#{base}"
          abort!
        end

        filename = set[base]
        unless filename =~ /\.(ru?by?|ya?ml|js(|on))\z/i
          @logger.info "Brick not supported/ relevant: #{filename}"
          next
        end

        begin
          @logger.debug "  reading #{filename}"
          content = File.read(filename)
          next if content.size==0

          if filename =~ /\.(rb|ruby)\z/i
            item.merge! parse_rb_file(base, section, filename)
          elsif filename =~ /\.js(|on)\z/i
            item.merge! JSON.parse(content)
          elsif filename =~ /\.ya?ml\z/i
            item.merge! YAML.load(content)
          else
            next
          end

        rescue
          abort! "  !! error: #{$!}"
        end
      else
        abort! "  !! error: #{section}/#{base} not found!"
      end
    end
    item.keys.each { |key|
      if @items[section].has_key?(key)
        abort! "  !! error: Duplicate item: #{section}/#{key}"
      end
    }
    @items[section].merge! item

    unless @items[section].keys.count == spec[section].count
      abort! "  !! error: Suspect that a #{section} item was missed! \nRequested: #{spec[section]}\n    Found: #{@items[section].keys}"
    end
  end

end

#sym_to_s(hash) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aws/cfn/compiler/mixins/parse.rb', line 101

def sym_to_s(hash)
  case hash.class.name
    when /Hash/
      item = {}
      hash.each { |k,v|
        item[k.to_s] = sym_to_s(v)
      }
      item
    when /Array/
      hash.map{|e| sym_to_s(e) }
    when /Fixnum|String|TrueClass|FalseClass/
      hash
    when /Symbol/
      hash.to_s
    else
      abort! "Internal error: #{hash} is a #{hash.class.name} which our Ruby parsing is not prepared for. Fix #{__FILE__}::sym_to_s"
  end
end