Created
February 12, 2018 05:25
-
-
Save TayKangSheng/9d2afe7696f468d7ab1cdae85d733b3e to your computer and use it in GitHub Desktop.
Bootstrap gem creation for rails application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The purpose is to create a Gem that has a dummy rails application and uses RSpec as the test framework. | |
# Check whether gem name is given. | |
if ARGV.length != 1 | |
puts "ERROR: We need exactly one parameter. The name of a gem. Eg. MyGem" | |
exit; | |
end | |
# Initializing dependencies and utilities | |
require 'active_support/inflector' | |
require 'open3' | |
def write_line_under(file_name, regex, string) | |
open(file_name, 'r+') do |f| | |
while (current_line = f.gets) | |
break if regex.match(current_line) | |
end | |
pos = f.pos | |
rest = f.read | |
f.seek pos | |
f.write string | |
f.write rest | |
end | |
end | |
def run_shell_command(shell_command_in_string) | |
puts "$ #{shell_command_in_string}" | |
Open3.popen3(shell_command_in_string) do |stdout, stderr, status, thread| | |
while line=stderr.gets do | |
puts(line) | |
end | |
end | |
end | |
def replace_text(file_name, regex, string) | |
file_contents = File.open(file_name).read | |
file_contents.gsub!(regex, string) | |
File.open(file_name, 'w+') do |f| | |
f.write(file_contents) | |
end | |
end | |
## Script Starts HERE | |
# Check provided gem name against convention | |
gem_name = ARGV[0] | |
if /[A-Z]/.match(gem_name) | |
puts "ERROR: Gem Name should not contain uppercase letter. Please refer to http://guides.rubygems.org/name-your-gem/ for Gems naming convention" | |
exit; | |
end | |
# Create rails plugin gem | |
# -T : Ignore tests | |
# --dummy-path : set path to create dummy rails application to spec/dummy. Prepare to set up RSpec later | |
run_shell_command("rails plugin new #{gem_name} -T --dummy-path=spec/dummy") | |
# Change directory into gem directory | |
Dir.chdir gem_name | |
# Add rspec-rails to gemspec file | |
puts "INFO: Add rspec-rails to gemspec file" | |
write_line_under("#{gem_name.underscore}.gemspec", /s.add_development_dependency/, " s.add_development_dependency \"rspec-rails\"\n") | |
# Fix gemspec details | |
puts "INFO: Setting gemspec details to default values" | |
replace_text("#{gem_name.underscore}.gemspec", /s.authors.*=.*/, "s.authors = [\"SP Digital\"]") | |
replace_text("#{gem_name.underscore}.gemspec", /s.email.*=.*/, "s.email = [\"[email protected]\"]") | |
replace_text("#{gem_name.underscore}.gemspec", /s.homepage.*=.*/, "s.homepage = \"https://code.in.spdigital.io\"") | |
replace_text("#{gem_name.underscore}.gemspec", /s.summary.*=.*/, "s.summary = \"This is a new Gem for rails!\"") | |
replace_text("#{gem_name.underscore}.gemspec", /s.summary.*=.*/, "s.summary = \"This is a new utilities Gem for rails!\"") | |
replace_text("#{gem_name.underscore}.gemspec", /s.description.*=.*/, "s.description = \"This is a new utilities Gem for rails!\"") | |
# Install dependencies | |
run_shell_command("bundle install") | |
# Set up RSpec | |
Dir.chdir "spec/dummy" | |
run_shell_command("ln -s ../../spec") | |
run_shell_command("rails generate rspec:install") | |
run_shell_command("cd -") | |
replace_text("spec/rails_helper.rb", /File\.expand_path\('..\/..\/config\/environment', __FILE__\)/, "File.expand_path(\"../dummy/config/environment\", __FILE__)") | |
# Test run RSpec | |
run_shell_command("rspec") | |
puts "INFO: #{gem_name} set up complete! Congratulations!" | |
puts " run `cd #{gem_name}` to start building your gem." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment