-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[rust gem] Major improvements for gem scaffolding (rebased) #8455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
38be055
958f84d
54939ed
f2111de
d97b7a1
866a856
e777e1e
fe8d611
7ad416d
f87f370
e7d262f
637e6dc
a606c3c
d48434c
c0ca3ae
e1a32b4
3858f6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pub fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let _ = rb_sys_env::activate()?; | ||
|
|
||
| Ok(()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --- | ||
| name: Build gems | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - "v*" | ||
| - "cross-gem/*" | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| ci-data: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| result: ${{ steps.fetch.outputs.result }} | ||
| steps: | ||
| - uses: oxidize-rb/actions/fetch-ci-data@v1 | ||
| id: fetch | ||
| with: | ||
| supported-ruby-platforms: | | ||
| exclude: ["arm-linux", "x64-mingw32"] | ||
| stable-ruby-versions: | | ||
| exclude: ["head"] | ||
|
|
||
| source-gem: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: ruby/setup-ruby@v1 | ||
| with: | ||
| bundler-cache: true | ||
|
|
||
| - name: Build gem | ||
| run: bundle exec rake build | ||
|
|
||
| - uses: actions/upload-artifact@v3 | ||
| with: | ||
| name: source-gem | ||
| path: pkg/*.gem | ||
|
|
||
| cross-gem: | ||
| name: Compile native gem for ${{ matrix.platform }} | ||
| runs-on: ubuntu-latest | ||
| needs: ci-data | ||
| strategy: | ||
| matrix: | ||
| platform: ${{ fromJSON(needs.ci-data.outputs.result).supported-ruby-platforms }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: ruby/setup-ruby@v1 | ||
| with: | ||
| bundler-cache: true | ||
|
|
||
| - uses: oxidize-rb/actions/cross-gem@v1 | ||
| id: cross-gem | ||
| with: | ||
| platform: ${{ matrix.platform }} | ||
| ruby-versions: ${{ join(fromJSON(needs.ci-data.outputs.result).stable-ruby-versions, ',') }} | ||
|
|
||
| - uses: actions/upload-artifact@v3 | ||
| with: | ||
| name: cross-gem | ||
| path: ${{ steps.cross-gem.outputs.gem-path }} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,14 @@ | |
|
|
||
| require_relative "<%= File.basename(config[:namespaced_path]) %>/version" | ||
| <%- if config[:ext] -%> | ||
| require_relative "<%= File.basename(config[:namespaced_path]) %>/<%= config[:underscored_name] %>" | ||
| # Attempt to load a versioned extension based on the Ruby version. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove these changes from template? That workaround is only for fat gem, not generally usage. |
||
| # Fall back to loading the non-versioned extension if version-specific loading fails. | ||
| begin | ||
| RUBY_VERSION =~ /(\d+\.\d+)/ | ||
| require "#{Regexp.last_match(1)}/<%= File.basename(config[:namespaced_path]) %>/<%= config[:underscored_name] %>" | ||
| rescue LoadError | ||
| require "<%= File.basename(config[:namespaced_path]) %>/<%= config[:underscored_name] %>" | ||
| end | ||
| <%- end -%> | ||
|
|
||
| <%- config[:constant_array].each_with_index do |c, i| -%> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,15 @@ RSpec.describe <%= config[:constant_name] %> do | |||||||||
| expect(<%= config[:constant_name] %>::VERSION).not_to be nil | ||||||||||
| end | ||||||||||
|
|
||||||||||
| <%- if config[:ext] == 'rust' -%> | ||||||||||
| it "can call into Rust" do | ||||||||||
| result = <%= config[:constant_name] %>.hello("world") | ||||||||||
|
|
||||||||||
| expect(result).to be("Hello earth, from Rust!") | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs Failure/Error: expect(result).to be("Hello earth, from Rust!")
expected #<String:1640> => "Hello earth, from Rust!"
got #<String:1648> => "Hello earth, from Rust!"
Compared using equal?, which compares object identity,
but expected and actual are not the same object. Use
`expect(actual).to eq(expected)` if you don't care about
object identity in this example.
Suggested change
Suggested change
|
||||||||||
| end | ||||||||||
| <%- else -%> | ||||||||||
| it "does something useful" do | ||||||||||
| expect(false).to eq(true) | ||||||||||
| end | ||||||||||
| <%- end -%> | ||||||||||
| end | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1726,12 +1726,11 @@ def create_temporary_dir(dir) | |
| expect(bundled_app("#{gem_name}/ext/#{gem_name}/Cargo.toml")).to exist | ||
| expect(bundled_app("#{gem_name}/ext/#{gem_name}/extconf.rb")).to exist | ||
| expect(bundled_app("#{gem_name}/ext/#{gem_name}/src/lib.rs")).to exist | ||
| expect(bundled_app("#{gem_name}/ext/#{gem_name}/build.rs")).to exist | ||
| end | ||
|
|
||
| it "includes rake-compiler, rb_sys gems and required_rubygems_version constraint" do | ||
| it "includes rake-compiler constraint" do | ||
| expect(bundled_app("#{gem_name}/Gemfile").read).to include('gem "rake-compiler"') | ||
| expect(bundled_app("#{gem_name}/#{gem_name}.gemspec").read).to include('spec.add_dependency "rb_sys"') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove this? |
||
| expect(bundled_app("#{gem_name}/#{gem_name}.gemspec").read).to include('spec.required_rubygems_version = ">= ') | ||
| end | ||
|
|
||
| it "depends on compile task for build" do | ||
|
|
@@ -1754,6 +1753,30 @@ def create_temporary_dir(dir) | |
|
|
||
| expect(bundled_app("#{gem_name}/Rakefile").read).to eq(rakefile) | ||
| end | ||
|
|
||
| it "configures the crate such that `cargo test` works", :ruby_repo, :mri_only do | ||
| env = setup_rust_env | ||
| gem_path = bundled_app(gem_name) | ||
| result = sys_exec("cargo test", env: env, dir: gem_path) | ||
|
|
||
| expect(result).to include("1 passed") | ||
| end | ||
|
|
||
| def setup_rust_env | ||
| skip "rust toolchain of mingw is broken" if RUBY_PLATFORM.match?("mingw") | ||
|
|
||
| env = { | ||
| "CARGO_HOME" => ENV.fetch("CARGO_HOME", File.join(ENV["HOME"], ".cargo")), | ||
| "RUSTUP_HOME" => ENV.fetch("RUSTUP_HOME", File.join(ENV["HOME"], ".rustup")), | ||
| "RUSTUP_TOOLCHAIN" => ENV.fetch("RUSTUP_TOOLCHAIN", "stable"), | ||
| } | ||
|
|
||
| system(env, "cargo", "-V", out: IO::NULL, err: [:child, :out]) | ||
| skip "cargo not present" unless $?.success? | ||
| # Hermetic Cargo setup | ||
| RbConfig::CONFIG.each {|k, v| env["RBCONFIG_#{k}"] = v } | ||
| env | ||
| end | ||
| end | ||
|
|
||
| context "--ext parameter set with go" do | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.blog/changelog/2024-04-16-deprecation-notice-v3-of-the-artifact-actions/