Skip to content
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

add support for explicitly specifying some artifacts to add #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/rubygems/commands/compile_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def initialize
options[:include_shared_dir] = value
end

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|
options[:prune] = true
end
Expand Down
11 changes: 11 additions & 0 deletions lib/rubygems/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "tmpdir"
require "rubygems/installer"
require "rubygems/package"
require 'find'

class Gem::Compiler
include Gem::UserInteraction
Expand All @@ -25,6 +26,16 @@ def compile

artifacts = collect_artifacts

(@options[:artifacts] || []).map do |include, glob|
resolved = Dir.glob("#{target_dir}/#{glob}")
if include
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I recommend any word other than include?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Missed that one. to_include ? to_be_included ? Or I could make it a mode variable that is either :include or :exclude. What do you think ?

artifacts.concat(resolved)
else
artifacts -= resolved
end
end


if shared_dir = options[:include_shared_dir]
shared_libs = collect_shared(shared_dir)

Expand Down
100 changes: 100 additions & 0 deletions test/rubygems/test_gem_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,106 @@ def test_compile_bundle_artifacts
assert_includes spec.files, "lib/#{artifact}"
end

def test_compile_bundle_include
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 => [[true, 'lib/*.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_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
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 => [[true, 'lib/**/*'], [false, 'lib/baz/*']]
)
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"
Expand Down