class AppStack::CompareFile

items as copy list candidate

Attributes

diff[RW]
from_app[RW]
from_file[RW]
import[RW]
render[RW]
to_app[RW]
to_file[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/app_stack/compare_list.rb, line 8
def initialize(opts)
  opts.each { |k, v| send("#{k}=".to_sym, v) }
  @to_file ||= to_app.get_file_path(from_file_short_name)
end

Public Instance Methods

diff?() click to toggle source
# File lib/app_stack/compare_list.rb, line 37
def diff?
  return true unless File.exists?(to_file)
  c_file = from_file
  if render?
    c_file = Tempfile.new(File.basename(from_file)).path
    render! c_file
  end

  @diff ||= Diffy::Diff.new(to_file, c_file, source: 'files')
  @diff && @diff.to_s.chomp.size > 0 ? true : false
end
from_file_short_name() click to toggle source
# File lib/app_stack/compare_list.rb, line 58
def from_file_short_name
  from_file.sub(/^#{from_app.directory}\//, '')
end
import?() click to toggle source
# File lib/app_stack/compare_list.rb, line 24
def import?
  import ? true : false
end
label() click to toggle source
# File lib/app_stack/compare_list.rb, line 66
def label
  str = "#{operation} #{from_file_short_name.blue} from " +
        "#{from_app.app_name.bold}"
  str << " as #{to_file_short_name.blue}" unless
    to_file_short_name == from_file_short_name
  str
end
local_new?() click to toggle source
# File lib/app_stack/compare_list.rb, line 49
def local_new?
  File.exists?(to_file) && File.mtime(to_file) > File.mtime(from_file)
end
operation() click to toggle source
# File lib/app_stack/compare_list.rb, line 32
def operation
  return 'render' if render?
  import? ? 'import' : 'sync'
end
process() click to toggle source
# File lib/app_stack/compare_list.rb, line 83
def process
  puts label
  render? ? render! : copy!
end
remote_new?() click to toggle source
# File lib/app_stack/compare_list.rb, line 53
def remote_new?
  return true unless File.exists?(to_file)
  File.mtime(from_file) > File.mtime(to_file)
end
render?() click to toggle source
# File lib/app_stack/compare_list.rb, line 20
def render?
  render ? true : false
end
reverse!() click to toggle source
# File lib/app_stack/compare_list.rb, line 13
def reverse!
  fail 'can not reverse an import or render item' if import? || render?
  from_app_, from_file_ = from_app.dup, from_file.dup
  @from_app, @from_file = to_app, to_file
  @to_app, @to_file = from_app_, from_file_
end
reverse_label() click to toggle source
# File lib/app_stack/compare_list.rb, line 74
def reverse_label
  "copy back #{from_file_short_name.blue} to #{to_app.app_name.bold}"
end
skip(msg) click to toggle source
# File lib/app_stack/compare_list.rb, line 78
def skip(msg)
  "Skip #{operation} ".white + from_file_short_name.blue + ' from '.white +
  from_app.app_name.bold + ': ' + msg
end
sync?() click to toggle source
# File lib/app_stack/compare_list.rb, line 28
def sync?
  import ? false : true
end
to_file_short_name() click to toggle source
# File lib/app_stack/compare_list.rb, line 62
def to_file_short_name
  to_file.sub(/^#{to_app.directory}\//, '')
end

Private Instance Methods

copy!() click to toggle source
# File lib/app_stack/compare_list.rb, line 104
def copy!
  target_dir = File.dirname(to_file)
  FileUtils.mkdir_p target_dir unless File.directory?(target_dir)
  FileUtils.cp_r from_file, to_file
end
render!(to = nil) click to toggle source
# File lib/app_stack/compare_list.rb, line 90
def render!(to = nil)
  to ||= to_file
  # return if @options.simulate
  oh = File.open(to, 'wb')
  if from_file.match(/\.liquid$/)
    oh.write Liquid::Template
      .parse(File.open(from_file, 'r:utf-8').read).render(to_app.attrs)
  else
    tilt = Tilt.new(from_file)
    oh.write tilt.render(OpenStruct.new(to_app.attrs))
  end
  oh.close
end