require "fileutils" require "erubis" module Cbt class Creator < App # find the template file from local or gem folders 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 # find template file name 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 # create skeleton files based on filename, module objects and template filename 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 # create initial lua files based on templates 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 end end