Class: SimplyGenius::Atmos::Commands::Generate

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/simplygenius/atmos/commands/generate.rb

Overview

Direct Known Subclasses

New

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI

#agree, #ask, #choose, color_enabled, color_enabled=, #display, #error, #notify, #say, #warn

Class Method Details

.descriptionObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/simplygenius/atmos/commands/generate.rb', line 13

def self.description
  <<~EOF
    Installs configuration templates used by atmos to create infrastructure
    resources e.g.
    
      atmos generate aws/vpc
    
    use --list to get a list of the template names for a given sourceroot
  EOF
end

Instance Method Details

#executeObject



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/simplygenius/atmos/commands/generate.rb', line 49

def execute
  signal_usage_error "template name is required" if template_list.blank? && ! list? && !update?

  sourcepath_list.each do |sp|
    SourcePath.register(File.basename(sp), sp)
  end

  if sourcepaths?

    # don't want to fail for new repo
    if  Atmos.config && Atmos.config.is_atmos_repo?
      Atmos.config['atmos.template_sources'].try(:each) do |item|
        SourcePath.register(item.name, item.location)
      end
    end

    # Always search for templates against the bundled templates directory
    SourcePath.register('bundled', File.expand_path('../../../../../templates', __FILE__))

  end

  if list?
    logger.info "Valid templates are:"
    SourcePath.registry.each do |spname, sp|
      logger.info("\tSourcepath #{sp}")
      filtered_names = sp.template_names.select do |name|
        template_list.blank? || template_list.any? {|f| name =~ /#{f}/ }
      end
      filtered_names.each {|n| logger.info ("\t\t#{n}")}
    end
  else
    g = Generator.new(force: force?,
                         pretend: dryrun?,
                         quiet: quiet?,
                         skip: skip?,
                         dependencies: dependencies?)

    begin

      context = SettingsHash.new
      context_list.each do |c|
        key, value = c.split('=', 2)
        context.notation_put(key, value)
      end

      if update?
        # this isn't 100% foolproof, but is a convenience that should help for most cases

        filtered_templates = state[:visited_templates].select do |vt|
          template_list.blank? || template_list.any? {|n| vt[:name] =~ /#{n}/ }
        end

        sps = filtered_templates.collect(&:source).uniq
        sps.each do |src|
          spname = src[:name]
          sploc = src[:location]

          existing_sp = SourcePath.registry[spname]
          if existing_sp
            if existing_sp.location != sploc
              logger.warn("Saved sourcepath location differs from that in configuration")
              logger.warn(" #{spname} -> saved=#{sploc} configured=#{existing_sp.location}")
              logger.warn(" consider running with --no-sourcepaths")
            end
          else
            sp = SourcePath.register(spname, sploc)
            logger.warn("Saved state contains a source path missing from configuration: #{sp}")
          end
        end

        filtered_templates.each do |vt|
          name = vt[:name]
          ctx = vt[:context]
          spname = vt[:source][:name]
          sp = SourcePath.registry[spname]
          tmpl = sp.template(name)
          tmpl.scoped_context.merge!(ctx) if ctx
          tmpl.context.merge!(context)
          g.apply_template(tmpl)
        end
      else
        g.generate(*template_list, context: context)
      end

      save_state(g.visited_templates, template_list)

    rescue  ArgumentError => e
      logger.error(e.message)
      exit 1
    end
  end

end

#save_state(visited_templates, entrypoint_template_names) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/simplygenius/atmos/commands/generate.rb', line 162

def save_state(visited_templates, entrypoint_template_names)
  if state_file.present?
    visited_state = []
    visited_templates.each do |tmpl|
      visited_tmpl = tmpl.to_h
      visited_tmpl[:context] = tmpl.scoped_context.to_h
      visited_state << visited_tmpl
    end

    state[:visited_templates] ||= []
    state[:visited_templates].concat(visited_state)
    state[:visited_templates].sort! {|h1, h2| h1[:name] <=> h2[:name] }.uniq!

    state[:entrypoint_templates] ||= []
    state[:entrypoint_templates].concat(entrypoint_template_names)
    state[:entrypoint_templates].sort!.uniq!

    File.write(state_file, YAML.dump(state.to_hash))
  end
end

#stateObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/simplygenius/atmos/commands/generate.rb', line 147

def state
  @state ||= begin
    if state_file.present?
      path = File.expand_path(state_file)
      yml_hash = {}
      if File.exist?(path)
        yml_hash = YAML.load_file(path)
      end
      SettingsHash.new(yml_hash)
    else
      SettingsHash.new
    end
  end
end

#state_fileObject



143
144
145
# File 'lib/simplygenius/atmos/commands/generate.rb', line 143

def state_file
  @state_file ||= Atmos.config["atmos.generate.state_file"]
end