From 055c0909cff12c3b6610eac3a486538e5daa22c5 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 29 Jan 2017 19:31:34 -0800 Subject: [PATCH 1/3] Add support for Cargo workspaces Adds a new config variable, `cargo_workspace_member`, which is the path relative to the `cargo_project_path` directory where the member crate is located. --- .rubocop.yml | 4 ++++ README.md | 5 ++++- lib/thermite/config.rb | 18 +++++++++++++++++- lib/thermite/tasks.rb | 2 ++ test/lib/thermite/config_test.rb | 6 ++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 90d870e..b31b7cb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,6 +3,10 @@ AllCops: - 'vendor/**/*' TargetRubyVersion: 2.1 +Lint/EndAlignment: + Enabled: true + EnforcedStyleAlignWith: variable + Metrics/AbcSize: Max: 20 diff --git a/README.md b/README.md index daf8fd6..27ae504 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/thermite/config.rb b/lib/thermite/config.rb index a50e38b..6d85a5d 100644 --- a/lib/thermite/config.rb +++ b/lib/thermite/config.rb @@ -168,6 +168,22 @@ def rust_path(*path_components) File.join(rust_toplevel_dir, *path_components) 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 = if (member_path = @options[:cargo_workspace_member]) + [member_path, 'Cargo.toml'] + else + ['Cargo.toml'] + end + + rust_path(*components) + end + end + # # Path to the Rust shared library in the context of the Ruby project. # @@ -198,7 +214,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 # diff --git a/lib/thermite/tasks.rb b/lib/thermite/tasks.rb index 344d1f0..bb26562 100644 --- a/lib/thermite/tasks.rb +++ b/lib/thermite/tasks.rb @@ -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 diff --git a/test/lib/thermite/config_test.rb b/test/lib/thermite/config_test.rb index 8157a95..b4e1778 100644 --- a/test/lib/thermite/config_test.rb +++ b/test/lib/thermite/config_test.rb @@ -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 From 83f84546f4905786931e466e184a1bd47d918b8e Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 30 Jan 2017 18:36:02 -0800 Subject: [PATCH 2/3] Run cargo with --manifest-path if using Cargo workspaces --- lib/thermite/cargo.rb | 3 +++ lib/thermite/config.rb | 15 ++++++++++----- test/lib/thermite/cargo_test.rb | 7 +++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/thermite/cargo.rb b/lib/thermite/cargo.rb index b931731..6b3b832 100644 --- a/lib/thermite/cargo.rb +++ b/lib/thermite/cargo.rb @@ -55,6 +55,9 @@ def run_cargo_if_exists(*args) # def run_cargo_rustc(target) cargo_args = %w(rustc) + if config.cargo_workspace_member + cargo_args.push('--manifest-path', config.cargo_workspace_member) + end cargo_args << '--release' if target == 'release' cargo_args.push(*cargo_rustc_args) run_cargo(*cargo_args) diff --git a/lib/thermite/config.rb b/lib/thermite/config.rb index 6d85a5d..db8a198 100644 --- a/lib/thermite/config.rb +++ b/lib/thermite/config.rb @@ -168,17 +168,22 @@ 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 = if (member_path = @options[:cargo_workspace_member]) - [member_path, 'Cargo.toml'] - else - ['Cargo.toml'] - end + components = ['Cargo.toml'] + components.unshift(cargo_workspace_member) if cargo_workspace_member rust_path(*components) end diff --git a/test/lib/thermite/cargo_test.rb b/test/lib/thermite/cargo_test.rb index 00b7fce..c48a229 100644 --- a/test/lib/thermite/cargo_test.rb +++ b/test/lib/thermite/cargo_test.rb @@ -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').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' From d6714c7ee2eadb99763eff67d729624710b8b6c8 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 30 Jan 2017 18:42:49 -0800 Subject: [PATCH 3/3] --manifest-path needs to point to the Cargo.toml, not the folder --- lib/thermite/cargo.rb | 3 ++- test/lib/thermite/cargo_test.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/thermite/cargo.rb b/lib/thermite/cargo.rb index 6b3b832..1cd65c5 100644 --- a/lib/thermite/cargo.rb +++ b/lib/thermite/cargo.rb @@ -56,7 +56,8 @@ def run_cargo_if_exists(*args) def run_cargo_rustc(target) cargo_args = %w(rustc) if config.cargo_workspace_member - cargo_args.push('--manifest-path', 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) diff --git a/test/lib/thermite/cargo_test.rb b/test/lib/thermite/cargo_test.rb index c48a229..8404ec7 100644 --- a/test/lib/thermite/cargo_test.rb +++ b/test/lib/thermite/cargo_test.rb @@ -37,7 +37,7 @@ def test_run_cargo_release_rustc 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').once + mock_module.expects(:run_cargo).with('rustc', '--manifest-path', 'foo/bar/Cargo.toml').once mock_module.run_cargo_rustc('debug') end