Module: AppStack::CopyListBuilder

Included in:
StackApp
Defined in:
lib/app_stack/copy_list_builder.rb

Overview

import from outside apps

Instance Method Summary collapse

Instance Method Details

#ask_diffsObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/app_stack/copy_list_builder.rb', line 95

def ask_diffs
  puts "These files are changed remotely:"
  i = 1
  @diffs.each do |t, diff|
    puts "[#{i.to_s.bold}] " + short_path(t).blue
    puts "from: #{@copy_files[t].blue}"
    puts diff.to_s(:color)
  end

  puts "use -f (--option) option to overwrite all files,"
  puts "or 'touch' local file to disable warn for specific file."
end

#check_diffObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/app_stack/copy_list_builder.rb', line 63

def check_diff
  @copy_files, @diffs = {}, {}
  @copy_candidates.each do |t, f|
    if File.exists?(t)
      diff = Diffy::Diff.new(f, t, :source => 'files')
      if diff
        @diffs[t] = diff
        @copy_files[t] = f
      else
        echo "Skip copy #{short_path(t).blue} (no diff)."
      end
    else
      @copy_files[t] = f
    end
  end
end

#check_mtimeObject

render/copy file list



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
56
57
58
59
60
61
# File 'lib/app_stack/copy_list_builder.rb', line 28

def check_mtime
  @render_files, @copy_candidates = {}, {}
  @copy_list.each do |f, t|
    type, f = f.split ':', 2

    # exclude file in the exclude list
    excluded = false
    @config[:exclude].each do |patt|
      if File.fnmatch?(patt, t)
        echo "exclude #{short_path(t)} by patthern ", patt
        excluded = true
        break
      end
    end
    next if excluded

    # check modification time
    if type == 'c'
      if newer?(f, t)
        @copy_candidates[t] = f
      else
        echo "skip copy #{short_path(t).blue} (mtime or existence)."
      end
    elsif type == 'r'
      if newer?(f, t) or @attr_mtime > File.mtime(t)
        @render_files[t] = f
      else
        echo "skip render #{short_path(t).blue} (mtime or existence)."
      end
    else
      fail "unknown operation type #{type} for #{short_path(t)}"
    end
  end
end

#copy_file!(fr, to) ⇒ Object



123
124
125
126
127
128
# File 'lib/app_stack/copy_list_builder.rb', line 123

def copy_file!(fr, to)
  target_dir = File.dirname(to)
  FileUtils.mkdir_p target_dir unless File.directory?(target_dir)
  puts "copied #{fr.blue} \n   to: " + to.green.bold
  FileUtils.cp_r fr, to
end

#do_copy!Object



108
109
110
# File 'lib/app_stack/copy_list_builder.rb', line 108

def do_copy!
  @copy_files.each { |to, fr| copy_file!(fr, to) }
end

#do_render!Object



112
113
114
# File 'lib/app_stack/copy_list_builder.rb', line 112

def do_render!
  @render_files.each { |to, fr| render_file!(fr, to) }
end

#import_files(import_conf) ⇒ Object

get exported source file list from app, filtered by import_conf



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/app_stack/copy_list_builder.rb', line 11

def import_files(import_conf)
  file_list = {}
  import_conf.each do |c|
    if c.is_a?(String) # group name
      echo "import from group #{c.bold.blue}"
      file_list.merge! export_files(c)
    elsif c.is_a?(Hash)
      echo "import form file: ", c
      file_list.merge! glob_files(c, true)
    else
      fail 'unknown import options: ' + c.inspect
    end
  end
  file_list
end

#merge!Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/app_stack/copy_list_builder.rb', line 80

def merge!
  check_mtime
  check_diff
  echo 'final copy list: ', @copy_files
  echo 'final diff list: ', @diffs
  echo 'final render lists: ', @render_files

  if @diffs.keys.size > 0
    ask_diffs # quit, copy and show diffs special files
  else
    do_copy!
    do_render!
  end
end

#newer?(f1, f2) ⇒ Boolean

if f1 newer than f2, or f2 not exits but f1 does.

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/app_stack/copy_list_builder.rb', line 117

def newer?(f1, f2)
  return false unless File.exists?(f1)
  return true unless File.exists?(f2)
  File.mtime(f1) > File.mtime(f2)
end

#render_file!(fr, to) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/app_stack/copy_list_builder.rb', line 130

def render_file!(fr, to)
  oh = File.open(to, 'wb')
  if fr.match(/\.liquid$/)
    require 'liquid'
    oh.write Liquid::Template.parse(File.open(fr,
                                              'r:utf-8').read).render(attrs)
  else
    tilt = Tilt.new(fr)
    oh.write tilt.render(OpenStruct.new(attrs))
  end
  oh.close
  puts "readered #{fr.blue}\n     to: " + to.green.bold
end