Skip to content

Commit

Permalink
Rewrite generate to use Thor rather than Rubigen
Browse files Browse the repository at this point in the history
Rubigen was minimally maintained, and Thor is better maintained as well
as being the generator framework used by Rails since 3.0. This should
also fix generating gateways when using newer versions of ActiveSupport,
since Thor has no dependency on ActiveSupport at all (see activemerchant#453).

Closes activemerchant#453, activemerchant#502.
  • Loading branch information
ntalbott committed Nov 9, 2012
1 parent fb222cb commit aa9d15e
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 284 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Orbital gateway: Fix status of void result [jonm-okc]
* Add Redsys gateway [samlown]
* Cybersource gateway: Add support for subscription credit [fabiokr]
* Use Thor for generators [ntalbott]

== Version 1.28.0 (August 10, 2012)

Expand Down
2 changes: 1 addition & 1 deletion activemerchant.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('rake')
s.add_development_dependency('mocha', '~> 0.11.3')
s.add_development_dependency('rails', '>= 2.3.14')
s.add_development_dependency('rubigen')
s.add_development_dependency('thor')
s.signing_key = ENV['GEM_PRIVATE_KEY']
s.cert_chain = ['gem-public_cert.pem']
end
33 changes: 33 additions & 0 deletions generators/active_merchant_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "thor/group"

class ActiveMerchantGenerator < Thor::Group
include Thor::Actions

argument :name
class_option :destroy, :type => :boolean, :desc => "Destroys rather than generates the gateway"

def initialize(*args)
super
rescue Thor::InvocationError
at_exit{print self.class.help(shell)}
raise
end

protected

def template(source, dest)
if options[:destroy]
remove_file dest
else
super
end
end

def identifier
@identifier ||= class_name.gsub(%r{([A-Z])}){|m| "_#{$1.downcase}"}.sub(%r{^_}, "")
end

def class_name
@class_name ||= name.gsub(%r{(^[a-z])|_([a-zA-Z])}){|m| ($1||$2).upcase}
end
end
5 changes: 0 additions & 5 deletions generators/gateway/USAGE

This file was deleted.

72 changes: 18 additions & 54 deletions generators/gateway/gateway_generator.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,25 @@
class GatewayGenerator < RubiGen::Base
LIB_DIR = "lib/active_merchant/billing/gateways/"
UNIT_TEST_DIR = "test/unit/gateways/"
REMOTE_TEST_DIR = "test/remote/gateways/"


default_options :author => nil

attr_reader :name

def initialize(runtime_args, runtime_options = {})
super
usage if args.length < 1
@name = args.shift
extract_options
end

def class_name
@name.classify
end
require "thor/group"

def manifest
record do |m|

m.directory LIB_DIR
m.directory UNIT_TEST_DIR
m.directory REMOTE_TEST_DIR
class GatewayGenerator < ActiveMerchantGenerator
source_root File.expand_path("..", __FILE__)

m.template 'gateway.rb', LIB_DIR + "#{name}.rb"
m.template 'gateway_test.rb', UNIT_TEST_DIR + "#{name}_test.rb"
m.template 'remote_gateway_test.rb', REMOTE_TEST_DIR + "remote_#{name}_test.rb"
end
def generate
template "templates/gateway.rb", gateway_file
template "templates/gateway_test.rb", gateway_test_file
template "templates/remote_gateway_test.rb", remote_gateway_test_file
end

protected
def banner
<<-EOS
Creates a ...

USAGE: #{$0} #{spec.name} name"
EOS
end
def gateway_file
"lib/active_merchant/billing/gateways/#{identifier}.rb"
end

def gateway_test_file
"test/unit/gateways/#{identifier}_test.rb"
end

def add_options!(opts)
# opts.separator ''
# opts.separator 'Options:'
# For each option below, place the default
# at the top of the file next to "default_options"
# opts.on("-a", "--author=\"Your Name\"", String,
# "Some comment about this option",
# "Default: none") { |options[:author]| }
# opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
end

def extract_options
# for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
# Templates can access these value via the attr_reader-generated methods, but not the
# raw instance variable value.
# @author = options[:author]
end
end
def remote_gateway_test_file
"lib/active_merchant/billing/gateways/remote_#{identifier}_test.rb"
end
end
2 changes: 1 addition & 1 deletion generators/gateway/templates/remote_gateway_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Remote<%= class_name %>Test < Test::Unit::TestCase
def setup
@gateway = <%= class_name %>Gateway.new(fixtures(:<%= class_name.underscore %>))
@gateway = <%= class_name %>Gateway.new(fixtures(:<%= identifier %>))

@amount = 100
@credit_card = credit_card('4000100011112224')
Expand Down
5 changes: 0 additions & 5 deletions generators/integration/USAGE

This file was deleted.

81 changes: 19 additions & 62 deletions generators/integration/integration_generator.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,25 @@
class IntegrationGenerator < RubiGen::Base
BASE_DIR = "lib/active_merchant/billing/integrations/"
TEST_DIR = "test/unit/integrations/"

default_options :author => nil

attr_reader :name

def initialize(runtime_args, runtime_options = {})
super
usage if args.size < 1
@name = args.shift
extract_options
end

def class_name
name.classify
end

def lib_dir
BASE_DIR + name
end
require "thor/group"

class IntegrationGenerator < ActiveMerchantGenerator
source_root File.expand_path("..", __FILE__)

def manifest
record do |m|
# Ensure appropriate folder(s) exists
m.directory lib_dir
m.directory TEST_DIR
m.directory TEST_DIR + "helpers"
m.directory TEST_DIR + "notifications"

m.template 'integration.rb', "#{lib_dir}.rb"
m.template 'helper.rb', lib_dir + "/helper.rb"
m.template 'notification.rb', lib_dir + "/notification.rb"

m.template 'module_test.rb', TEST_DIR + "#{name}_module_test.rb"
m.template 'helper_test.rb', TEST_DIR + "helpers/#{name}_helper_test.rb"
m.template 'notification_test.rb', TEST_DIR + "notifications/#{name}_notification_test.rb"
end
def generate
template "templates/integration.rb", "#{lib}.rb"
template "templates/helper.rb", "#{lib}/helper.rb"
template "templates/notification.rb", "#{lib}/notification.rb"

template "templates/module_test.rb", "#{test_dir}/#{identifier}_module_test.rb"
template "templates/helper_test.rb", "#{test_dir}/helpers/#{identifier}_helper_test.rb"
template "templates/notification_test.rb", "#{test_dir}/notifications/#{identifier}_notification_test.rb"
end

protected
def banner
<<-EOS
Creates a ...

USAGE: #{$0} #{spec.name} name"
EOS
end
def lib
"lib/active_merchant/billing/integrations/#{identifier}"
end

def add_options!(opts)
# opts.separator ''
# opts.separator 'Options:'
# For each option below, place the default
# at the top of the file next to "default_options"
# opts.on("-a", "--author=\"Your Name\"", String,
# "Some comment about this option",
# "Default: none") { |options[:author]| }
# opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
end

def extract_options
# for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
# Templates can access these value via the attr_reader-generated methods, but not the
# raw instance variable value.
# @author = options[:author]
end
end
def test_dir
"test/unit/integrations"
end
end
4 changes: 2 additions & 2 deletions generators/integration/templates/integration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require File.dirname(__FILE__) + '/<%= name %>/helper.rb'
require File.dirname(__FILE__) + '/<%= name %>/notification.rb'
require File.dirname(__FILE__) + '/<%= identifier %>/helper.rb'
require File.dirname(__FILE__) + '/<%= identifier %>/notification.rb'

module ActiveMerchant #:nodoc:
module Billing #:nodoc:
Expand Down
22 changes: 11 additions & 11 deletions generators/integration/templates/notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class <%= class_name %>NotificationTest < Test::Unit::TestCase
include ActiveMerchant::Billing::Integrations
def setup
@<%= name %> = <%= class_name %>::Notification.new(http_raw_data)
@<%= identifier %> = <%= class_name %>::Notification.new(http_raw_data)
end
def test_accessors
assert @<%= name %>.complete?
assert_equal "", @<%= name %>.status
assert_equal "", @<%= name %>.transaction_id
assert_equal "", @<%= name %>.item_id
assert_equal "", @<%= name %>.gross
assert_equal "", @<%= name %>.currency
assert_equal "", @<%= name %>.received_at
assert @<%= name %>.test?
assert @<%= identifier %>.complete?
assert_equal "", @<%= identifier %>.status
assert_equal "", @<%= identifier %>.transaction_id
assert_equal "", @<%= identifier %>.item_id
assert_equal "", @<%= identifier %>.gross
assert_equal "", @<%= identifier %>.currency
assert_equal "", @<%= identifier %>.received_at
assert @<%= identifier %>.test?
end
def test_compositions
assert_equal Money.new(3166, 'USD'), @<%= name %>.amount
assert_equal Money.new(3166, 'USD'), @<%= identifier %>.amount
end

# Replace with real successful acknowledgement code
Expand All @@ -31,7 +31,7 @@ def test_send_acknowledgement
end

def test_respond_to_acknowledge
assert @<%= name %>.respond_to?(:acknowledge)
assert @<%= identifier %>.respond_to?(:acknowledge)
end

private
Expand Down
14 changes: 0 additions & 14 deletions script/destroy

This file was deleted.

22 changes: 12 additions & 10 deletions script/generate
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env ruby
APP_ROOT = File.join(File.dirname(__FILE__), '..')
require "rubygems"
require "thor"

begin
require 'rubigen'
rescue LoadError
require 'rubygems'
require 'rubigen'
require File.expand_path("../../generators/active_merchant_generator", __FILE__)

Dir[File.expand_path("../..", __FILE__) + "/generators/*/*.rb"].each do |generator|
require generator
end

class Generate < Thor
register(GatewayGenerator, "gateway", "gateway NAME", "Generates a new gateway.")
register(IntegrationGenerator, "integration", "integration NAME", "Generates a new integration.")
end
require 'rubigen/scripts/generate'

ARGV.shift if ['--help', '-h'].include?(ARGV[0])
RubiGen::Base.use_component_sources! [:activemerchant, :rubygems, :test_unit]
RubiGen::Scripts::Generate.new.run(ARGV)
Generate.start
46 changes: 0 additions & 46 deletions test/unit/generators/test_gateway_generator.rb

This file was deleted.

Loading

0 comments on commit aa9d15e

Please sign in to comment.