This is an automated email from the ASF dual-hosted git repository.

erickguan pushed a commit to branch feat/ruby-skinny-gems
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 07f8aa90b6b1db5fc103ad1c0fba71cdf2943100
Author: Erick Guan <[email protected]>
AuthorDate: Tue Nov 24 00:18:29 2026 +0800

    feat(bindings/ruby): add content-addressable native gems
---
 .github/workflows/release_ruby.yml            | 61 ++++++++++++-------
 bindings/ruby/.tool-versions                  |  2 +-
 bindings/ruby/CONTRIBUTING.md                 | 18 ++++++
 bindings/ruby/README.md                       |  8 ++-
 bindings/ruby/Rakefile                        | 45 ++++++++++----
 bindings/ruby/opendal.gemspec                 | 12 ++--
 bindings/ruby/test/package/native_gem_test.rb | 87 +++++++++++++++++++++++++++
 7 files changed, 189 insertions(+), 44 deletions(-)

diff --git a/.github/workflows/release_ruby.yml 
b/.github/workflows/release_ruby.yml
index 45e267f01a..b1c66b90f9 100644
--- a/.github/workflows/release_ruby.yml
+++ b/.github/workflows/release_ruby.yml
@@ -32,6 +32,7 @@ on:
       - "bindings/ruby/Rakefile"
       - "bindings/ruby/lib/opendal.rb"
       - "bindings/ruby/opendal.gemspec"
+      - "bindings/ruby/test/package/**"
   # Rubygems doesn't have a staging environment, and Rubygems don't provide a
   # public registry. Most gems follow a pre-release tag convention. But
   # OpenDAL doesn't want pre-releases to a publish registry beyond
@@ -90,10 +91,12 @@ jobs:
           VERSION=$(bundle exec rake version)
           echo "version=$VERSION" >> "$GITHUB_OUTPUT"
 
-          echo "## Ruby gem built successfully! 📦" >> "$GITHUB_STEP_SUMMARY"
-          echo "" >> "$GITHUB_STEP_SUMMARY"
-          printf '**Ref:** %s\n' "${GITHUB_REF_NAME:-$GITHUB_REF}" >> 
"$GITHUB_STEP_SUMMARY"
-          echo "**Version:** $VERSION" >> "$GITHUB_STEP_SUMMARY"
+          {
+            echo "## Ruby gem built successfully! 📦"
+            echo ""
+            printf '**Ref:** %s\n' "${GITHUB_REF_NAME:-$GITHUB_REF}"
+            echo "**Version:** $VERSION"
+          } >> "$GITHUB_STEP_SUMMARY"
 
   smoke-test:
     needs: build
@@ -169,22 +172,12 @@ jobs:
           rustup default stable
           rustup target add ${{ matrix.rust_target }}
 
-      - name: Setup Ruby 3.3
-        id: ruby-3-3
-        uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # 
v1.343.0
-        with:
-          ruby-version: "3.3"
-          bundler-cache: true
-          working-directory: bindings/ruby
-
-      - name: Compile the Ruby 3.3 extension
-        run: bundle exec rake native:compile
-
       - name: Setup Ruby 3.4
         id: ruby-3-4
         uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # 
v1.343.0
         with:
           ruby-version: "3.4"
+          bundler: "4.1.0"
           bundler-cache: true
           working-directory: bindings/ruby
 
@@ -196,11 +189,28 @@ jobs:
         uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # 
v1.343.0
         with:
           ruby-version: "4.0"
+          bundler: "4.1.0"
+          bundler-cache: true
+          working-directory: bindings/ruby
+
+      - name: Compile the Ruby 4.0 extension
+        run: bundle exec rake native:compile
+
+      - name: Setup Ruby 3.3 for packaging
+        id: ruby-3-3
+        uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # 
v1.343.0
+        with:
+          ruby-version: "3.3"
+          rubygems: "4.1.0"
+          bundler: "4.1.0"
           bundler-cache: true
           working-directory: bindings/ruby
 
-      - name: Compile the Ruby 4.0 extension and package the native gem
-        run: bundle exec rake native:compile native:opendal
+      - name: Compile the Ruby 3.3 extension
+        run: bundle exec rake native:compile
+
+      - name: Package and verify the native gems
+        run: bundle exec rake native:opendal test:package
 
       - name: Smoke test the native gem on each Ruby version
         env:
@@ -208,8 +218,9 @@ jobs:
           RUBY_34: ${{ steps.ruby-3-4.outputs.ruby-prefix }}
           RUBY_40: ${{ steps.ruby-4-0.outputs.ruby-prefix }}
         run: |
+          gem_file="pkg/opendal-$(bundle exec rake 
version)-$OPENDAL_RUBY_PLATFORM.gem"
           for prefix in "$RUBY_33" "$RUBY_34" "$RUBY_40"; do
-            "$prefix/bin/ruby" "$prefix/bin/gem" install --local 
pkg/opendal-*.gem --no-document
+            "$prefix/bin/ruby" "$prefix/bin/gem" install --local "$gem_file" 
--no-document
             "$prefix/bin/ruby" -ropendal -rtmpdir <<'RUBY'
           Dir.mktmpdir do |root|
             op = OpenDal::Operator.new("fs", {"root" => root})
@@ -258,6 +269,8 @@ jobs:
       - name: Setup Ruby and install dependencies
         uses: ruby/setup-ruby@be3c7df29b3349ec6bcfc6b4792f1cf9f5237d18 # 
v1.343.0
         with:
+          rubygems: "4.1.0"
+          bundler: "4.1.0"
           bundler-cache: true
           working-directory: bindings/ruby
 
@@ -280,8 +293,10 @@ jobs:
 
       - name: Log Release
         run: |
-          echo "## Ruby binding released! 🚀" >> "$GITHUB_STEP_SUMMARY"
-          echo "" >> "$GITHUB_STEP_SUMMARY"
-          echo "**Version:** $(bundle exec rake version)" >> 
"$GITHUB_STEP_SUMMARY"
-          echo "" >> "$GITHUB_STEP_SUMMARY"
-          echo "Check out the [RubyGems 
page](https://rubygems.org/gems/opendal) for more details." >> 
"$GITHUB_STEP_SUMMARY"
+          {
+            echo "## Ruby binding released! 🚀"
+            echo ""
+            echo "**Version:** $(bundle exec rake version)"
+            echo ""
+            echo "Check out the [RubyGems 
page](https://rubygems.org/gems/opendal) for more details."
+          } >> "$GITHUB_STEP_SUMMARY"
diff --git a/bindings/ruby/.tool-versions b/bindings/ruby/.tool-versions
index b77860ba9b..053cba7fcd 100644
--- a/bindings/ruby/.tool-versions
+++ b/bindings/ruby/.tool-versions
@@ -1 +1 @@
-ruby 4.0
+ruby 3.3
diff --git a/bindings/ruby/CONTRIBUTING.md b/bindings/ruby/CONTRIBUTING.md
index d8c69689f9..fd9046f2c0 100644
--- a/bindings/ruby/CONTRIBUTING.md
+++ b/bindings/ruby/CONTRIBUTING.md
@@ -30,3 +30,21 @@ bundle exec rake standard:fix
 rustfmt --config-path ../../rustfmt.toml src/*.rs # Run rustfmt for Rust files
 cargo clippy --fix --all-targets # Run rust linter clippy
 ```
+
+## Native gems
+
+Compile each supported Ruby ABI before packaging:
+
+```shell
+bundle exec rake native:compile # Run with Ruby 3.3, 3.4, and 4.0.
+```
+
+Use RubyGems 4.1 or later to package the compiled extensions and check the
+artifacts:
+
+```shell
+bundle exec rake native:opendal test:package
+```
+
+This produces one conventional platform gem and one content-addressable gem
+for each Ruby ABI in `pkg/`. The source gem still uses `bundle exec rake 
build`.
diff --git a/bindings/ruby/README.md b/bindings/ruby/README.md
index d53d66adc2..c2f73a6768 100644
--- a/bindings/ruby/README.md
+++ b/bindings/ruby/README.md
@@ -21,9 +21,11 @@ version.
 
 ## Installation
 
-The Ruby binding requires Ruby 3.3 or later. Precompiled gems include native
-extensions for Ruby 3.3, 3.4, and 4.0. The source gem builds the native
-extension during installation and requires Rust 1.91 or later.
+The Ruby binding requires Ruby 3.3 or later. Precompiled gems support Ruby
+3.3, 3.4, and 4.0. RubyGems and Bundler 4.1 or later select a smaller gem
+containing the extension for your Ruby ABI. Older clients use a platform gem
+containing all supported ABIs. The source gem builds the native extension
+during installation and requires Rust 1.91 or later.
 
 ```shell
 gem install opendal
diff --git a/bindings/ruby/Rakefile b/bindings/ruby/Rakefile
index b00e2e50d6..51212369b0 100644
--- a/bindings/ruby/Rakefile
+++ b/bindings/ruby/Rakefile
@@ -73,6 +73,10 @@ Rake::TestTask.new("test:base") do |t|
   t.test_files = FileList["test/*_test.rb"]
 end
 
+Rake::TestTask.new("test:package") do |t|
+  t.test_files = FileList["test/package/*_test.rb"]
+end
+
 Rake::TestTask.new("test:service") do |t|
   t.libs << "lib"
   t.libs << "test"
@@ -161,26 +165,22 @@ namespace :native do
     compile_extension("#{NATIVE_BUILD_DIR}/#{ruby_version}")
   end
 
-  desc "Package the precompiled extensions for 
#{ENV.fetch("OPENDAL_RUBY_PLATFORM", Gem::Platform.local.to_s)}"
+  desc "Package conventional and ABI-specific gems for 
#{ENV.fetch("OPENDAL_RUBY_PLATFORM", Gem::Platform.local.to_s)}"
   task PACKAGE_NAME do
-    platform = ENV.fetch("OPENDAL_RUBY_PLATFORM", Gem::Platform.local.to_s)
-    Rake::Task["native:#{PACKAGE_NAME}:#{platform}"].invoke
-  end
+    abort "Native packaging requires RubyGems 4.1 or later" if 
Gem::Version.new(Gem::VERSION) < Gem::Version.new("4.1")
 
-  task "#{PACKAGE_NAME}:#{ENV.fetch("OPENDAL_RUBY_PLATFORM", 
Gem::Platform.local.to_s)}" do
     missing_paths = NATIVE_EXTENSION_PATHS.reject { |path| 
File.file?(File.join(NATIVE_BUILD_DIR, path)) }
     unless missing_paths.empty?
       abort "Missing native extensions. Run rake native:compile with each 
supported Ruby.\n#{missing_paths.join("\n")}"
     end
 
     platform = ENV.fetch("OPENDAL_RUBY_PLATFORM", Gem::Platform.local.to_s)
-    spec = GEMSPEC.dup
-    spec.platform = Gem::Platform.new(platform)
-    spec.extensions.clear
-    # New Ruby ABIs need their own extension. The source gem has no upper 
bound.
-    spec.required_ruby_version = [*spec.required_ruby_version.as_list, "< 
4.1.dev"]
     extension_paths = NATIVE_EXTENSION_PATHS.map { |path| 
"lib/#{EXTENSION}/#{path}" }
-    spec.files |= extension_paths
+    native_files = GEMSPEC.files.reject do |path|
+      path.start_with?("src/", "core/") ||
+        %w[Cargo.toml Cargo.lock build.rs].include?(path) ||
+        
path.match?(%r{\Alib/opendal_ruby/\d+\.\d+/opendal_ruby\.(so|bundle)\z})
+    end
 
     FileUtils.mkdir_p "pkg"
     begin
@@ -188,8 +188,27 @@ namespace :native do
         FileUtils.mkdir_p File.dirname(destination)
         FileUtils.cp File.join(NATIVE_BUILD_DIR, source), destination
       end
-      gem_file = Gem::Package.build(spec)
-      FileUtils.mv gem_file, "pkg/#{File.basename(gem_file)}"
+
+      [nil, *NATIVE_RUBY_VERSIONS].each do |ruby_version|
+        spec = GEMSPEC.dup
+        spec.platform = Gem::Platform.new(platform)
+        spec.extensions = []
+        spec.requirements = []
+        # Conventional gems retain every supported ABI for older clients.
+        spec.required_ruby_version = if ruby_version
+          "~> #{ruby_version}.0"
+        else
+          [*GEMSPEC.required_ruby_version.as_list, "< 4.1.dev"]
+        end
+        spec.files = if ruby_version
+          native_files | ["lib/#{EXTENSION}/#{ruby_version}/#{EXTENSION_FILE}"]
+        else
+          native_files | extension_paths
+        end
+
+        gem_file = Gem::Package.build(spec, content_addressable: 
!ruby_version.nil?)
+        FileUtils.mv gem_file, "pkg/#{File.basename(gem_file)}"
+      end
     ensure
       FileUtils.rm_f extension_paths
     end
diff --git a/bindings/ruby/opendal.gemspec b/bindings/ruby/opendal.gemspec
index 0c187182bb..bc3aac91dd 100644
--- a/bindings/ruby/opendal.gemspec
+++ b/bindings/ruby/opendal.gemspec
@@ -18,16 +18,20 @@
 # frozen_string_literal: true
 
 tracked_files_or_glob = lambda do |dir|
-  git_dir = File.join(dir, ".git")
+  git_files = begin
+    IO.popen(["git", "-C", dir, "ls-files", "-z"], err: File::NULL, 
&:read).split("\x0")
+  rescue Errno::ENOENT
+    []
+  end
 
-  if File.exist?(git_dir)
-    IO.popen(["git", "-C", dir, "ls-files", "-z"], &:read).split("\x0")
-  else
+  if git_files.empty?
     Dir.chdir(dir) do
       Dir.glob("**/*", File::FNM_DOTMATCH).reject do |f|
         File.directory?(f)
       end
     end
+  else
+    git_files
   end
 end
 
diff --git a/bindings/ruby/test/package/native_gem_test.rb 
b/bindings/ruby/test/package/native_gem_test.rb
new file mode 100644
index 0000000000..c1c5ab5833
--- /dev/null
+++ b/bindings/ruby/test/package/native_gem_test.rb
@@ -0,0 +1,87 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# frozen_string_literal: true
+
+require "digest"
+require "minitest/autorun"
+require "rbconfig"
+require "rubygems/package"
+require "tmpdir"
+
+class NativeGemTest < Minitest::Test
+  def setup
+    @source_spec = Gem::Specification.load("opendal.gemspec")
+    @platform = Gem::Platform.new(ENV.fetch("OPENDAL_RUBY_PLATFORM", 
Gem::Platform.local.to_s))
+    @packages = 
Dir.glob("pkg/#{@source_spec.name}-#{@source_spec.version}-*.gem").map do |path|
+      Gem::Package.new(path)
+    end.select { |package| package.spec.platform == @platform }
+    @requirements = {
+      ">= 3.3, < 4.1.dev" => %w[3.3 3.4 4.0],
+      "~> 3.3.0" => %w[3.3],
+      "~> 3.4.0" => %w[3.4],
+      "~> 4.0.0" => %w[4.0]
+    }
+
+    assert_equal 4, @packages.size, "Build the native gems with rake 
native:opendal first"
+  end
+
+  def test_compatibility_metadata
+    assert_equal @requirements.keys.sort, @packages.map { |package| 
package.spec.required_ruby_version.to_s }.sort
+
+    @packages.each do |package|
+      spec = package.spec
+      assert_equal @source_spec.name, spec.name
+      assert_equal @source_spec.version, spec.version
+      assert_empty spec.extensions
+      assert_empty spec.requirements
+      assert 
spec.required_rubygems_version.satisfied_by?(Gem::Version.new("4.1.0"))
+      assert_equal spec.required_ruby_version == Gem::Requirement.new(">= 
3.3", "< 4.1.dev"),
+        spec.required_rubygems_version.satisfied_by?(Gem::Version.new("4.0.0"))
+    end
+  end
+
+  def test_runtime_and_legal_files_without_rust_build_inputs
+    required_files = @source_spec.files.grep(%r{\Alib/.*\.rb\z})
+    required_files.concat(Dir.glob("{LICENSE*,NOTICE*,DEPENDENCIES*}").select 
{ |path| File.file?(path) })
+    refute_empty required_files.grep(/LICENSE/)
+    refute_empty required_files.grep(/NOTICE/)
+
+    @packages.each do |package|
+      assert_empty required_files - package.contents
+      assert_empty 
package.contents.grep(%r{\A(?:src/|core/|Cargo\.toml\z|Cargo\.lock\z|build\.rs\z)})
+    end
+  end
+
+  def test_each_gem_contains_its_compiled_extensions
+    extension_file = "opendal_ruby.#{RbConfig::CONFIG.fetch("DLEXT")}"
+
+    @packages.each do |package|
+      versions = @requirements.fetch(package.spec.required_ruby_version.to_s)
+      expected_paths = versions.map { |version| 
"lib/opendal_ruby/#{version}/#{extension_file}" }
+      assert_equal expected_paths.sort, 
package.contents.grep(/\.(?:so|bundle)\z/).sort
+
+      Dir.mktmpdir do |directory|
+        package.extract_files(directory)
+        versions.zip(expected_paths).each do |version, path|
+          assert_equal 
Digest::SHA256.file("tmp/native/#{version}/#{extension_file}").hexdigest,
+            Digest::SHA256.file(File.join(directory, path)).hexdigest
+        end
+      end
+    end
+  end
+end

Reply via email to