Bee C0 Coverage Information - RCov

lib/bee_task_packagemanager.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/bee_task_packagemanager.rb 130 90
97.69%
96.67%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 # Copyright 2006-2012 Michel Casabianca <michel.casabianca@gmail.com>
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 require 'rubygems'
16 require 'bee_util'
17 
18 module Bee
19 
20   module Task
21 
22     # Package manager is responsible for loading packages and calling tasks.
23     class PackageManager
24 
25       include Bee::Util::BuildErrorMixin
26 
27       # Constructor.
28       # - build: the build we are running.
29       def initialize(build)
30         @build = build
31         @packages = {}
32       end
33 
34       # Run a given task.
35       # - task: YAML object for the task to run.
36       def run_task(task)
37         packaged = task.keys[0]
38         package, name = Bee::Util::get_package_name(packaged)
39         parameters = @build.context.evaluate_object(task[packaged])
40         if not @packages[package]
41           @packages[package] = Bee::Task::PackageManager.load_package(package, @build)
42         end
43         error "Task '#{name}' not found in package '#{package}'" if
44           not @packages[package].respond_to?(name)
45         @packages[package].send(name, parameters)
46       end
47 
48       # Get help for a given task.
49       # - task: YAML object for the task to run.
50       def help_task(task)
51         package, name = Bee::Util::get_package_name(task)
52         if not @packages[package]
53           @packages[package] = Bee::Task::PackageManager.load_package(package, @build)
54         end
55         help = {}
56         if name == '?'
57           methods = @packages[package].class.public_instance_methods(false)
58           for method in methods
59             help[method] = @packages[package].class.method_info(method).comment
60           end
61           return help
62         else
63           error "Task '#{name}' not found in package '#{package}'" if
64             not @packages[package].respond_to?(name)
65           help[task] = @packages[package].class.method_info(name).comment
66         end
67         return help
68       end
69 
70       # List all available tasks.
71       def self.list_tasks
72         names = [nil] + Bee::Util::find_gems(/^bee_/).map {|gem| gem.name[4..-1]}
73         tasks = []
74         for name in names
75           begin
76             package = self.load_package(name)
77             methods = package.class.public_instance_methods(false)
78             if name
79               methods.map! {|method| "#{name}.#{method}"}
80             else
81               methods.map! {|method| method.to_s}
82             end
83             tasks += methods
84           rescue Exception
85           end
86         end
87         return tasks.sort
88       end
89 
90       # List all available templates.
91       def self.list_templates
92         gems = Bee::Util::find_gems(/^bee$/, /^bee_/)
93         templates = []
94         for gem in gems
95           gem_path = gem.full_gem_path
96           eggs = Dir.glob("#{gem_path}/egg/*.yml").
97             map{|p| p[gem_path.length+5..-5]}
98           if gem.name != 'bee'
99             package = gem.name[4..-1]
100             templates += eggs.map{|e| "#{package}.#{e}"}
101           else
102             templates += eggs
103           end
104         end
105         return templates.sort
106       end
107 
108       private
109 
110       # Load a given package using introspection: we try to instantiate class
111       # named after the package capitalized, in module Bee::Task.
112       # - package: the package name.
113       def self.load_package(package, build=nil)
114         package = 'default' if not package
115         package.downcase!
116         script = "bee_task_#{package}"
117         clazz = package.capitalize
118         begin
119           require script
120           return Bee::Task.const_get(clazz).new(build)
121         rescue Exception
122           raise Bee::Util::BuildError.new("Task package '#{package}' not found")
123         end
124       end
125 
126     end
127 
128   end
129 
130 end

Generated on Fri Oct 09 02:07:49 +0200 2015 with rcov 1.0.0