Class: ChromeExtensionStarter

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ ChromeExtensionStarter

Returns a new instance of ChromeExtensionStarter.



9
10
11
12
13
14
15
16
17
18
# File 'lib/chrome_extension_starter.rb', line 9

def initialize(argv)
  @target_name = argv.first
  @uuid = UUIDTools::UUID.random_create.to_s.gsub('-', '')
  if !@target_name
    warn "usage: chrome-extension-starter myextension"
    exit 1
  end
  @path = Pathname.new Dir.pwd
  @root_path = Pathname.new(__FILE__).parent.parent
end

Instance Method Details

#generate_templates(target_path, template_path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chrome_extension_starter.rb', line 30

def generate_templates(target_path, template_path)
  FileUtils.cp_r template_path.to_s, target_path.to_s
  Pathname.glob(target_path.to_s + "**/**/*") do |f|
    next unless f.file?
    f.open("r+") do |f|
      target_name = @target_name
      uuid = @uuid
      content = f.read
      f.rewind
      f.puts ERB.new(content).result(binding)
      f.truncate f.tell
    end
  end
end

#runObject



20
21
22
23
24
25
26
27
28
# File 'lib/chrome_extension_starter.rb', line 20

def run
  target_path = @path.join(@target_name)
  if target_path.exist?
    warn "#{@target_name} is already exist."
    exit 1
  end

  generate_templates(target_path, @root_path.join('templates'))
end