Rails pluginをこまめにつくるようになったのでrails plugin new用のtemplateを用意した。

Rails pluginをこまめにつくるようになったのでrails plugin new用のtemplateを用意した。

とにかく一発でできるようにしようとgsub_fileとバッククオートの乱暴なやつになった。 spec.add_development_dependencyとか追加できるコマンドはあるのだろうか。

rails plugin new

rails _4.0.0_ plugin new my_plugin_name --skip-test-unit --skip-bundle --dummy-path=spec/dummy -m plugin_template.rb 

template.rb

gemspec_path = "#{self.name}.gemspec"

gsub_file gemspec_path, %r{TODO\: Your name}, 'mmmpa'
gsub_file gemspec_path, %r{TODO\: Your email}, 'mmmpa.mmmpa@gmail.com'
gsub_file gemspec_path, '|s|', '|spec|'
gsub_file gemspec_path, 's.', 'spec.'
gsub_file gemspec_path, 'spec.homepage    = "TODO"', 'spec.homepage    = "http://mmmpa.net"'
gsub_file gemspec_path, %r{^.+spec\.files[\s\S]*}, <<EOS
  spec.files         = `git ls-files`.split("\\n")
  spec.test_files    = `git ls-files -- {test,spec,features}/*`.split("\\n")
  spec.executables   = `git ls-files -- bin/*`.split("\\n").map{ |f| File.basename(f) }
  spec.require_paths = ["lib"]
  spec.add_dependency "rails", "~> 4.0"

  spec.add_development_dependency "bundler", "~> 1.10"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "sqlite3"
  spec.add_development_dependency "rspec"
  spec.add_development_dependency "rspec-rails"
  spec.add_development_dependency "rspec-html-matchers"
  spec.add_development_dependency "factory_girl_rails"
  spec.add_development_dependency "coveralls"
  spec.add_development_dependency "rb-readline"
end
EOS

gsub_file 'MIT-LICENSE', 'YOURNAME', 'mmmpa'
remove_file 'README.rdoc'

inside('') do
	run "cp #{__dir__}/template_files/.travis.yml .travis.yml"
	run "cp #{__dir__}/template_files/.coveralls.yml .coveralls.yml"
	run "cp #{__dir__}/template_files/.codeclimate.yml .codeclimate.yml"
	run "bundle install"
end

inside('spec/dummy') do
	run "cp config/database.yml config/database.def.yml"
	run "rails generate rspec:install"
	run "mv spec/spec_helper.rb ../spec_helper.rb"
	run "mv spec/rails_helper.rb ../rails_helper.rb"
	run "mv .rspec ../../.rspec"
	run "rm -fr spec"
end

gsub_file 'spec/spec_helper.rb', /\A/, <<EOS
require 'coveralls'
Coveralls.wear!
EOS

gsub_file 'spec/rails_helper.rb', %r{[\s\S]*^require 'rspec/rails'}, <<EOS
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../dummy/config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'factory_girl_rails'
require 'rspec-html-matchers'
EOS

gsub_file 'spec/rails_helper.rb', 'RSpec.configure do |config|', <<EOS
RSpec.configure do |config|
	config.include RSpecHtmlMatchers
	config.include FactoryGirl::Syntax::Methods

	config.before(:all) do
		FactoryGirl.reload
	end
EOS

gsub_file 'spec/dummy/config/application.rb', '  class Application < Rails::Application', <<EOS
	class Application < Rails::Application
		config.generators do |g|
			g.assets false
			g.helper false
			g.view false

			g.test_framework :rspec, fixtures: true, view_specs: false, helper_specs: false, routing_specs:
							false, controller_specs: true, request_specs: true
			g.fixture_replacement :factory_girl, dir: 'spec/factories'
		end
EOS

inside('') do
	`git init`
	`git config user.name mmmpa`
	`git config user.email "mmmpa.mmmpa@gmail.com"`
	`git add .`
	`git commit -m 'readied'`
	`git remote add origin github:mmmpa/#{self.name}.git`
end