Class: EasyHtmlGenerator::Generator::Compile::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/easy_html_generator/generator/compile/base.rb

Overview

this is the generator base class

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#enabled?, #file_changed?, #generate, #initialize, #log, #log_running, #repetitive?, #store_file_hash, #tasks

Constructor Details

This class inherits a constructor from EasyHtmlGenerator::Generator::Base

Instance Method Details

#do_file(input_file, output_file, config) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/easy_html_generator/generator/compile/base.rb', line 37

def do_file(input_file, output_file, config)
  return unless File.exist?(input_file) && File.file?(input_file)

  log "-> do_file #{input_file.sub(@project.src_path, '').green}"

  FileUtils.mkdir_p File.dirname(output_file)

  input_content  = File.read(input_file)

  output_content = do_input!(input_content, input_file, output_file, config)

  store_file_hash(input_file)

  return unless output_content

  File.write(output_file, output_content)
end

#do_input!(_input, _input_file) ⇒ Object



33
34
35
# File 'lib/easy_html_generator/generator/compile/base.rb', line 33

def do_input!(_input, _input_file)
  fail NotImplementedError.new "#{self.class.name} is an abstract class."
end

#generate!(config) ⇒ Object



8
9
10
11
12
13
# File 'lib/easy_html_generator/generator/compile/base.rb', line 8

def generate!(config)
  walk_files(config) do |input_file, output_file|
    next unless file_changed? input_file
    do_file(input_file, output_file, config)
  end
end

#input_to_output_file(input_file, config) ⇒ Object



29
30
31
# File 'lib/easy_html_generator/generator/compile/base.rb', line 29

def input_to_output_file(input_file, config)
  input_file.sub(config.source, config.target)
end

#walk_files(config) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/easy_html_generator/generator/compile/base.rb', line 15

def walk_files(config)
  selector = File.join(config.source, config.selector)

  Dir[selector].each do |input_file|
    output_file = input_to_output_file(input_file, config)

    yield(input_file, output_file)
  end
end

#watch_files(config) ⇒ Object



25
26
27
# File 'lib/easy_html_generator/generator/compile/base.rb', line 25

def watch_files(config)
  Dir["#{config.source}/**/*"]
end