Class: Cbt::Creator

Inherits:
App
  • Object
show all
Defined in:
lib/cbt/creator.rb

Instance Attribute Summary

Attributes inherited from App

#components, #git, #options

Instance Method Summary collapse

Methods inherited from App

#current_component_names, #error, #info, #initialize, #tags, #warn

Constructor Details

This class inherits a constructor from Cbt::App

Instance Method Details

#create!(filename, luamod, template) ⇒ Object

create skeleton files based on filename, module objects and template filename



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cbt/creator.rb', line 33

def create! filename, luamod, template 
  if (not File.exists?(filename)) or @options[:force]
    info "create file #{filename}"
    fh = File.open(filename, 'w:utf-8')
    fh.puts Erubis::Eruby.new(File.open(template, 'r:utf-8').read).result({
      "super_class" => luamod.super_class, "name" => luamod.name, 
      "label" => sprintf("(component: %s)", luamod.component_name)
    })
    fh.close
  else
    info "skip existing file #{filename}"
  end
end

#find_tpl(luamod, mock = false) ⇒ Object

find template file name



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cbt/creator.rb', line 20

def find_tpl luamod, mock = false
  tt = ""
  if luamod.name =~ /^[A-Z]/
    tt = luamod.super_class ? "Class_with_super" : "Class"
  else
    tt = luamod.super_class ? "part_with_super" : "part"
  end

  tt = mock ? tt + ".mock.lua.erb" : tt + ".lua.erb"
  find_tpl_file(tt)
end

#find_tpl_file(tt) ⇒ Object

find the template file from local or gem folders



8
9
10
11
12
13
14
15
16
17
# File 'lib/cbt/creator.rb', line 8

def find_tpl_file tt
  # first find the template under the current folder
  tpl = File.join("template", tt)
  
  # if not find there, use the default template come from the gem
  unless File.exists? tpl
    tpl = File.expand_path(File.dirname(__FILE__)) + "/../../templates/" + tt
  end
  File.expand_path(tpl)
end

#process!Object

create initial lua files based on templates



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cbt/creator.rb', line 48

def process!
  comps = current_component_names
  warn "no component specified." unless comps.size > 0
  comps.each do |cname|
    comp = @components[cname]
    raise "undefined components #{cname}" unless comp

    # create each module lua files
    rep_dir = comp.dir(@options[:components_dir])
    comp.modules.each do |name, mod|
      unless File.directory? rep_dir
        info "create directory #{rep_dir}"
        FileUtils.mkdir_p rep_dir
      end

      # create file:
      create!(File.join(rep_dir, mod.name + ".mock.lua"), mod, find_tpl(mod, true)) 
      create!(File.join(rep_dir, mod.name + ".lua"), mod, find_tpl(mod, false)) 
      create!(File.join(rep_dir, mod.name + "_spec.lua"), mod, find_tpl_file('spec.lua.erb')) unless name == 'main'
    end

    create!(File.join(rep_dir, "main.lua"), LuaModule.new(cname, 'main'), find_tpl_file('main.lua.erb')) unless comp.modules['main']
  end # component names
end