Skip to content

Commit

Permalink
Merge pull request #30 from malept/cargo-workspaces
Browse files Browse the repository at this point in the history
Add support for Cargo workspaces
  • Loading branch information
malept authored Jan 31, 2017
2 parents 2799f6f + d6714c7 commit 5454beb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ AllCops:
- 'vendor/**/*'
TargetRubyVersion: 2.1

Lint/EndAlignment:
Enabled: true
EnforcedStyleAlignWith: variable

Metrics/AbcSize:
Max: 20

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ Possible options:
Example: `https://example.com/download/%{version}/%{filename}`. Replacement variables:
- `filename` - The value of `Config.tarball_filename`
- `version` - the crate version from `Cargo.toml`
* `cargo_project_path` - the path to the Cargo project. Defaults to the current working directory.
* `cargo_project_path` - the path to the top-level Cargo project. Defaults to the current working
directory.
* `cargo_workspace_member` - if set, the relative path to the Cargo workspace member. Usually used
when it is part of a repository containing multiple crates.
* `github_releases` - whether to look for Rust binaries via GitHub releases when installing
the gem, and `cargo` is not found. Defaults to `false`.
* `github_release_type` - when `github_releases` is `true`, the mode to use to download the Rust
Expand Down
4 changes: 4 additions & 0 deletions lib/thermite/cargo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def run_cargo_if_exists(*args)
#
def run_cargo_rustc(target)
cargo_args = %w(rustc)
if config.cargo_workspace_member
manifest = File.join(config.cargo_workspace_member, 'Cargo.toml')
cargo_args.push('--manifest-path', manifest)
end
cargo_args << '--release' if target == 'release'
cargo_args.push(*cargo_rustc_args)
run_cargo(*cargo_args)
Expand Down
23 changes: 22 additions & 1 deletion lib/thermite/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ def rust_path(*path_components)
File.join(rust_toplevel_dir, *path_components)
end

#
# If run in a multi-crate environment, the Cargo workspace member that contains the
# Ruby extension.
#
def cargo_workspace_member
@cargo_workspace_member ||= @options[:cargo_workspace_member]
end

#
# The absolute path to the `Cargo.toml` file. The path depends on the existence of the
# `cargo_workspace_member` configuration option.
#
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

#
# Path to the Rust shared library in the context of the Ruby project.
#
Expand Down Expand Up @@ -198,7 +219,7 @@ def git_tag_regex
# Parsed TOML object (courtesy of `tomlrb`).
#
def toml
@toml ||= Tomlrb.load_file(rust_path('Cargo.toml'), symbolize_keys: true)
@toml ||= Tomlrb.load_file(cargo_toml_path, symbolize_keys: true)
end

#
Expand Down
2 changes: 2 additions & 0 deletions lib/thermite/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Tasks < Rake::TaskLib
# - `version` - the crate version from the `Cargo.toml` file
# * `cargo_project_path` - the path to the Cargo project. Defaults to the current
# working directory.
# * `cargo_workspace_member` - if set, the relative path to the Cargo workspace member. Usually
# used when it is part of a repository containing multiple crates.
# * `github_releases` - whether to look for rust binaries via GitHub releases when installing
# the gem, and `cargo` is not found. Defaults to `false`.
# * `github_release_type` - when `github_releases` is `true`, the mode to use to download the
Expand Down
7 changes: 7 additions & 0 deletions test/lib/thermite/cargo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def test_run_cargo_release_rustc
mock_module.run_cargo_rustc('release')
end

def test_run_cargo_rustc_with_workspace_member
mock_module.config.stubs(:dynamic_linker_flags).returns('')
mock_module.config.stubs(:cargo_workspace_member).returns('foo/bar')
mock_module.expects(:run_cargo).with('rustc', '--manifest-path', 'foo/bar/Cargo.toml').once
mock_module.run_cargo_rustc('debug')
end

def test_run_cargo_rustc_with_dynamic_linker_flags
mock_module.config.stubs(:dynamic_linker_flags).returns('foo bar')
if RbConfig::CONFIG['target_os'] == 'mingw32'
Expand Down
6 changes: 6 additions & 0 deletions test/lib/thermite/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ def test_rust_path
assert_equal '/tmp/foobar/baz/quux', config.rust_path('baz', 'quux')
end

def test_cargo_toml_path_with_workspace_member
FileUtils.stubs(:pwd).returns('/tmp/foobar')
config(cargo_workspace_member: 'baz')
assert_equal '/tmp/foobar/baz/Cargo.toml', config.cargo_toml_path
end

def test_default_git_tag_regex
assert_equal described_class::DEFAULT_TAG_REGEX, config.git_tag_regex
end
Expand Down

0 comments on commit 5454beb

Please sign in to comment.