Module: Rscons::Util
- Defined in:
- lib/rscons/util.rb
Overview
A collection of stand-alone utility methods.
Class Method Summary collapse
-
.absolute_path?(path) ⇒ Boolean
Return whether the given path is an absolute filesystem path.
-
.colorize_markup(message) ⇒ Array
Colorize a builder run message.
-
.command_to_s(command) ⇒ String
Return a string representation of a command.
-
.determine_n_threads ⇒ Integer
Determine the number of threads to use by default.
-
.find_executable(name) ⇒ String?
Look for an executable.
-
.format_elapsed_time(elapsed) ⇒ String
Format an elapsed time in human-readable format.
-
.make_relative_path(path) ⇒ String
Make a relative path corresponding to a possibly absolute one.
-
.parse_dependency_file(mf_fname) ⇒ Array<String>
Parse dependencies from a Makefile or ldc2 dependency file.
-
.short_format_paths(paths) ⇒ String
Return a string showing the path specified, or if more than one, then the first path with a “(+D)” afterward, where D is the number of remaining paths.
-
.wait_for_thread(*threads) ⇒ Thread
Wait for any of a number of threads to complete.
Class Method Details
.absolute_path?(path) ⇒ Boolean
Return whether the given path is an absolute filesystem path.
11 12 13 14 15 16 17 |
# File 'lib/rscons/util.rb', line 11 def absolute_path?(path) if RUBY_PLATFORM =~ /mingw/ path =~ %r{^(?:\w:)?[\\/]} else path.start_with?("/") end end |
.colorize_markup(message) ⇒ Array
Colorize a builder run message.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rscons/util.rb', line 26 def colorize_markup() if =~ /^(.*?)(<[^>]+>)(.*)$/ prefix, code, suffix = $1, $2, $3 case code when "<target>" code = :magenta when "<source>" code = :cyan when "<reset>" code = :reset end [prefix, code, *colorize_markup(suffix)].delete_if {|v| v == ""} else [] end end |
.command_to_s(command) ⇒ String
Return a string representation of a command.
50 51 52 |
# File 'lib/rscons/util.rb', line 50 def command_to_s(command) command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ') end |
.determine_n_threads ⇒ Integer
Determine the number of threads to use by default.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rscons/util.rb', line 58 def determine_n_threads # If the user specifies the number of threads in the environment, then # respect that. if ENV["RSCONS_NTHREADS"] =~ /^(\d+)$/ return $1.to_i end # Otherwise try to figure out how many threads are available on the # host hardware. begin case RbConfig::CONFIG["host_os"] when /linux/ return File.read("/proc/cpuinfo").scan(/^processor\s*:/).size when /mswin|mingw/ if `wmic cpu get NumberOfLogicalProcessors /value` =~ /NumberOfLogicalProcessors=(\d+)/ return $1.to_i end when /darwin/ if `sysctl -n hw.ncpu` =~ /(\d+)/ return $1.to_i end end rescue end # If we can't figure it out, default to 1. 1 end |
.find_executable(name) ⇒ String?
Look for an executable.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rscons/util.rb', line 108 def find_executable(name) if name["/"] or name["\\"] if File.file?(name) and File.executable?(name) return name end else path_entries = ENV["PATH"].split(File::PATH_SEPARATOR) path_entries.find do |path_entry| if path = test_path_for_executable(path_entry, name) return path end end end end |
.format_elapsed_time(elapsed) ⇒ String
Format an elapsed time in human-readable format.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rscons/util.rb', line 127 def format_elapsed_time(elapsed) hours = (elapsed / (60 * 60)).to_i elapsed -= hours * 60 * 60 minutes = (elapsed / 60).to_i elapsed -= minutes * 60 seconds = elapsed.ceil result = "" if hours > 0 result += "#{hours}h " end if hours > 0 || minutes > 0 result += "#{minutes}m " end result += "#{seconds}s" result end |
.make_relative_path(path) ⇒ String
Make a relative path corresponding to a possibly absolute one.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rscons/util.rb', line 151 def make_relative_path(path) if absolute_path?(path) if path =~ %r{^(\w):(.*)$} "_#{$1}#{$2}" else "_#{path}" end else path end end |
.parse_dependency_file(mf_fname) ⇒ Array<String>
Parse dependencies from a Makefile or ldc2 dependency file.
This method is used internally by Rscons builders.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rscons/util.rb', line 172 def parse_dependency_file(mf_fname) deps = [] buildup = '' File.read(mf_fname).each_line do |line| if line =~ /^(.*)\\\s*$/ buildup += ' ' + $1 else buildup += ' ' + line if buildup =~ /^[^:]+\(\S+\)\s*:.*?:[^:]+\((\S+)\)/ # ldc2-style dependency line deps << $1 elsif buildup =~ /^.*: (.*)$/ # Makefile-style dependency line mf_deps = $1 deps += mf_deps.split(' ').map(&:strip) end buildup = '' end end deps end |
.short_format_paths(paths) ⇒ String
Return a string showing the path specified, or if more than one, then the first path with a “(+D)” afterward, where D is the number of remaining paths.
96 97 98 99 100 101 102 |
# File 'lib/rscons/util.rb', line 96 def short_format_paths(paths) if paths.size == 1 paths.first else "#{paths.first} (+#{paths.size - 1})" end end |
.wait_for_thread(*threads) ⇒ Thread
Wait for any of a number of threads to complete.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/rscons/util.rb', line 201 def wait_for_thread(*threads) if threads.empty? raise "No threads to wait for" end queue = Queue.new threads.each do |thread| # Create a wait thread for each thread we're waiting for. Thread.new do begin thread.join ensure queue.push(thread) end end end # Wait for any thread to complete. queue.pop end |