class Zashoku::Generator::Generator

Attributes

generated[RW]

Public Class Methods

new(name) click to toggle source
# File lib/generator.rb, line 32
def initialize(name)
  @generated = []
  @name = name
  @name_path = Pathname.new(File.absolute_path(@name))
end

Public Instance Methods

generate!() click to toggle source
# File lib/generator.rb, line 76
def generate!; end
pairs() click to toggle source
# File lib/generator.rb, line 38
def pairs
  @pairs || @pairs = {
    /<name>/ => @name,
    /<Name>/ => @name.capitalize,
    /<zv_short>/ => Zashoku::Version.first(2).join('.'),
    /<zv_long>/ => Zashoku::Version.join('.')
  }
end
refactor(thing) click to toggle source
# File lib/generator.rb, line 47
def refactor(thing)
  pairs.reduce(thing.to_s) { |f, t| f = f.gsub(t[0], t[1]) }
end
transfer(template, output) click to toggle source
# File lib/generator.rb, line 51
def transfer(template, output)
  input_dir  = Pathname.new(File.join(TemplateDir, template))
  output_dir = Pathname.new(output)
  raise "path #{output_dir} already exists" if Dir.exist?(output_dir)
  Dir.glob("#{input_dir}/**/*").each do |file|
    next if Dir.exist?(file)

    new_file =
      File.join(
        output_dir,
        refactor(Pathname.new(file).relative_path_from(input_dir))
      )

    FileUtils.mkdir_p(File.dirname(new_file))

    File.open(new_file, 'w') do |f|
      f.print refactor(File.open(file, 'r', &:read))
    end

    FileUtils.chmod(File.stat(file).mode, new_file)
  end

  output
end