Skip to content

Instantly share code, notes, and snippets.

@renier
Created March 27, 2014 18:43
Show Gist options
  • Save renier/9815009 to your computer and use it in GitHub Desktop.
Save renier/9815009 to your computer and use it in GitHub Desktop.
Is there a better way to pick up included files for 'rake package'?
require 'rake/packagetask'
require 'bundler/gem_tasks'
require 'rubocop/rake_task'
require 'find'
require 'pathname'
ROOT_DIR = File.dirname(__FILE__)
spec = Gem::Specification::load("#{Dir.glob(ROOT_DIR + '/*.gemspec')[0]}")
task :rubocop do
Rubocop::RakeTask.new
end
ignored = File.open("#{ROOT_DIR}/.gitignore").readlines.map { |f| f.chomp }
ignored += ['.git', '.gitignore' ]
Rake::PackageTask.new(spec.name, spec.version.to_s) do |p|
p.need_tar_bz2 = p.need_zip = true
included = []
Find.find(ROOT_DIR) do |path|
path = Pathname.new(path).relative_path_from(Pathname.new(ROOT_DIR)).to_s
ok = true
ignored.each do |ignore|
if File.fnmatch(ignore, path)
Find.prune if FileTest.directory?(path)
ok = false
break
end
end
included << path if ok
end
p.package_files = included
end
task default: 'rubocop'
@renier
Copy link
Author

renier commented Mar 27, 2014

Yes, there is a much better way. Asked on IRC, and someone pointed to git ls-files used by bundler. Since I'm already loading my gemspec, and it uses the same method to fill in the files, I can just use this:

p.package_files = spec.files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment