Module: Aws::Cfn::Compiler::Load

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

Instance Method Summary collapse

Instance Method Details

#get_brick_dirname(dir, rsrc, start = nil, rel = false) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/aws/cfn/compiler/mixins/load.rb', line 61

def get_brick_dirname(dir,rsrc,start=nil,rel=false)
  path = nil
  base = nil
  @config[:brick_path_list].each do |p|
    if rel
      # base = File.realpath(File.expand_path(File.join(@config[:directory], base)))
      base = File.realpath(File.expand_path(File.join(p, base)))
    else
      base = start || p
    end
    [dir, dir.downcase].each do |d|
      path = File.join(base, dir)
      if File.directory?(path)
        break
      end
    end
    fileglob = File.join(path,rsrc)
    if File.exists?(fileglob)
      candidates = [fileglob]
    else
      fileglob += ".*"
      candidates = Dir.glob(fileglob).map{ |e|
        if e.match(%r'\.#{@format_regex}$')
          e
        else
          []
        end
      }.flatten
    end
    if File.directory?(path) and candidates.size > 0
      break
    end
  end
  unless path
    @logger.error "  !! error: Cannot calculate brick dirname in #{dir} for #{rsrc} with brick path: \n\t#{@config[:brick_path_list].join("\n\t")} \n"
    abort!
  end
  patn = path
  unless @config[:expandedpaths]
    patn = short_path(path,3)
  end

  unless File.directory?(path)
    @logger.error "  !! error: Cannot load #{dir}/#{rsrc} with brick path: \n\t#{@config[:brick_path_list].join("\n\t")} \n(started with #{File.join(base, dir)}')"
    abort!
  end
  path
end

#get_brick_path(dir, rsrc, start = nil, rel = false) ⇒ Object


57
58
59
# File 'lib/aws/cfn/compiler/mixins/load.rb', line 57

def get_brick_path(dir,rsrc,start=nil,rel=false)
  File.join(get_brick_dirname(dir,rsrc,start,rel),rsrc)
end

#get_file_set(want, path, exts = []) ⇒ Object



7
8
9
10
11
12
13
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
# File 'lib/aws/cfn/compiler/mixins/load.rb', line 7

def get_file_set(want, path, exts=[])
  raise "Bad call to #{self.class.name}.get_file_set: want == nil" unless want
  @logger.debug "Look for #{want.ai} in #{[path]} with #{exts} extensions"
  if exts.nil?
    exts = @config[:precedence]
  end
  file_regex=%r/^(\S+)\.(#{exts.join('|')})$/
  if exts.empty?
    file_regex=%r/^(\S+)()$/
    exts=['']
  end
  regex = "^(#{want.join('|')})$"
  set = {}
  abs = File.expand_path(path)
  abs = path unless @config[:expandedpaths]
  raise "Oops! Does '#{path}' directory exist?" unless File.directory?(abs)
  #abs = File.realpath(abs)
  begin
    Dir.glob("#{abs}/*").each{ |f|
      match = File.basename(f).match(file_regex)
      if match
        name = match[1]
        ext  = match[2]
        set[ext] = {} unless set[ext]
        @logger.trace "#{name} =~ #{regex}"
        set[ext][name] = f if name.match(regex)
      end
    }
  rescue RegexpError => e
    raise "The regular expression attempting to match resources in '#{path}' is incorrect! #{e.message}"
  end
  @logger.debug "getPathSet set=#{set.ai}"
  res = {}
  # Iterate extension sets in increasing precedence order ...
  # Survivor will be the most desireable version of the item
  # i.e. the .rb environment, role, data bag, etc. will be preferred over the .json version
  exts.each{ |e|
    h = set[e]
    if h
      h.each{ |n,f|
        @logger.info "Ignoring #{File.basename(res[n])}" if res[n]
        res[n] = f
      }
    else
      @logger.debug "'#{e}' set is empty! (No #{path}/*.#{e} files found using precedence #{exts})"
    end
  }
  res
end

#short_path(path, n = 2) ⇒ Object


110
111
112
113
114
115
116
117
# File 'lib/aws/cfn/compiler/mixins/load.rb', line 110

def short_path(path,n=2)
  if path
    patn = path.split(File::SEPARATOR)[0-n..-1]
    patn = patn.join(File::SEPARATOR)
  else
    path
  end
end