Class: EasyHtmlGenerator::Generator::Base

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

Overview

this is the generator base class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, config) ⇒ Base

Returns a new instance of Base.



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/easy_html_generator/generator/base.rb', line 8

def config
  @config
end

Instance Method Details

#dest_pathObject



19
20
21
# File 'lib/easy_html_generator/generator/base.rb', line 19

def dest_path
  "#{@project.dist_path}/#{@config.dest}"
end

#do_file(src, target, *args) ⇒ Object



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

def do_file(src, target, *args)
  return unless File.exist?(src) && File.file?(src)

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

  FileUtils.mkdir_p File.dirname(target)

  args.push target
  i = File.read(src)
  o = do_input(i, *args)

  File.write(target, o)

  EasyHtmlGenerator::Checksum.store_file(src)
end

#do_input(input, *_args) ⇒ Object



59
60
61
# File 'lib/easy_html_generator/generator/base.rb', line 59

def do_input(input, *_args)
  input
end

#generateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easy_html_generator/generator/base.rb', line 27

def generate
  return unless @config.enabled

  log_running

  selector  = File.join(src_path, @config.selector)

  FileUtils.mkdir_p dest_path

  walk_files(selector) do |i, o|
    next unless should_do_file? i

    do_file(i, o)
  end
end

#input_to_output_file(input_file) ⇒ Object



55
56
57
# File 'lib/easy_html_generator/generator/base.rb', line 55

def input_to_output_file(input_file)
  input_file.sub(src_path, dest_path)
end

#log(msg) ⇒ Object



79
80
81
82
# File 'lib/easy_html_generator/generator/base.rb', line 79

def log(msg)
  STDERR.puts "  | #{msg.sub(@project.src_path, '')
    .sub('EasyHtmlGenerator::', '')}"
end

#log_runningObject



23
24
25
# File 'lib/easy_html_generator/generator/base.rb', line 23

def log_running
  log "#{self.class.name.yellow}"
end

#resolve_path_prefix(path, default_prefix = '') ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/easy_html_generator/generator/base.rb', line 84

def resolve_path_prefix(path, default_prefix = '')
  if path.include? 'src://'
    default_prefix = @project.src_path
    path.sub!('src://', '')
  end
  if path.include? 'dist://'
    default_prefix = @project.dist_path
    path.sub!('dist://', '')
  end
  File.join(default_prefix, path)
end

#should_do_file?(i) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/easy_html_generator/generator/base.rb', line 51

def should_do_file?(i)
  EasyHtmlGenerator::Checksum.file_changed? i
end

#src_pathObject



15
16
17
# File 'lib/easy_html_generator/generator/base.rb', line 15

def src_path
  "#{@project.src_path}/#{@config.src}"
end

#walk_files(selector) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/easy_html_generator/generator/base.rb', line 43

def walk_files(selector)
  Dir[selector].each do |input_file|
    output_file = input_to_output_file(input_file)

    yield(input_file, output_file)
  end
end