Class: Rscons::Script::GlobalDsl
- Inherits:
-
Object
- Object
- Rscons::Script::GlobalDsl
- Defined in:
- lib/rscons/script.rb
Overview
Global DSL methods.
Direct Known Subclasses
Instance Method Summary collapse
-
#build_dir ⇒ String
Get the Rscons build directory path.
-
#download(url, dest, options = {}) ⇒ void
Download a file.
-
#env(*args) {|env| ... } ⇒ Object
Create an environment.
-
#glob(*patterns) ⇒ Array<String>
Return a list of paths matching the specified pattern(s).
-
#initialize(script) ⇒ GlobalDsl
constructor
Create a GlobalDsl.
-
#param(name, value, takes_arg, description) ⇒ Object
Construct a task parameter.
-
#path_append(path) ⇒ void
Append a path component to the PATH variable.
-
#path_components ⇒ Array<String>
Return path components from the PATH variable.
-
#path_prepend(path) ⇒ void
Prepend a path component to the PATH variable.
-
#path_set(new_path) ⇒ void
Set the PATH variable.
-
#rscons(path, *args) ⇒ Object
Invoke rscons in a subprocess for a subsidiary Rsconscript file.
-
#sh(*command) ⇒ Object
Execute a shell command, exiting on failure.
-
#task(*args, &block) ⇒ Object
Create or modify a task.
-
#variant(*args) ⇒ Object
Define a variant, or within a with_variants block, query if it is active.
-
#variant_enabled?(*args) ⇒ Boolean
Check if a variant is enabled.
-
#variant_group(*args, &block) ⇒ Object
Create a variant group.
-
#with_variants(&block) ⇒ Object
Iterate through variants.
Constructor Details
#initialize(script) ⇒ GlobalDsl
Create a GlobalDsl.
9 10 11 |
# File 'lib/rscons/script.rb', line 9 def initialize(script) @script = script end |
Instance Method Details
#build_dir ⇒ String
Get the Rscons build directory path.
17 18 19 |
# File 'lib/rscons/script.rb', line 17 def build_dir Rscons.application.build_dir end |
#download(url, dest, options = {}) ⇒ void
This method returns an undefined value.
Download a file.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rscons/script.rb', line 62 def download(url, dest, = {}) [:redirect_limit] ||= 5 unless [:redirected] if File.exist?(dest) && [:sha256sum] if Digest::SHA2.hexdigest(File.binread(dest)) == [:sha256sum] # Destination file already exists and has the expected checksum. return end end end uri = URI(url) use_ssl = url.start_with?("https://") response = nil = "" digest = Digest::SHA2.new begin Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http| File.open(dest, "wb") do |fh| response = http.get(uri.request_uri) do |data| fh.write(data) digest << data end end end rescue SocketError => e raise RsconsError.new("Error downloading #{dest}: #{e.}") end if response.is_a?(Net::HTTPRedirection) if [:redirect_limit] == 0 raise RsconsError.new("Redirect limit reached when downloading #{dest}") else return download(response["location"], dest, .merge(redirect_limit: [:redirect_limit] - 1, redirected: true)) end end unless response.is_a?(Net::HTTPSuccess) raise RsconsError.new("Error downloading #{dest}") end if [:sha256sum] && [:sha256sum] != digest.hexdigest raise RsconsError.new("Unexpected checksum on #{dest}") end end |
#env(name, options) ⇒ Object #env(options) ⇒ Object
Create an environment.
If a block is given it is immediately executed and passed the newly created Environment object as an argument. If the Environment is created in task context, the process
method is immediately called. Otherwise, the Environment is not processed until the task execution phase.
141 142 143 144 145 146 147 148 |
# File 'lib/rscons/script.rb', line 141 def env(*args, &block) Rscons.application.check_configure e = Environment.new(*args, &block) if Rscons.application.task_execution_phase? e.process end e end |
#glob(*patterns) ⇒ Array<String>
Return a list of paths matching the specified pattern(s).
A pattern can contain a “/**” component to recurse through directories. If the pattern ends with “/**” then only the recursive list of directories will be returned.
Examples:
-
“src/**”: return all directories under “src”, recursively (including “src” itself).
-
“src/*/”: return all files and directories recursively under the src directory.
-
“src/*/.c”: return all .c files recursively under the src directory.
-
“dir/*/”: return all directories in dir, but no files.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rscons/script.rb', line 36 def glob(*patterns) require "pathname" patterns.reduce([]) do |result, pattern| if pattern.end_with?("/**") pattern += "/" end result += Dir.glob(pattern).map do |path| Pathname.new(path.gsub("\\", "/")).cleanpath.to_s end end.sort end |
#param(name, value, takes_arg, description) ⇒ Object
Construct a task parameter.
160 161 162 |
# File 'lib/rscons/script.rb', line 160 def param(name, value, takes_arg, description) Task::Param.new(name, value, takes_arg, description) end |
#path_append(path) ⇒ void
This method returns an undefined value.
Append a path component to the PATH variable.
188 189 190 |
# File 'lib/rscons/script.rb', line 188 def path_append(path) path_set(path_components + [File.(path)]) end |
#path_components ⇒ Array<String>
Return path components from the PATH variable.
168 169 170 |
# File 'lib/rscons/script.rb', line 168 def path_components ENV["PATH"].split(File::PATH_SEPARATOR) end |
#path_prepend(path) ⇒ void
This method returns an undefined value.
Prepend a path component to the PATH variable.
178 179 180 |
# File 'lib/rscons/script.rb', line 178 def path_prepend(path) path_set([File.(path)] + path_components) end |
#path_set(new_path) ⇒ void
This method returns an undefined value.
Set the PATH variable.
198 199 200 201 202 203 |
# File 'lib/rscons/script.rb', line 198 def path_set(new_path) if new_path.is_a?(Array) new_path = new_path.join(File::PATH_SEPARATOR) end ENV["PATH"] = new_path end |
#rscons(path, *args) ⇒ Object
Invoke rscons in a subprocess for a subsidiary Rsconscript file.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/rscons/script.rb', line 212 def rscons(path, *args) rscons_path = File.($0) path = File.(path) if File.directory?(path) command = [*args] dir = path else command = ["-f", path, *args] dir = File.dirname(path) end if File.exist?("#{dir}/rscons") rscons_path = "#{dir}/rscons" end command = [rscons_path] + command print_dir = dir != "." && dir != File.(Dir.pwd) if ENV["specs"] and not ENV["dist_specs"] # specs command = ["ruby", $LOAD_PATH.map {|p| ["-I", p]}, command].flatten # specs end # specs puts "rscons: Entering directory '#{dir}'" if print_dir result = system(*command, chdir: dir) puts "rscons: Leaving directory '#{dir}'" if print_dir unless result raise RsconsError.new("Failed command: " + command.join(" ")) end end |
#sh(command, options = {}) ⇒ Object #sh(*command, options = {}) ⇒ Object
Execute a shell command, exiting on failure. The behavior to exit on failure is suppressed if the :continue
option is given.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/rscons/script.rb', line 263 def sh(*command) = {} if command.last.is_a?(Hash) = command.slice!(-1) end continue = .delete(:continue) if command.size == 1 && command[0].is_a?(Array) command = command[0] end if Rscons.application.verbose if command.size > 1 puts Util.command_to_s(command) else puts command[0] end end begin system(*command, .merge(exception: true)) rescue StandardError => e = "#{e.backtrace[2]}: #{e.}" if continue Ansi.write($stderr, :red, , :reset, "\n") else raise RsconsError.new() end end end |
#task(*args, &block) ⇒ Object
Create or modify a task.
292 293 294 |
# File 'lib/rscons/script.rb', line 292 def task(*args, &block) Util.task(*args, &block) end |
#variant(*args) ⇒ Object
Define a variant, or within a with_variants block, query if it is active.
298 299 300 |
# File 'lib/rscons/script.rb', line 298 def variant(*args) Rscons.application.variant(*args) end |
#variant_enabled?(*args) ⇒ Boolean
Check if a variant is enabled.
303 304 305 |
# File 'lib/rscons/script.rb', line 303 def variant_enabled?(*args) Rscons.application.variant_enabled?(*args) end |
#variant_group(*args, &block) ⇒ Object
Create a variant group.
308 309 310 |
# File 'lib/rscons/script.rb', line 308 def variant_group(*args, &block) Rscons.application.variant_group(*args, &block) end |
#with_variants(&block) ⇒ Object
Iterate through variants.
313 314 315 316 |
# File 'lib/rscons/script.rb', line 313 def with_variants(&block) Rscons.application.enable_variants Rscons.application.with_variants(&block) end |