Class: Thermite::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/thermite/config.rb

Overview

Configuration helper

Constant Summary collapse

DEFAULT_TAG_REGEX =

The default git tag regular expression (semantic versioning format).

/^(#{Thermite::SemVer::VERSION})$/

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Creates a new configuration object.

options is the same as the Tasks#initialize parameter.



35
36
37
# File 'lib/thermite/config.rb', line 35

def initialize(options = {})
  @options = options
end

Instance Method Details

#binary_uri_formatObject

The interpolation-formatted string used to construct the download URI for the pre-built native extension. Can be set via the THERMITE_BINARY_URI_FORMAT environment variable, or a binary_uri_format option.



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

def binary_uri_format
  @binary_uri_format ||= ENV['THERMITE_BINARY_URI_FORMAT'] ||
                         @options[:binary_uri_format] ||
                         false
end

#cargo_shared_libraryObject

The basename of the shared library built by Cargo.



114
115
116
117
118
119
120
# File 'lib/thermite/config.rb', line 114

def cargo_shared_library
  @cargo_shared_library ||= begin
    filename = "#{library_name}.#{shared_ext}"
    filename = "lib#{filename}" unless Gem.win_platform?
    filename
  end
end

#cargo_target_path(target, *path_components) ⇒ Object

Generate a path relative to the CARGO_TARGET_DIR environment variable, or #rust_toplevel_dir/target if that is not set.



184
185
186
187
# File 'lib/thermite/config.rb', line 184

def cargo_target_path(target, *path_components)
  target_base = ENV.fetch('CARGO_TARGET_DIR', File.join(rust_toplevel_dir, 'target'))
  File.join(target_base, target, *path_components)
end

#cargo_toml_pathObject

The absolute path to the Cargo.toml file. The path depends on the existence of the #cargo_workspace_member configuration option.



201
202
203
204
205
206
207
208
# File 'lib/thermite/config.rb', line 201

def cargo_toml_path
  @cargo_toml_path ||= begin
    components = ['Cargo.toml']
    components.unshift(cargo_workspace_member) if cargo_workspace_member

    rust_path(*components)
  end
end

#cargo_workspace_memberObject

If run in a multi-crate environment, the Cargo workspace member that contains the Ruby extension.



193
194
195
# File 'lib/thermite/config.rb', line 193

def cargo_workspace_member
  @cargo_workspace_member ||= @options[:cargo_workspace_member]
end

#crate_versionObject

Alias to the crate version specified in the TOML file.



254
255
256
# File 'lib/thermite/config.rb', line 254

def crate_version
  toml[:package][:version]
end

#debug_filenameObject

Location to emit debug output, if not nil. Defaults to nil.



42
43
44
# File 'lib/thermite/config.rb', line 42

def debug_filename
  @debug_filename ||= ENV['THERMITE_DEBUG_FILENAME']
end

#dynamic_linker_flagsObject

Linker flags for libruby.



278
279
280
# File 'lib/thermite/config.rb', line 278

def dynamic_linker_flags
  @dynamic_linker_flags ||= RbConfig::CONFIG['DLDFLAGS'].strip
end

#git_tag_regexObject

The format (as a regular expression) that git tags containing Rust binary tarballs are supposed to match. Defaults to DEFAULT_TAG_REGEX.



234
235
236
237
238
239
240
241
242
# File 'lib/thermite/config.rb', line 234

def git_tag_regex
  @git_tag_regex ||= begin
    if @options[:git_tag_regex]
      Regexp.new(@options[:git_tag_regex])
    else
      DEFAULT_TAG_REGEX
    end
  end
end

#library_nameObject

The name of the library compiled by Rust.

Due to the way that Cargo works, all hyphens in library names are replaced with underscores.



104
105
106
107
108
109
# File 'lib/thermite/config.rb', line 104

def library_name
  @library_name ||= begin
    base = toml[:lib] && toml[:lib][:name] ? toml[:lib] : toml[:package]
    base[:name].tr('-', '_') if base[:name]
  end
end

#libruby_pathObject

Absolute path to the shared libruby.



159
160
161
# File 'lib/thermite/config.rb', line 159

def libruby_path
  @libruby_path ||= File.join(RbConfig::CONFIG['libdir'], RbConfig::CONFIG['LIBRUBY_SO'])
end

#ruby_extension_dirObject

The relative directory where the Rust shared library resides, in the context of the Ruby project.



214
215
216
# File 'lib/thermite/config.rb', line 214

def ruby_extension_dir
  @ruby_extension_dir ||= @options.fetch(:ruby_extension_dir, 'lib')
end

#ruby_extension_pathObject

Path to the Rust shared library in the context of the Ruby project.



221
222
223
# File 'lib/thermite/config.rb', line 221

def ruby_extension_path
  ruby_path(ruby_extension_dir, shared_library)
end

#ruby_path(*path_components) ⇒ Object

Generate a path relative to #ruby_toplevel_dir, given the path_components that are passed to File.join.



150
151
152
# File 'lib/thermite/config.rb', line 150

def ruby_path(*path_components)
  File.join(ruby_toplevel_dir, *path_components)
end

#ruby_toplevel_dirObject

The top-level directory of the Ruby project. Defaults to the current working directory.



142
143
144
# File 'lib/thermite/config.rb', line 142

def ruby_toplevel_dir
  @ruby_toplevel_dir ||= @options.fetch(:ruby_project_path, FileUtils.pwd)
end

#ruby_versionObject

The major and minor version of the Ruby interpreter that's currently running.



75
76
77
78
79
80
# File 'lib/thermite/config.rb', line 75

def ruby_version
  @ruby_version ||= begin
    version_info = rbconfig_ruby_version.split('.')
    "ruby#{version_info[0]}#{version_info[1]}"
  end
end

#rust_path(*path_components) ⇒ Object

Generate a path relative to #rust_toplevel_dir, given the path_components that are passed to File.join.



176
177
178
# File 'lib/thermite/config.rb', line 176

def rust_path(*path_components)
  File.join(rust_toplevel_dir, *path_components)
end

#rust_toplevel_dirObject

The top-level directory of the Cargo project. Defaults to the current working directory.



168
169
170
# File 'lib/thermite/config.rb', line 168

def rust_toplevel_dir
  @rust_toplevel_dir ||= @options.fetch(:cargo_project_path, FileUtils.pwd)
end

#shared_extObject

The file extension of the compiled shared Rust library.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/thermite/config.rb', line 49

def shared_ext
  @shared_ext ||= begin
    if dlext == 'bundle'
      'dylib'
    elsif Gem.win_platform?
      'dll'
    else
      dlext
    end
  end
end

#shared_libraryObject

The basename of the Rust shared library, as installed in the #ruby_extension_path.



125
126
127
# File 'lib/thermite/config.rb', line 125

def shared_library
  @shared_library ||= "#{library_name}.so"
end

#static_extension?Boolean

Whether to use a statically linked extension.

Returns:

  • (Boolean)


285
286
287
# File 'lib/thermite/config.rb', line 285

def static_extension?
  ENV.key?('RUBY_STATIC') || RbConfig::CONFIG['ENABLE_SHARED'] == 'no'
end

#tarball_filename(version) ⇒ Object

Return the basename of the tarball generated by the thermite:tarball Rake task, given a package version.



133
134
135
136
137
# File 'lib/thermite/config.rb', line 133

def tarball_filename(version)
  static = static_extension? ? '-static' : ''

  "#{library_name}-#{version}-#{ruby_version}-#{target_os}-#{target_arch}#{static}.tar.gz"
end

#target_archObject

Alias for RbConfig::CONFIG['target_cpu'].



87
88
89
# File 'lib/thermite/config.rb', line 87

def target_arch
  @target_arch ||= RbConfig::CONFIG['target_cpu']
end

#target_osObject

Alias for RbConfig::CONFIG['target_os'].



94
95
96
# File 'lib/thermite/config.rb', line 94

def target_os
  @target_os ||= RbConfig::CONFIG['target_os']
end

#tomlObject

Parsed TOML object (courtesy of tomlrb).



247
248
249
# File 'lib/thermite/config.rb', line 247

def toml
  @toml ||= Tomlrb.load_file(cargo_toml_path, symbolize_keys: true)
end

#toml_configObject

The Thermite-specific config from the TOML file.



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/thermite/config.rb', line 261

def toml_config
  @toml_config ||= begin
    # Not using .dig to be Ruby < 2.3 compatible
    if toml && toml[:package] && toml[:package][:metadata] &&
       toml[:package][:metadata][:thermite]
      toml[:package][:metadata][:thermite]
    else
      {}
    end
  end
end