Module: Hisyo::Util

Included in:
Generator
Defined in:
lib/hisyo/util.rb

Constant Summary collapse

COLORS =
{
  :black => 30,
  :red => 31,
  :green => 32,
  :yellow => 33,
  :blue => 34,
  :magenta => 35,
  :cyan => 36,
  :while => 37,
}

Instance Method Summary collapse

Instance Method Details

#color(text, color = :green) ⇒ Object



68
69
70
# File 'lib/hisyo/util.rb', line 68

def color(text, color = :green)
  options[:color] ? "\e[1m\e[#{COLORS[color] || "33"}m#{text}\e[0m" : text
end

#command(cmd) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/hisyo/util.rb', line 59

def command(cmd)
  begin
    ret = %x!#{cmd}!
    return ret if $?.exitstatus == 0
  rescue Errno::ENOENT
    nil
  end
end

#copy(src) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/hisyo/util.rb', line 14

def copy(src)
  Find.find(src) do |file|
    next if file == src
    is_dir = File.directory?(file)
    dest = File.join(options[:root], file.gsub(src, ""))
    dir = is_dir ? dest : File.dirname(dest)
    if File.exists?(dest)
      skip(dest)
      next
    end
    FileUtils.mkdir_p(dir) unless options[:dryrun]
    next if is_dir

    if file.match(/\.erubis$/)
      dest.gsub!(/\.erubis$/, "")
      template = Tilt.new(file)
      content = template.render(Object.new, @params) do
        File.exist?(dest) ? File.read(dest) : ""
      end
      if File.exist?(dest) && File.file?(dest)
        tmp = template.render(Object.new, @params) { "" }.strip
        if File.read(dest)[tmp]
          skip(dest)
          next
        end
      end
    else
      content = File.read(file)
      if File.exist?(dest) && File.read(dest)[content]
        skip(dest)
      end
    end

    if File.exist?(dest)
      merge(dest)
    else
      create(dest)
    end

    unless options[:dryrun]
      File.open(dest, "w"){|f| f.write content}
    end
  end
end

#create(dest) ⇒ Object



72
73
74
# File 'lib/hisyo/util.rb', line 72

def create(dest)
  puts color("create: ", :green) + dest if @options[:verbose]
end

#merge(dest) ⇒ Object



80
81
82
# File 'lib/hisyo/util.rb', line 80

def merge(dest)
  puts color("merge: ", :magenta) + dest if @options[:verbose]
end

#skip(dest) ⇒ Object



76
77
78
# File 'lib/hisyo/util.rb', line 76

def skip(dest)
  puts color("skip: ", :yellow) + dest if @options[:verbose]
end