Module: Thermite::Cargo

Included in:
Tasks
Defined in:
lib/thermite/cargo.rb

Overview

Cargo helpers

Instance Method Summary collapse

Instance Method Details

#cargoObject

Path to cargo. Can be overwritten by using the CARGO environment variable.



32
33
34
# File 'lib/thermite/cargo.rb', line 32

def cargo
  @cargo ||= find_executable(ENV.fetch('CARGO', 'cargo'))
end

#cargo_msg(require_severity) ⇒ Object

Message used when cargo is not found.

require_severity is the verb that indicates how important Rust is to the library.



77
78
79
80
81
82
83
84
# File 'lib/thermite/cargo.rb', line 77

def cargo_msg(require_severity)
  <<EOM
****
Rust's Cargo is #{require_severity} to build this extension. Please install
Rust and put it in the PATH, or set the CARGO environment variable appropriately.
****
EOM
end

Message used when cargo is recommended but not found.



96
97
98
# File 'lib/thermite/cargo.rb', line 96

def cargo_recommended_msg
  cargo_msg('recommended (but not required)')
end

#cargo_required_msgObject

Message used when cargo is required but not found.



89
90
91
# File 'lib/thermite/cargo.rb', line 89

def cargo_required_msg
  cargo_msg('required')
end

#inform_user_about_cargoObject

Inform the user about cargo if it doesn't exist.

If optional_rust_extension is true, print message to STDERR. Otherwise, raise an exception.



66
67
68
69
70
# File 'lib/thermite/cargo.rb', line 66

def inform_user_about_cargo
  raise cargo_required_msg unless options[:optional_rust_extension]

  $stderr.write(cargo_recommended_msg)
end

#run_cargo(*args) ⇒ Object

Run cargo with the given args and return STDOUT.



39
40
41
42
43
# File 'lib/thermite/cargo.rb', line 39

def run_cargo(*args)
  Dir.chdir(config.rust_toplevel_dir) do
    sh "#{cargo} #{Shellwords.join(args)}"
  end
end

#run_cargo_build(target) ⇒ Object

Run cargo build, given a target (i.e., release [default] or debug).



55
56
57
58
59
# File 'lib/thermite/cargo.rb', line 55

def run_cargo_build(target)
  cargo_args = %w(build)
  cargo_args << '--release' if target == 'release'
  run_cargo(*cargo_args)
end

#run_cargo_if_exists(*args) ⇒ Object

Only run_cargo if it is found in the executable paths.



48
49
50
# File 'lib/thermite/cargo.rb', line 48

def run_cargo_if_exists(*args)
  run_cargo(*args) if cargo
end