Class: Thermite::Config

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

Overview

Configuration helper

Constant Summary collapse

DEFAULT_TAG_REGEX =

The basic semantic versioning format.

/^(v\d+\.\d+\.\d+)$/

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Creates a new configuration object.

options is the same as the Tasks#initialize parameter.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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

#crate_versionObject

Alias to the crate version specified in the TOML file.



245
246
247
# File 'lib/thermite/config.rb', line 245

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

#debug_filenameObject

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



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

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

#dynamic_linker_flagsObject

Linker flags for libruby.



269
270
271
# File 'lib/thermite/config.rb', line 269

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_FORMAT.



225
226
227
228
229
230
231
232
233
# File 'lib/thermite/config.rb', line 225

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.



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

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.



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

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

#ruby_extension_pathObject

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



212
213
214
# File 'lib/thermite/config.rb', line 212

def ruby_extension_path
  ruby_path('lib', 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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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

#static_extension?Boolean

Whether to use a statically linked extension.

Returns:

  • (Boolean)


276
277
278
# File 'lib/thermite/config.rb', line 276

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.



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

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'].



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

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

#target_osObject

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



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

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

#tomlObject

Parsed TOML object (courtesy of tomlrb).



238
239
240
# File 'lib/thermite/config.rb', line 238

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

#toml_configObject

The Thermite-specific config from the TOML file.



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/thermite/config.rb', line 252

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