class SimplyGenius::Atmos::Generator::ThorGenerator

Attributes

parent[R]
tmpl[R]

Public Class Methods

new(tmpl, parent, **opts) click to toggle source
Calls superclass method
# File lib/simplygenius/atmos/generator.rb, line 79
def initialize(tmpl, parent, **opts)
  @tmpl = tmpl
  @parent = parent
  super([], **opts)
end

Public Instance Methods

add_config(yml_file, key, value, additive: true) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 195
def add_config(yml_file, key, value, additive: true)
  new_yml = SettingsHash.add_config(yml_file, key, value, additive: additive)
  create_file yml_file, new_yml
  @raw_configs.delete(yml_file) if @raw_configs
end
agree(question, character = nil, varname: nil, &details) click to toggle source
Calls superclass method SimplyGenius::Atmos::UI#agree
# File lib/simplygenius/atmos/generator.rb, line 163
def agree(question, character = nil, varname: nil, &details)
  result = lookup_context(varname)
  if result.nil?
    result = super(question, character, &details)
  end
  result = !!result
  track_context(varname, result)
  result
end
apply() click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 90
def apply
  template_dir = tmpl.directory
  path = tmpl.source.directory

  logger.debug("Applying template '#{tmpl.name}' from '#{template_dir}' in sourcepath '#{path}'")

  Find.find(template_dir) do |f|
    next if f == template_dir  # don't create a directory for the template dir itself, but don't prune so we recurse
    Find.prune if f == tmpl.config_path  # don't copy over templates.yml
    Find.prune if f == tmpl.actions_path # don't copy over templates.rb

    # Using File.join(x, '') to ensure trailing slash to make sure we end
    # up with a relative path
    template_rel = f.gsub(/#{File.join(template_dir, '')}/, '')
    source_rel = f.gsub(/#{File.join(path, '')}/, '')
    dest_rel   = source_rel.gsub(/^#{File.join(tmpl.name, '')}/, '')

    # Only include optional files when their conditions eval to true
    optional = tmpl.optional[template_rel]
    if optional
      exclude = ! eval(optional)
      logger.debug("Optional template '#{template_rel}' with condition: '#{optional}', excluding=#{exclude}")
      Find.prune if exclude
    end

    logger.debug("Template '#{source_rel}' => '#{dest_rel}'")
    if File.directory?(f)
      empty_directory(dest_rel)
    else
      copy_file(source_rel, dest_rel, mode: :preserve)
    end
  end

  eval tmpl.actions, binding, tmpl.actions_path
end
ask(question, answer_type = nil, varname: nil, &details) click to toggle source
Calls superclass method SimplyGenius::Atmos::UI#ask
# File lib/simplygenius/atmos/generator.rb, line 153
def ask(question, answer_type = nil, varname: nil, &details)
  result = lookup_context(varname)
  if result.nil?
    result = super(question, answer_type, &details)
  end
  track_context(varname, result)
  result
end
choose(*items, varname: nil, &details) click to toggle source
Calls superclass method SimplyGenius::Atmos::UI#choose
# File lib/simplygenius/atmos/generator.rb, line 174
def choose(*items, varname: nil, &details)
  result = lookup_context(varname)
  if result.nil?
    result = super(*items, &details)
  end
  track_context(varname, result)
  result
end
config_present?(yml_file, key, value=nil) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 208
def config_present?(yml_file, key, value=nil)
  val = get_config(yml_file, key)

  result = val.present?
  if value && result
    if val.is_a?(Array)
      result = Array(value).all? {|v| val.include?(v) }
    else
      result = (val == value)
    end
  end

  return result
end
context() click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 126
def context
  tmpl.context
end
generate(name, ctx: context) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 184
def generate(name, ctx: context)
  parent.generate(name, context: ctx.clone)
end
get_config(yml_file, key) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 202
def get_config(yml_file, key)
  config = raw_config(yml_file)
  config.notation_get(key)
end
lookup_context(varname) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 134
def lookup_context(varname)
  varname.blank? ? nil: tmpl.scoped_context[varname]
end
method_missing(method_name, *args, &blk) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 146
def method_missing(method_name, *args, &blk)
  scoped_context.method_missing(method_name, *args, &blk)
end
new_keys?(src_yml_file, dest_yml_file) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 224
def new_keys?(src_yml_file, dest_yml_file)
  src = raw_config(src_yml_file).keys.sort
  dest = raw_config(dest_yml_file).keys.sort
  (src - dest).size > 0
end
raw_config(yml_file) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 189
def raw_config(yml_file)
  @raw_configs ||= {}
  @raw_configs[yml_file] ||= SettingsHash.new((YAML.load_file(yml_file) rescue {}))
end
respond_to_missing?(method_name, *args) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 142
def respond_to_missing?(method_name, *args)
  scoped_context.respond_to_missing?(method_name, *args)
end
scoped_context() click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 130
def scoped_context
  tmpl.scoped_context
end
track_context(varname, value) click to toggle source
# File lib/simplygenius/atmos/generator.rb, line 138
def track_context(varname, value)
  varname.blank? || value.nil? ? nil : tmpl.scoped_context[varname] = value
end