module SheepAst::LetCompile

module to enable compile from a file to a file.

Public Instance Methods

compile(data, datastore, template_file = nil, **options) click to toggle source
# File lib/sheep_ast/action/let_compile.rb, line 55
def compile(data, datastore, template_file = nil, **options)
  if !data.nil?
    namespace = w_or_wo_ns(data, **{ **options, namespace: true })
    namespace_arr = data[:_namespace]
  end
  outdir = datastore.value(:_sheep_outdir)
  outdir = './' if outdir.nil?
  template_dir = datastore.value(:_sheep_template_dir)
  template_file_ = find_file(template_dir, template_file)

  if !template_file_.nil?
    raw = File.read(template_file_)
    head_index = raw.index("\n")
    head = raw[0..T.must(head_index) - 1]
    partitioned = T.must(T.must(head).split('!')[1]).rpartition('.')
    suffix = partitioned.last
    title = binding.eval(partitioned.first) # rubocop:disable all
  end

  user_def = T.unsafe(self).user_def_compile(data, datastore, template_file_, **options)

  if options[:dry_run]
    format_dump {
      ldump "data : #{data.inspect}"
      ldump "namespace : #{namespace.inspect}"
      ldump "namespace_arr : #{namespace_arr.inspect}"
      ldump "user_def : #{user_def.inspect}"
      ldump "erb_head : #{head}"
      ldump "title : #{title}"
      ldump "suffix : #{suffix}"
    }
    return T.unsafe(self).ret(**options)
  end

  ldebug? and ldebug '=== compile debug ==='
  ldebug? and ldebug "data : #{data.inspect}"
  ldebug? and ldebug "namespace : #{namespace.inspect}"
  ldebug? and ldebug "namespace_arr : #{namespace_arr.inspect}"
  ldebug? and ldebug "user_def : #{user_def.inspect}"
  ldebug? and ldebug "erb_head : #{head}"
  ldebug? and ldebug "title : #{title}"
  ldebug? and ldebug "suffix : #{suffix}"
  ldebug? and ldebug "outdir : #{outdir.inspect}"
  ldebug? and ldebug '=== end ==='

  template_contents = T.must(raw)[T.must(head_index) + 1..-1]
  erb = ERB.new(template_contents, trim_mode: 1)
  res = erb.result(binding)

  to_file = "#{title}.#{suffix}" if title && suffix
  if to_file.nil?
    puts res
    return T.unsafe(self).ret(**options)
  end

  update_file(to_file, res, **options)
  return T.unsafe(self).ret(**options)
rescue => e # rubocop: disable all
  bt = e.backtrace
  lfatal "Exception was occured inside let_compile. bt = #{bt}"
  lfatal "class = #{e.class}"
  lfatal "message = #{e.message}"
  if !ENV['SHEEP_DEBUG_PRY'].nil?
    lfatal 'Entering pry debug session'
    binding.pry # rubocop: disable all
  else
    lfatal 'Not entering pry debug session.'
    lfatal 'Please define SHEEP_DEBUG_PRY for entering pry debug session'
    lfatal 'Critical. Exit'
    raise
  end
  return T.unsafe(self).ret(**options)
end
construct_file_name(namespace, title, **options) click to toggle source
# File lib/sheep_ast/action/let_compile.rb, line 136
def construct_file_name(namespace, title, **options)
  namespace_sep = namespace_separator_file(**options)
  namespace.empty? ? title.to_s : "#{namespace}#{namespace_sep}#{title}"
end
user_def_compile(data, datastore, template_file, **options) click to toggle source
# File lib/sheep_ast/action/let_compile.rb, line 150
def user_def_compile(data, datastore, template_file, **options); end