require 'find' require 'logger' require 'yaml' module Cbt class App attr_accessor :options, :git, :components def initialize options = {} @options = options @git = Girc.new('git', @options[:verbose]) # initialize logger @logger = Logger.new(STDOUT) @logger.level = @options[:verbose] ? Logger::INFO : Logger::WARN @logger.formatter = proc {|severity, datetime, progname, msg| "#{msg.to_s}\n"} @config = YAML.load(File.open(@options[:config_file]).read) # parse a components name=>object list @components = Hash.new @config["components"].each do |name, conf| @components[name] = Component.new(name, conf) end # merge tag attributes recursively @config["tags"].each do |t,v| tv = @config["tags"]["_default"].clone || {} p = nil t.split('.').each do |pt| p = p ? [p, pt].join('.') : pt # merge in order: tv < a < a.b < a.b.c, ... # puts "merge from #{p} to #{t}" tv.merge! @config["tags"][p] if @config["tags"][p] end @config["tags"][t] = tv end # check uniqueness for assets and lua module luam, assets = Hash.new, Hash.new @components.each do |cname, comp| comp.assets.each do |aname, asset| error "asset #{aname} defined in both components #{cname} and #{assets[aname]}" if assets[aname] assets[aname] = cname end comp.modules.each do |mname, lua| error "lua module #{mname} defined in both components #{cname} and #{luam[mname]}" if luam[mname] luam[mname] = cname end end end # log message handler # ------------------------------ def error msg @logger.fatal ("[ERROR] " + msg).bold.red exit end def warn msg @logger.warn ("[WARN] " + msg).yellow end def info msg @logger.info "[INFO] " + msg end # find current components from the command line option # or from the git branch name def current_component_names component_list = Array.new error "you can not specify component name with '--all' option" if @options[:component] and @options[:all] if @options[:component] component_list = @options[:component].split(",") elsif @options[:all] component_list = @components.keys else if @git.in_git_dir? and /^(comp|component|ct)\/.+/.match(@git.current_branch) info "parse component names from git branch (#{@git.current_branch})" component_list << @git.current_branch.gsub(/^(comp|component|ct)\//, '') end end component_list.each { |n| error "unknown component name '#{n}'" unless @components[n] } component_list end # return the tag definition from config file. def tags tag @config["tags"][tag.to_s] || {} end end end