From 583ba98513e61a78d8c6232be847b4c4be47041a Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 14 Jul 2019 22:54:04 +0900 Subject: [PATCH] Install a stub file in binary gems only Co-authored-by: Nobuyoshi Nakada --- Rakefile | 2 ++ lib/psych.rb | 25 +++++++++++++------------ psych.gemspec | 3 ++- stub/psych.rb | 6 ++++++ 4 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 stub/psych.rb diff --git a/Rakefile b/Rakefile index 345edcd3..d72bb775 100644 --- a/Rakefile +++ b/Rakefile @@ -29,6 +29,8 @@ if RUBY_PLATFORM =~ /java/ else require 'rake/extensiontask' spec = Gem::Specification.load("psych.gemspec") + # Add stub/ on $LOAD_PATH only for binary gems + spec.require_paths.insert(0, *%w[stub]) Rake::ExtensionTask.new("psych", spec) do |ext| ext.lib_dir = File.join(*['lib', ENV['FAT_DIR']].compact) ext.cross_compile = true diff --git a/lib/psych.rb b/lib/psych.rb index 2a2ec2af..e8577811 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -1,21 +1,22 @@ # frozen_string_literal: true require 'psych/versions' -case RUBY_ENGINE -when 'jruby' - require 'psych_jars' - if JRuby::Util.respond_to?(:load_ext) - JRuby::Util.load_ext('org.jruby.ext.psych.PsychLibrary') + +# When required from the stub file, the C extension is already loaded +unless caller_locations(1, 1)[0].absolute_path == File.expand_path('../stub/psych.rb', __dir__) + case RUBY_ENGINE + when 'jruby' + require 'psych_jars' + if JRuby::Util.respond_to?(:load_ext) + JRuby::Util.load_ext('org.jruby.ext.psych.PsychLibrary') + else + require 'java'; require 'jruby' + org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false) + end else - require 'java'; require 'jruby' - org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false) - end -else - begin - require "#{RUBY_VERSION[/\d+\.\d+/]}/psych.so" - rescue LoadError require 'psych.so' end end + require 'psych/nodes' require 'psych/streaming' require 'psych/visitors' diff --git a/psych.gemspec b/psych.gemspec index 469ee5de..a699767e 100644 --- a/psych.gemspec +++ b/psych.gemspec @@ -41,7 +41,8 @@ DESCRIPTION "lib/psych/set.rb", "lib/psych/stream.rb", "lib/psych/streaming.rb", "lib/psych/syntax_error.rb", "lib/psych/tree_builder.rb", "lib/psych/versions.rb", "lib/psych/visitors.rb","lib/psych/visitors/depth_first.rb", "lib/psych/visitors/emitter.rb", "lib/psych/visitors/json_tree.rb", "lib/psych/visitors/to_ruby.rb", - "lib/psych/visitors/visitor.rb", "lib/psych/visitors/yaml_tree.rb", "lib/psych/y.rb", "psych.gemspec" + "lib/psych/visitors/visitor.rb", "lib/psych/visitors/yaml_tree.rb", "lib/psych/y.rb", "psych.gemspec", + "stub/psych.rb" ] s.rdoc_options = ["--main", "README.md"] diff --git a/stub/psych.rb b/stub/psych.rb new file mode 100644 index 00000000..3d11f30d --- /dev/null +++ b/stub/psych.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# This file is only on $LOAD_PATH when using a binary gem + +require "#{RUBY_VERSION[/\d+\.\d+/]}/psych.so" +require_relative '../lib/psych'