From d0cb8265c4a94e906b5b969045eb4f4b067f0e6f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 31 Oct 2019 18:31:48 -0300 Subject: [PATCH 1/2] add support for explicitly specifying some artifacts to add The 'rice' gem has bunch of files in ruby/lib that is not part of the gem proper - it is to be used by the libraries. It's obviously a misuse of the Rubygems system, but a useful one so far. This adds the --artifact PATH argument that allows to: - add a file explicitly - add a complete directory tree --- lib/rubygems/commands/compile_command.rb | 4 ++ lib/rubygems/compiler.rb | 14 +++++- test/rubygems/test_gem_compiler.rb | 62 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/rubygems/commands/compile_command.rb b/lib/rubygems/commands/compile_command.rb index 87b7fc1..4377b81 100644 --- a/lib/rubygems/commands/compile_command.rb +++ b/lib/rubygems/commands/compile_command.rb @@ -13,6 +13,10 @@ def initialize options[:include_shared_dir] = value end + add_option "--artifact PATH", "Additional artifact to package (relative to the gem dir)" do |value, options| + (options[:artifacts] ||= []) << value + end + add_option "--prune", "Clean non-existing files during re-packaging" do |value, options| options[:prune] = true end diff --git a/lib/rubygems/compiler.rb b/lib/rubygems/compiler.rb index 2db9e74..a770160 100644 --- a/lib/rubygems/compiler.rb +++ b/lib/rubygems/compiler.rb @@ -3,6 +3,7 @@ require "tmpdir" require "rubygems/installer" require "rubygems/package" +require 'find' class Gem::Compiler include Gem::UserInteraction @@ -23,7 +24,18 @@ def compile build_extensions - artifacts = collect_artifacts + artifacts = (@options[:artifacts] || []).map do |rel_path| + full_path = File.join(target_dir, rel_path) + if File.file?(full_path) + full_path + else + Find.enum_for(:find, full_path) + .find_all { |p| File.file?(p) } + .to_a + end + end.flatten + + artifacts.concat collect_artifacts if shared_dir = options[:include_shared_dir] shared_libs = collect_shared(shared_dir) diff --git a/test/rubygems/test_gem_compiler.rb b/test/rubygems/test_gem_compiler.rb index b246f0d..b8398d2 100644 --- a/test/rubygems/test_gem_compiler.rb +++ b/test/rubygems/test_gem_compiler.rb @@ -173,6 +173,68 @@ def test_compile_bundle_artifacts assert_includes spec.files, "lib/#{artifact}" end + def test_compile_bundle_additional_artifacts + util_reset_arch + + artifact = "foo.some_ext" + + gem_file = util_bake_gem("foo") { |s| + util_fake_extension s, "foo", util_custom_configure(artifact) + } + + compiler = Gem::Compiler.new( + gem_file, :output => @output_dir, + :artifacts => ['lib/foo.some_ext'] + ) + output_gem = nil + + use_ui @ui do + output_gem = compiler.compile + end + + assert_path_exists File.join(@output_dir, output_gem) + spec = util_read_spec File.join(@output_dir, output_gem) + + assert_includes spec.files, "lib/#{artifact}" + end + + def test_compile_bundle_additional_directory_artifacts + util_reset_arch + + script = <<-EO_MKRF + File.open("Rakefile", "w") do |f| + f.puts <<-EOF + task :default do + lib_dir = ENV["RUBYARCHDIR"] || ENV["RUBYLIBDIR"] + FileUtils.mkdir_p File.join(lib_dir, 'baz', 'somefile.txt') + FileUtils.mkdir_p File.join(lib_dir, 'foo', 'bar') + touch File.join(lib_dir, 'foo', 'bar', 'somefile.txt') + end + EOF + end + EO_MKRF + + gem_file = util_bake_gem("foo") { |s| + util_fake_extension s, "foo", script + } + + compiler = Gem::Compiler.new( + gem_file, :output => @output_dir, + :artifacts => ['lib/foo'] + ) + output_gem = nil + + use_ui @ui do + output_gem = compiler.compile + end + + assert_path_exists File.join(@output_dir, output_gem) + spec = util_read_spec File.join(@output_dir, output_gem) + + assert_includes spec.files, "lib/foo/bar/somefile.txt" + refute_includes spec.files, "lib/baz/somefile.txt" + end + # We need to check that tempdir paths that contain spaces as are handled # properly on Windows. In some cases, Dir.tmpdir may returned shortened # versions of these components, e.g. "C:/Users/JOHNDO~1/AppData/Local/Temp" From 7162463d8009ac66dcf0323b66c1e0bab8de0ef0 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 6 Nov 2019 13:19:29 -0300 Subject: [PATCH 2/2] use globs and allow sequential application of include and exclude patterns --- lib/rubygems/commands/compile_command.rb | 8 +++-- lib/rubygems/compiler.rb | 17 +++++---- test/rubygems/test_gem_compiler.rb | 46 +++++++++++++++++++++--- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/lib/rubygems/commands/compile_command.rb b/lib/rubygems/commands/compile_command.rb index 4377b81..2d948e3 100644 --- a/lib/rubygems/commands/compile_command.rb +++ b/lib/rubygems/commands/compile_command.rb @@ -13,8 +13,12 @@ def initialize options[:include_shared_dir] = value end - add_option "--artifact PATH", "Additional artifact to package (relative to the gem dir)" do |value, options| - (options[:artifacts] ||= []) << value + add_option "--include GLOB", "Additional artifact to package (relative to the gem dir)" do |value, options| + (options[:artifacts] ||= []) << [true, value] + end + + add_option "--exclude GLOB", "Additional artifact to package (relative to the gem dir)" do |value, options| + (options[:artifacts] ||= []) << [false, value] end add_option "--prune", "Clean non-existing files during re-packaging" do |value, options| diff --git a/lib/rubygems/compiler.rb b/lib/rubygems/compiler.rb index a770160..2bdc897 100644 --- a/lib/rubygems/compiler.rb +++ b/lib/rubygems/compiler.rb @@ -24,18 +24,17 @@ def compile build_extensions - artifacts = (@options[:artifacts] || []).map do |rel_path| - full_path = File.join(target_dir, rel_path) - if File.file?(full_path) - full_path + artifacts = collect_artifacts + + (@options[:artifacts] || []).map do |include, glob| + resolved = Dir.glob("#{target_dir}/#{glob}") + if include + artifacts.concat(resolved) else - Find.enum_for(:find, full_path) - .find_all { |p| File.file?(p) } - .to_a + artifacts -= resolved end - end.flatten + end - artifacts.concat collect_artifacts if shared_dir = options[:include_shared_dir] shared_libs = collect_shared(shared_dir) diff --git a/test/rubygems/test_gem_compiler.rb b/test/rubygems/test_gem_compiler.rb index b8398d2..1424407 100644 --- a/test/rubygems/test_gem_compiler.rb +++ b/test/rubygems/test_gem_compiler.rb @@ -173,7 +173,7 @@ def test_compile_bundle_artifacts assert_includes spec.files, "lib/#{artifact}" end - def test_compile_bundle_additional_artifacts + def test_compile_bundle_include util_reset_arch artifact = "foo.some_ext" @@ -184,7 +184,7 @@ def test_compile_bundle_additional_artifacts compiler = Gem::Compiler.new( gem_file, :output => @output_dir, - :artifacts => ['lib/foo.some_ext'] + :artifacts => [[true, 'lib/*.some_ext']] ) output_gem = nil @@ -198,7 +198,45 @@ def test_compile_bundle_additional_artifacts assert_includes spec.files, "lib/#{artifact}" end - def test_compile_bundle_additional_directory_artifacts + def test_compile_bundle_exclude_then_include + util_reset_arch + + script = <<-EO_MKRF + File.open("Rakefile", "w") do |f| + f.puts <<-EOF + task :default do + lib_dir = ENV["RUBYARCHDIR"] || ENV["RUBYLIBDIR"] + FileUtils.mkdir_p File.join(lib_dir, 'baz', 'file1.rb') + FileUtils.mkdir_p File.join(lib_dir, 'baz', 'file2.rb') + FileUtils.mkdir_p File.join(lib_dir, 'foo', 'bar') + touch File.join(lib_dir, 'foo', 'bar', 'somefile.txt') + end + EOF + end + EO_MKRF + + gem_file = util_bake_gem("foo") { |s| + util_fake_extension s, "foo", script + } + + compiler = Gem::Compiler.new( + gem_file, + output: @output_dir, + artifacts: [[false, '**/foo.rb'], [true, 'lib/foo/**/*']] + ) + output_gem = nil + + use_ui @ui do + output_gem = compiler.compile + end + + assert_path_exists File.join(@output_dir, output_gem) + spec = util_read_spec File.join(@output_dir, output_gem) + + assert_includes spec.files, "lib/foo/bar/somefile.txt" + end + + def test_compile_bundle_include_then_exclude util_reset_arch script = <<-EO_MKRF @@ -220,7 +258,7 @@ def test_compile_bundle_additional_directory_artifacts compiler = Gem::Compiler.new( gem_file, :output => @output_dir, - :artifacts => ['lib/foo'] + :artifacts => [[true, 'lib/**/*'], [false, 'lib/baz/*']] ) output_gem = nil