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(name, options = {}, &block) ⇒ Object
Create or modify a task.
-
#variant(name, options = {}) ⇒ Object
Define a variant, or within a with_variants block, query if it is active.
-
#variant_enabled?(variant_name) ⇒ Boolean
Check if a variant is enabled.
-
#variant_group(*args, &block) ⇒ Object
Create a variant group.
-
#with_variants(&block) ⇒ Object
Iterate through enabled 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.
54 55 56 57 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 86 87 88 89 90 91 92 93 94 |
# File 'lib/rscons/script.rb', line 54 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.
133 134 135 136 137 138 139 140 |
# File 'lib/rscons/script.rb', line 133 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 |
# File 'lib/rscons/script.rb', line 36 def glob(*patterns) Util.glob(*patterns) end |
#param(name, value, takes_arg, description) ⇒ Object
Construct a task parameter.
152 153 154 |
# File 'lib/rscons/script.rb', line 152 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.
180 181 182 |
# File 'lib/rscons/script.rb', line 180 def path_append(path) path_set(path_components + [File.(path)]) end |
#path_components ⇒ Array<String>
Return path components from the PATH variable.
160 161 162 |
# File 'lib/rscons/script.rb', line 160 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.
170 171 172 |
# File 'lib/rscons/script.rb', line 170 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.
190 191 192 193 194 195 |
# File 'lib/rscons/script.rb', line 190 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.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/rscons/script.rb', line 204 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.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/rscons/script.rb', line 255 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(name, options = {}, &block) ⇒ Object
Create or modify a task.
The action block given will be invoked by Rscons when the task executes. It will be passed two arguments:
1) The Task object.
2) A Hash of task parameter names and values.
304 305 306 |
# File 'lib/rscons/script.rb', line 304 def task(*args, &block) Util.task(*args, &block) end |
#variant(name, options = {}) ⇒ Object
Define a variant, or within a with_variants block, query if it is active.
321 322 323 |
# File 'lib/rscons/script.rb', line 321 def variant(name, = {}) Rscons.application.variant(name, ) end |
#variant_enabled?(variant_name) ⇒ Boolean
Check if a variant is enabled.
This can be used, for example, in a configuration block to omit or include configuration checks based on which variants have been configured.
336 337 338 |
# File 'lib/rscons/script.rb', line 336 def variant_enabled?(variant_name) Rscons.application.variant_enabled?(variant_name) end |
#variant_group(name, options = {}) ⇒ Object #variant_group(options = {}) ⇒ Object
Create a variant group.
350 351 352 |
# File 'lib/rscons/script.rb', line 350 def variant_group(*args, &block) Rscons.application.variant_group(*args, &block) end |
#with_variants(&block) ⇒ Object
Iterate through enabled variants.
The given block is called for each combination of enabled variants across the defined variant groups.
358 359 360 361 |
# File 'lib/rscons/script.rb', line 358 def with_variants(&block) Rscons.application.enable_variants Rscons.application.with_variants(&block) end |