require 'fileutils' module Cbt class Checkout < App # copy file from s to t, use luobo if convert class defined def cp_file s, t converted = false if /\.(?\w+)$/ =~ s if ext_ begin klass = Module.const_get(ext_.capitalize + 'Luobo') rescue Exception => e end if klass and klass.is_a?(Class) info "Use #{klass} to convert #{s}" klass.cp! s, t, @options[:platform] converted = true end end end # if no any luobo class converted the file, copy as it is FileUtils.cp s, t unless converted # add to consistency list @digester.add_history s @digester.add_history t end def add_copy_list sfile, tfile # only add source file that has changed unless no_consistency_check @file_list[sfile] = tfile if @options[:no_consistency_check] or @digester.changed?(sfile) if @file_list[sfile] and @digester.changed?(tfile) and File.exists? tfile error "target file #{tfile} changed. use --force to force overwrite" unless @options[:force] end end # copy asset files for all components def copy_assets! comp, ws_dir comp.assets.each do |aname, asset| comp_dir = comp.dir(@options[:components_dir]) # detect if an asset file saved both in global and component space sfile = asset.path([comp_dir], @options[:platform]) global_file = asset.path([@options[:assets_dir]], @options[:platform]) warn "#{sfile} will overwrite #{global_file}" if sfile and global_file sfile = global_file unless sfile error "asset file for #{aname}, component #{comp.name} not found" unless sfile tfile = m.target(ws_dir) add_copy_list sfile, tfile end end # copy required component assets and files def copy_require! comp, ws_dir comp.modules.each do |mname, m| next if mname == 'main' # skip main file from the application core component comp_dir = comp.dir(@options[:components_dir]) sfile = m.path([comp_dir], @options[:require_platform]) tfile = m.target(ws_dir) error "no file find for module lua file #{mname}, component #{comp.name}" unless sfile # add lua module file int copy waiting list add_copy_list sfile, tfile end # copy all end # copy lua/spec files for a component def process_component! comp info "checkout component #{comp.name}" # the copy candidate files list @file_list = Hash.new comp_dir = comp.dir(@options[:components_dir]) ws_dir = comp.ws_dir(@options[:workspace_dir], @options[:platform]) unless File.directory? ws_dir info "create workspace directory #{ws_dir}" FileUtils.mkdir_p ws_dir end @digester = Digester.new(ws_dir + ".yml") # copy assets files for all components @components.each do |cname, c| copy_assets! c, ws_dir unless cname == comp.name end copy_assets! comp, ws_dir # copy the current component at last # copy require files for all components other than current component @components.each do |cname, c| copy_require! c, ws_dir unless cname == comp.name end # for each lua module, error if no file comp.modules['main'] = LuaModule.new(comp.name, 'main') unless comp.modules['main'] comp.modules.each do |mname, m| sfile = m.path([comp_dir], @options[:platform]) tfile = m.target(ws_dir) error "no file find for module lua file #{mname}, component #{comp.name}" unless sfile # add lua module file int copy waiting list add_copy_list sfile, tfile end # for all spec files spec_dir = comp_dir spec_dir = @options[:components_dir] if @options[:all_spec] Dir[spec_dir + "/*_spec.lua"].each do |lua| add_copy_list lua, File.join(ws_dir, File.basename(lua)) end # check if t registered twice tlist = Hash.new @file_list.each do |s,t| warn "#{s} will overwrite #{tlist[t]} (for #{t})" if tlist[t] tlist[t] = s if @options[:simulation] puts "#{s} -> #{t}" else info "copy #{s} -> #{t}" cp_file s, t @digester.add_history s @digester.add_history t end end end # wrap for all components def process! unless File.directory? @options[:workspace_dir] info "create workspace directory #{@options[:workspace_dir]}" FileUtils.mkdir_p @options[:workspace_dir] end if @options[:build] == true warn "set require platform to #{@options[:platform]}" unless @options[:require_platform] == @options[:platform] @options[:require_platform] = @options[:platform] end # process for all component current_component_names.each do |cname| comp = @components[cname] process_component! comp end end end end