Class: Guard::JekyllPlus::Builder::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/jekyll_plus/builder/action.rb

Direct Known Subclasses

Adder, Modifier, Rebuilder, Remover

Instance Method Summary collapse

Constructor Details

#initialize(config, site) ⇒ Action

Returns a new instance of Action.



13
14
15
# File 'lib/guard/jekyll_plus/builder/action.rb', line 13

def initialize(config, site)
  @config, @site = config, site
end

Instance Method Details

#build(files, message, mark) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/guard/jekyll_plus/builder/action.rb', line 27

def build(files, message, mark)
  @config.info [message, 'building...'.yellow].join(' ')

  if files
    puts '| ' # spacing
    files.each { |file| puts '|' + mark + file }
    puts '| ' # spacing
  end

  elapsed = Benchmark.realtime { @site.process }.round(2)

  change = format('%s → %s', @config.source, @config.destination)
  @config.info format('build completed in %ss '.green + change, elapsed)

  # rescue almost everything because Jekyll::Convertible forwards
  # every plugin-specific exception it encounters
rescue StandardError => e
  @config.error 'build has failed'
  @config.error e.to_s
  throw :task_has_failed
end

#build_was_needed(paths) ⇒ Object



73
74
75
76
77
# File 'lib/guard/jekyll_plus/builder/action.rb', line 73

def build_was_needed(paths)
  matched = jekyll_matches(paths)
  return false  if matched.empty?
  build(matched, @msg, @mark)
end

#copy(src, dst) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/guard/jekyll_plus/builder/action.rb', line 95

def copy(src, dst)
  if @config.excluded?(src)
    puts '|' + ('  ~ ' + "Ignoring excluded file: #{src}").yellow
    return
  end

  FileUtils.mkdir_p File.dirname(dst)
  FileUtils.cp src, dst
  puts '|' + ''.green + dst
end

#destination_path(file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/guard/jekyll_plus/builder/action.rb', line 60

def destination_path(file)
  src_abs_path = Pathname(@config.source).expand_path

  abs_path = Pathname(file).expand_path
  rel_path = begin
               abs_path.relative_path_from(src_abs_path)
             rescue ArgumentError # probably happens only on Windows
               raise "File not in Jekyll source dir: #{file}"
             end

  (Pathname(@config.destination) + rel_path).to_s
end


125
126
127
# File 'lib/guard/jekyll_plus/builder/action.rb', line 125

def footer
  puts '| ' # spacing
end

#header(files) ⇒ Object



119
120
121
122
123
# File 'lib/guard/jekyll_plus/builder/action.rb', line 119

def header(files)
  plural = files.nil? ? '' : pluralize('file', files.size)
  @config.info [@activity, plural].join(' ').send(@color)
  puts '| ' unless files.nil? # spacing
end

#ignore_stitch_sources(files) ⇒ Object



49
50
51
52
53
54
# File 'lib/guard/jekyll_plus/builder/action.rb', line 49

def ignore_stitch_sources(files)
  return files unless (ignore = ENV['GUARD_STITCH_PLUS_FILES'])

  ignore = ignore.split(',')
  files.reject { |f| ignore.include? f }
end

#jekyll_matches(paths) ⇒ Object



17
18
19
# File 'lib/guard/jekyll_plus/builder/action.rb', line 17

def jekyll_matches(paths)
  paths.select { |file| file =~ @config.extensions }
end

#non_jekyll_matches(paths) ⇒ Object



21
22
23
24
25
# File 'lib/guard/jekyll_plus/builder/action.rb', line 21

def non_jekyll_matches(paths)
  paths.select do |file|
    !file.match(/^_/) && !file.match(@config.extensions)
  end
end

#pluralize(word, array) ⇒ Object



56
57
58
# File 'lib/guard/jekyll_plus/builder/action.rb', line 56

def pluralize(word, array)
  "#{word}#{array.size > 1 ? 's' : ''}"
end

#remove(path) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/guard/jekyll_plus/builder/action.rb', line 106

def remove(path)
  if File.exist? path
    FileUtils.rm path
    puts '|' + ''.red + path
  end

  dir = File.dirname path
  return if Dir[dir + '/*'].any?

  FileUtils.rm_rk(dir)
  puts '|' + '  x '.red + dir
end

#update(paths) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/guard/jekyll_plus/builder/action.rb', line 79

def update(paths)
  return if build_was_needed(paths)

  unmatched = non_jekyll_matches(paths)
  return if unmatched.empty?
  files = ignore_stitch_sources(unmatched)
  return if files.empty?

  do_update(files)
  footer
rescue RuntimeError, SystemCallError => e
  @config.error "#{@name} has failed"
  @config.error e.to_s
  throw :task_has_failed
end