summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Medeiros <me@andremedeiros.info>2014-02-21 14:00:22 +1100
committerZachary Scott <e@zzak.io>2014-03-02 22:20:07 -0800
commitf3321c797873cc5cf89f7a256db0479e593dc81f (patch)
tree16531bd84518b6ede53682c92fe5f57faad99f88
parent04ff3bd9ad024471dacca7513d473eed40325aee (diff)
downloadbundler-f3321c797873cc5cf89f7a256db0479e593dc81f.tar.gz
Added the --ext option to the gem command. This generates a skeleton for C extensions.
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/bundler/cli.rb1
-rw-r--r--lib/bundler/cli/gem.rb22
-rw-r--r--lib/bundler/templates/newgem/Rakefile.tt8
-rw-r--r--lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt3
-rw-r--r--lib/bundler/templates/newgem/ext/newgem/newgem.c.tt9
-rw-r--r--lib/bundler/templates/newgem/ext/newgem/newgem.h.tt6
-rw-r--r--lib/bundler/templates/newgem/lib/newgem.rb.tt3
-rw-r--r--lib/bundler/templates/newgem/newgem.gemspec.tt6
-rw-r--r--spec/commands/newgem_spec.rb18
10 files changed, 70 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d7d90f36d1..e557b7bd1d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,7 @@ Features:
- display the `post_install_message` for gems installed via :git (@phallstrom)
- `bundle outdated --strict` now only reports allowed updates (@davidblondeau)
- `bundle show --verbose` Add gem ummary to the output (@lardcanoe)
+ - `bundle gem GEM --ext` now generates a skeleton for a C extension (@superdealloc)
## 1.5.3 (2014-02-06)
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index 4a6dad32c6..df54c47f96 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -329,6 +329,7 @@ module Bundler
:lazy_default => [ENV['BUNDLER_EDITOR'], ENV['VISUAL'], ENV['EDITOR']].find{|e| !e.nil? && !e.empty? },
:required => false, :banner => "/path/to/your/editor",
:desc => "Open generated gemspec in the specified editor (defaults to $EDITOR or $BUNDLER_EDITOR)"
+ method_option :ext, :type => :boolean, :detailt => false, :banner => "Generate the boilerplate for C extension code"
def gem(name)
require 'bundler/cli/gem'
diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb
index aabdf639dd..5366e66701 100644
--- a/lib/bundler/cli/gem.rb
+++ b/lib/bundler/cli/gem.rb
@@ -9,6 +9,7 @@ module Bundler
def run
name = gem_name.chomp("/") # remove trailing slash if present
+ underscored_name = name.tr('-', '_')
namespaced_path = name.tr('-', '/')
target = File.join(Dir.pwd, name)
constant_name = name.split('_').map{|p| p[0..0].upcase + p[1..-1] }.join
@@ -17,13 +18,15 @@ module Bundler
git_user_name = `git config user.name`.chomp
git_user_email = `git config user.email`.chomp
opts = {
- :name => name,
- :namespaced_path => namespaced_path,
- :constant_name => constant_name,
- :constant_array => constant_array,
- :author => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
- :email => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
- :test => options[:test]
+ :name => name,
+ :underscored_name => underscored_name,
+ :namespaced_path => namespaced_path,
+ :constant_name => constant_name,
+ :constant_array => constant_array,
+ :author => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
+ :email => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
+ :test => options[:test],
+ :ext => options[:ext]
}
gemspec_dest = File.join(target, "#{name}.gemspec")
thor.template(File.join("newgem/Gemfile.tt"), File.join(target, "Gemfile"), opts)
@@ -49,6 +52,11 @@ module Bundler
if options[:test]
thor.template(File.join("newgem/.travis.yml.tt"), File.join(target, ".travis.yml"), opts)
end
+ if options[:ext]
+ thor.template(File.join("newgem/ext/newgem/extconf.rb.tt"), File.join(target, "ext/#{name}/extconf.rb"), opts)
+ thor.template(File.join("newgem/ext/newgem/newgem.h.tt"), File.join(target, "ext/#{name}/#{underscored_name}.h"), opts)
+ thor.template(File.join("newgem/ext/newgem/newgem.c.tt"), File.join(target, "ext/#{name}/#{underscored_name}.c"), opts)
+ end
Bundler.ui.info "Initializing git repo in #{target}"
Dir.chdir(target) { `git init`; `git add .` }
diff --git a/lib/bundler/templates/newgem/Rakefile.tt b/lib/bundler/templates/newgem/Rakefile.tt
index 2ecf1864b1..f4fc3c6ee9 100644
--- a/lib/bundler/templates/newgem/Rakefile.tt
+++ b/lib/bundler/templates/newgem/Rakefile.tt
@@ -14,3 +14,11 @@ RSpec::Core::RakeTask.new(:spec)
task :default => :spec
<% end -%>
+
+<% if config[:ext] -%>
+require "rake/extensiontask"
+
+Rake::ExtensionTask.new("<%=config[:underscored_name]%>") do |ext|
+ ext.lib_dir = "lib/<%=config[:namespaced_path]%>"
+end
+<% end -%>
diff --git a/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt b/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt
new file mode 100644
index 0000000000..3fd81816f1
--- /dev/null
+++ b/lib/bundler/templates/newgem/ext/newgem/extconf.rb.tt
@@ -0,0 +1,3 @@
+require "mkmf"
+
+create_makefile(<%=config[:underscored_name].inspect%>)
diff --git a/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt b/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt
new file mode 100644
index 0000000000..5dad3641ff
--- /dev/null
+++ b/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt
@@ -0,0 +1,9 @@
+#include "<%=config[:underscored_name]%>.h"
+
+VALUE rb_m<%=config[:constant_array].join%>;
+
+void
+Init_<%=config[:underscored_name]%>(void)
+{
+ rb_m<%=config[:constant_array].join%> = rb_define_module(<%=config[:constant_name].inspect%>);
+}
diff --git a/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt b/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt
new file mode 100644
index 0000000000..960fdfb492
--- /dev/null
+++ b/lib/bundler/templates/newgem/ext/newgem/newgem.h.tt
@@ -0,0 +1,6 @@
+#ifndef <%=config[:underscored_name].upcase%>_H
+#define <%=config[:underscored_name].upcase%>_H 1
+
+#include "ruby.h"
+
+#endif /* <%=config[:underscored_name].upcase%>_H */
diff --git a/lib/bundler/templates/newgem/lib/newgem.rb.tt b/lib/bundler/templates/newgem/lib/newgem.rb.tt
index 35920cf886..b3f816bac6 100644
--- a/lib/bundler/templates/newgem/lib/newgem.rb.tt
+++ b/lib/bundler/templates/newgem/lib/newgem.rb.tt
@@ -1,4 +1,7 @@
require "<%=config[:namespaced_path]%>/version"
+<%- if config[:ext] -%>
+require "<%=config[:namespaced_path]%>/<%=config[:underscored_name]%>"
+<%- end -%>
<%- config[:constant_array].each_with_index do |c,i| -%>
<%= ' '*i %>module <%= c %>
diff --git a/lib/bundler/templates/newgem/newgem.gemspec.tt b/lib/bundler/templates/newgem/newgem.gemspec.tt
index 34072c3083..cc5109b1a2 100644
--- a/lib/bundler/templates/newgem/newgem.gemspec.tt
+++ b/lib/bundler/templates/newgem/newgem.gemspec.tt
@@ -8,6 +8,9 @@ Gem::Specification.new do |spec|
spec.version = <%=config[:constant_name]%>::VERSION
spec.authors = [<%=config[:author].inspect%>]
spec.email = [<%=config[:email].inspect%>]
+<% if config[:ext] -%>
+ spec.extensions = ["ext/<%=config[:underscored_name]%>/extconf.rb"]
+<% end -%>
spec.summary = %q{TODO: Write a short summary. Required.}
spec.description = %q{TODO: Write a longer description. Optional.}
spec.homepage = ""
@@ -20,6 +23,9 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> <%= Bundler::VERSION.split(".")[0..1].join(".") %>"
spec.add_development_dependency "rake"
+<% if config[:ext] -%>
+ spec.add_development_dependency "rake-compiler"
+<% end -%>
<% if config[:test] -%>
spec.add_development_dependency "<%=config[:test]%>"
<% end -%>
diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb
index 17ef9f6f36..6791c36c16 100644
--- a/spec/commands/newgem_spec.rb
+++ b/spec/commands/newgem_spec.rb
@@ -399,5 +399,23 @@ describe "bundle gem" do
expect(bundled_app("test-gem/test/minitest_helper.rb")).to_not exist
end
end
+
+ context "--ext parameter set" do
+ before do
+ reset!
+ in_app_root
+ bundle "gem test_gem --ext"
+ end
+
+ it "builds ext skeleton" do
+ expect(bundled_app("test_gem/ext/test_gem/extconf.rb")).to exist
+ expect(bundled_app("test_gem/ext/test_gem/test_gem.h")).to exist
+ expect(bundled_app("test_gem/ext/test_gem/test_gem.c")).to exist
+ end
+
+ it "includes rake-compiler" do
+ expect(bundled_app("test_gem/test_gem.gemspec").read).to include('spec.add_development_dependency "rake-compiler"')
+ end
+ end
end
end