Class: Cbt::App

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

Direct Known Subclasses

Checkout, Cleanup, Creator, Diagram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cbt/app.rb', line 8

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

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



7
8
9
# File 'lib/cbt/app.rb', line 7

def components
  @components
end

#gitObject

Returns the value of attribute git.



7
8
9
# File 'lib/cbt/app.rb', line 7

def git
  @git
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/cbt/app.rb', line 7

def options
  @options
end

Instance Method Details

#current_component_namesObject

find current components from the command line option or from the git branch name



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cbt/app.rb', line 68

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

#error(msg) ⇒ Object

log message handler




53
54
55
56
# File 'lib/cbt/app.rb', line 53

def error msg
  @logger.fatal ("[ERROR] " + msg).bold.red
  exit
end

#info(msg) ⇒ Object



62
63
64
# File 'lib/cbt/app.rb', line 62

def info msg
  @logger.info "[INFO] " + msg
end

#tags(tag) ⇒ Object

return the tag definition from config file.



88
89
90
# File 'lib/cbt/app.rb', line 88

def tags tag
  @config["tags"][tag.to_s] || {}
end

#warn(msg) ⇒ Object



58
59
60
# File 'lib/cbt/app.rb', line 58

def warn msg
  @logger.warn ("[WARN] " + msg).yellow
end