Bee C0 Coverage Information - RCov

lib/bee_task_package.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/bee_task_package.rb 211 151
99.05%
98.68%

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_build'
17 require 'bee_util'
18 
19 module Bee
20   
21   module Task
22 
23     # Base class for task package. Provides methods to print output using
24     # the build formatter and a method to check task parameters. Furthermore,
25     # this base class extends MethodInfoBase which provides methods
26     # comments, for autodocumentation purpose.
27     class Package < Bee::Util::MethodInfoBase
28       
29       include Bee::Util::BuildErrorMixin
30 
31       # Verbosity indicator
32       attr_reader :verbose
33       
34       # Constructor.
35       # - build: the build we are running.
36       def initialize(build)
37         @build = build
38         if @build and @build.listener
39           @verbose = @build.listener.formatter.verbose
40         else
41           @verbose = false
42         end
43       end
44 
45       protected
46 
47       # Check task parameters. Raise a RuntimeError with explanation message
48       # if a mandatory parameter is missing or an unknown parameter was found.
49       # - params: task parameters as a Hash.
50       # - description: parameters description as a Hash with following keys:
51       #   :mandatory telling if the parameter is mandatory (true or false),
52       #   :type which is the class name of the parameter and
53       #   :default for default value.
54       def check_parameters(params, description)
55         task = caller[0].match(/`(.*)'/)[1]
56         error "'#{task}' parameters must be a hash" unless params.kind_of?(Hash)
57         for param in description.keys
58           error "#{task} '#{param}' parameter is mandatory" unless
59             params[param.to_s] or description[param][:mandatory] == false
60           if params[param.to_s] != nil
61             case description[param][:type]
62             when :string
63               error "#{task} '#{param}' parameter must be a string" unless
64                 params[param.to_s].kind_of?(String)
65             when :integer
66               error "#{task} '#{param}' parameter must be an integer" unless
67                 params[param.to_s].kind_of?(Integer)
68             when :float
69               error "#{task} '#{param}' parameter must be a float" unless
70                 params[param.to_s].kind_of?(Float)
71             when :number
72               error "#{task} '#{param}' parameter must be a number" unless
73                 params[param.to_s].kind_of?(Numeric)
74             when :boolean
75               error "#{task} '#{param}' parameter must be a boolean" unless
76                 params[param.to_s] == true or params[param.to_s] == false
77             when :array
78               error "#{task} '#{param}' parameter must be an array" unless
79                 params[param.to_s].kind_of?(Array)
80             when :string_or_array
81               error "#{task} '#{param}' parameter must be a string or an array" unless
82                 params[param.to_s].kind_of?(String) or params[param.to_s].kind_of?(Array)
83               params[param.to_s] = Array(params[param.to_s])
84             when :string_or_integer
85               error "#{task} '#{param}' parameter must be a string or an integer" unless
86                 params[param.to_s].kind_of?(String) or params[param.to_s].kind_of?(Integer)
87             when :hash
88               error "#{task} '#{param}' parameter must be a hash" unless
89                 params[param.to_s].kind_of?(Hash)
90             when :hash_or_array
91               error "#{task} '#{param}' parameter must be a hash or list of hashes" unless
92                 params[param.to_s].kind_of?(Hash) or params[param.to_s].kind_of?(Array)
93               if params[param.to_s].kind_of?(Hash)
94                 params[param.to_s] = [params[param.to_s]]
95               elsif !params[param.to_s].kind_of?(Array)
96                 error "#{task} '#{param}' parameter must be a hash or a list of hashes"
97               end
98             else
99               error "Unknown parameter type '#{description[param][:type]}'"
100             end
101             params[param.to_sym] = params[param.to_s]
102           else
103             if description[param][:default]
104               params[param.to_sym] = description[param][:default]
105               params[param.to_s] = description[param][:default]
106             end
107           end
108         end
109         for param in params.keys
110           error "Unknown parameter '#{param}'" if 
111             not (description.key?(param) or description.key?(param.to_sym))
112         end
113       end
114       
115       # Utility method to find and filter files.
116       # - root: root directory for files to search.
117       # - includes: list of globs for files to include in search.
118       # - excludes: list of globs for files to exclude from search.
119       # - dotmatch: tells if joker matches dot files.
120       # Return: the list of found files (no directories included).
121       def filter_files(root, includes, excludes, dotmatch=true)
122         error "includes must be a glob or a list of globs" unless
123           !includes or includes.kind_of?(String) or includes.kind_of?(Array)
124         error "excludes must be a glob or a list of globs" unless
125           !excludes or excludes.kind_of?(String) or excludes.kind_of?(Array)
126         error "root must be an existing directory" unless
127           !root or File.exists?(root)
128         current_dir = Dir.pwd
129         begin
130           if dotmatch
131             options = File::FNM_PATHNAME | File::FNM_DOTMATCH
132           else
133             options = File::FNM_PATHNAME
134           end
135           Dir.chdir(root) if root
136           included = []
137           includes = '**/*' if not includes
138           includes = Array(includes)
139           for include in includes
140             error "includes must be a glob or a list of globs" unless
141               include.kind_of?(String)
142             # should expand directories ?
143             # include = "#{include}/**/*" if File.directory?(include)
144             entries = Dir.glob(include, options)
145             included += entries if entries
146           end
147           included.uniq!
148           if excludes
149             included.reject! do |file|
150               rejected = false
151               for exclude in excludes
152                 if File.fnmatch?(exclude, file, options)
153                   rejected = true
154                   break
155                 end
156               end
157               rejected
158             end
159           end
160           included.reject! { |file| File.directory?(file) }
161           return included
162         ensure
163           Dir.chdir(current_dir)
164         end
165       end
166 
167       # Copy a list of files to a given diretory:
168       # - root: root directory of source files.
169       # - files: a list of files to copy relative to root.
170       # - dest: destination directory.
171       # - flatten: tells if a flat copy is made.
172       def copy_files(root, files, dest, flatten)
173         puts "Copying #{files.length} file(s) to '#{dest}'"
174         for file in files
175           from_file = File.join(root, file)
176           if flatten
177             to_file = File.join(dest, File.basename(file))
178           else
179             to_file = File.join(dest, file)
180           end
181           to_dir    = File.dirname(to_file)
182           FileUtils.makedirs(to_dir) if not File.exists?(to_dir)
183           FileUtils.cp(from_file, to_file)
184         end
185       end
186 
187       # Print text on the console.
188       # - text: text to print.
189       def print(text)
190         if @build and @build.listener
191           @build.listener.formatter.print(text)
192         else
193           Kernel.print(text)
194         end
195       end
196       
197       # Puts text on the console.
198       # - text: text to puts.
199       def puts(text)
200         if @build and @build.listener
201           @build.listener.formatter.puts(text)
202         else
203           Kernel.puts(text)
204         end
205       end
206 
207     end
208     
209   end
210   
211 end

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