Class: Cxxproject::PluginContext

Inherits:
Object
  • Object
show all
Includes:
Context
Defined in:
lib/cxxproject/plugin_context.rb

Overview

context in which plugins are evaluated a cxx_plugin is a gem that:

  • follows the naming convention cxxplugin_name
  • that has a plugin.rb file in lib and
  • that calls cxx_plugin

the context contains

  • @cxxproject2rake
  • @building_blocks
  • @log

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Context

#check_hash, #get_as_array

Constructor Details

#initialize(cxxproject2rake, building_blocks, log, expected_arity) ⇒ PluginContext

Returns a new instance of PluginContext.



26
27
28
29
30
31
# File 'lib/cxxproject/plugin_context.rb', line 26

def initialize(cxxproject2rake, building_blocks, log, expected_arity)
  @cxxproject2rake = cxxproject2rake
  @building_blocks = building_blocks
  @log = log
  @expected_arity = expected_arity
end

Class Method Details

.create_no_args_contextObject



18
19
20
# File 'lib/cxxproject/plugin_context.rb', line 18

def self.create_no_args_context
  return PluginContext.new(nil, nil, nil, 0)
end

.create_three_args_context(cxx, building_blocks, log) ⇒ Object



22
23
24
# File 'lib/cxxproject/plugin_context.rb', line 22

def self.create_three_args_context(cxx, building_blocks, log)
  return PluginContext.new(cxx, building_blocks, log, 3)
end

.expand(toolchain) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cxxproject/plugin_context.rb', line 64

def self.expand(toolchain)
  to_expand = nil
  from = nil
  while (needs_expansion(toolchain)) do
    to_expand = find_toolchain_subhash(toolchain)
    from = find_toolchain_element(toolchain,to_expand[:BASED_ON])
    to_expand.delete(:BASED_ON)
    Cxxproject::Toolchain::Provider.merge(to_expand, from, false)
  end
  return toolchain
end

.find_toolchain_element(tc, name) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cxxproject/plugin_context.rb', line 107

def self.find_toolchain_element(tc,name)
  res = []
  loop = lambda do |res,tc,name|
    tc.each do |k,v|
      if k == name
        res << v
      elsif v.is_a?(Hash)
        loop.call(res,v,name)
      end
    end
  end
  loop.call(res,tc,name)
  return res[0] if res.length > 0
end

.find_toolchain_subhash(tc) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cxxproject/plugin_context.rb', line 91

def self.find_toolchain_subhash(tc)
  res = []
  loop = lambda do |res,tc|
    tc.each do |k,v|
      if(k == :BASED_ON)
        res << tc
      elsif v.is_a?(Hash)
        loop.call(res,v)
      end
    end
  end
  loop.call(res,tc)
  return res[0] if res.length > 0
end

.needs_expansion(tc) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cxxproject/plugin_context.rb', line 76

def self.needs_expansion(tc)
  res = false
  tc.each do |k,v|
    if k == :BASED_ON
      res = true
    elsif v.is_a?(Hash)
      res = needs_expansion(v)
    end
    if res
      break
    end
  end
  return res
end

Instance Method Details

#check_archiver(hash) ⇒ Object



138
139
140
141
# File 'lib/cxxproject/plugin_context.rb', line 138

def check_archiver(hash)
  raise "not a hash" unless hash.is_a?(Hash)
  check_hash(hash, Cxxproject::Toolchain::Provider.default[:ARCHIVER].keys)
end

#check_compiler(hash) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/cxxproject/plugin_context.rb', line 125

def check_compiler(hash)
  raise "not a hash" unless hash.is_a?(Hash)
  check_hash(hash, Cxxproject::Toolchain::Provider.default[:COMPILER].keys)
  [:CPP, :C, :ASM].each do |sym|
    check_compiler_hash(hash, sym)
  end
end

#check_compiler_hash(hash, sym) ⇒ Object



122
123
124
# File 'lib/cxxproject/plugin_context.rb', line 122

def check_compiler_hash(hash, sym)
  check_hash(hash[sym], Cxxproject::Toolchain::Provider.default[:COMPILER][sym].keys << :BASED_ON) if hash[sym]
end

#check_linker(hash) ⇒ Object



133
134
135
136
# File 'lib/cxxproject/plugin_context.rb', line 133

def check_linker(hash)
  raise "not a hash" unless hash.is_a?(Hash)
  check_hash(hash, Cxxproject::Toolchain::Provider.default[:LINKER].keys)
end

#cxx_plugin(&blk) ⇒ Object

method for plugins to get the cxxproject2rake building_blocks log



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cxxproject/plugin_context.rb', line 37

def cxx_plugin(&blk)
  if blk.arity != @expected_arity
    return
  end

  case blk.arity
  when 0
    blk.call()
  when 3
    blk.call(@cxxproject2rake, @building_blocks, @log)
  end
end

#eval_plugin(plugin_text) ⇒ Object

will use the content of the plugin.rb file and evaluate it this will in turn result in a call to cxx_plugin



145
146
147
# File 'lib/cxxproject/plugin_context.rb', line 145

def eval_plugin(plugin_text)
  instance_eval(plugin_text)
end

#toolchain(name, tc) ⇒ Object

specify a toolchain hash supports:

  • :command


53
54
55
56
57
58
59
60
61
62
# File 'lib/cxxproject/plugin_context.rb', line 53

def toolchain(name, tc)
  raise "not a tc" unless tc.is_a?(Hash)
  check_hash(tc, Cxxproject::Toolchain::Provider.default.keys)
  check_compiler(tc[:COMPILER]) if tc[:COMPILER]
  check_linker(tc[:LINKER]) if tc[:LINKER]
  check_archiver(tc[:ARCHIVER]) if tc[:ARCHIVER]
  PluginContext::expand(tc)
  Cxxproject::Toolchain::Provider.add(name)
  Cxxproject::Toolchain::Provider.merge(Cxxproject::Toolchain::Provider[name], tc)
end