Class: WpGenerate::Generator

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

Direct Known Subclasses

Template, Theme

Defined Under Namespace

Classes: Template, Theme

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Generator

Returns a new instance of Generator.

Raises:

  • (NotImplementedError)


2
3
4
# File 'lib/wp_generate/generator.rb', line 2

def initialize *args
  raise NotImplementedError, "This class doesn't do anything on its own, subclass it!"
end

Instance Method Details

#cwdObject



56
57
58
# File 'lib/wp_generate/generator.rb', line 56

def cwd
  ENV['WPGEN_WORK_DIR'] || Dir.pwd
end

#generateObject



10
11
12
13
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
# File 'lib/wp_generate/generator.rb', line 10

def generate
  opt_parse

  template_name = self.class.to_s.demodulize.downcase
  @templates.each_pair do |template_path,output|
    output = "#{cwd}/#{output}"
    input = "#{template_name}/#{template_path}"
    full_path = File.join(templates_dir, input)
    erb = false
    if not File.exist? full_path
      full_path = "#{full_path}.erb"
      erb = true
    end

    raise IOError, "Will not overwrite existing files without global -f option" if File.exist? output and not @options[:force]

    dir = File.dirname(output)
    FileUtils.makedirs dir unless File.directory? dir

    STDERR.puts "#{input} => #{output}" unless @options[:quiet]
    name = @vars[:name]
    open(output, 'w+') do |f|
      if erb
        f.write ERB.new(open(full_path).read).result(binding)
      else
        f.write open(full_path).read
      end
    end
  end
end

#opt_parseObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wp_generate/generator.rb', line 41

def opt_parse
  if @options.is_a? Array
    opt = {}
    @options.each do |o|
      case o
      when '-f'
        opt[:force] = true
      when '-q'
        opt[:quiet] = true
      end
    end
    @options = opt
  end
end

#templates_dirObject



6
7
8
# File 'lib/wp_generate/generator.rb', line 6

def templates_dir
  File.join(File.dirname(__FILE__), 'templates')
end