Skip to content

Instantly share code, notes, and snippets.

@falonofthetower
Created August 31, 2016 14:58
Show Gist options
  • Save falonofthetower/4d6ea0fbbae1b309fccd75420a96e4d6 to your computer and use it in GitHub Desktop.
Save falonofthetower/4d6ea0fbbae1b309fccd75420a96e4d6 to your computer and use it in GitHub Desktop.
# Add the current directory to the path Thor uses
# to look up files
RAILS_REQUIREMENT = "~> 4.2.6"
def apply_template!
add_template_repository_to_source_path
assert_minimum_rails_version
template "Gemfile.tt", :force => true
# assert_valid_options
assert_postgresql
copy_file "gitignore", ".gitignore", :force => true
copy_file "Capfile"
copy_file "config.ru", :force => true
copy_file "rspec", ".rspec"
apply "config/template.rb"
apply "app/template.rb"
end
def add_template_repository_to_source_path
if __FILE__ =~ %r{\Ahttps?://}
# this portiion is not working
source_paths.unshift(tempdir = Dir.mktmpdir("rails-template-"))
at_exit { FileUtils.remove_entry(tempdir) }
git :clone => [
"--quiet",
"https://peter_karth@bitbucket.org/peter_karth/yggdrasil.git",
tempdir
].map(&:shellescape).join(" ")
else
source_paths.unshift(File.dirname(__FILE__))
end
end
require "fileutils"
require "shellwords"
def assert_minimum_rails_version
requirement = Gem::Requirement.new(RAILS_REQUIREMENT)
rails_version = Gem::Version.new(Rails::VERSION::STRING)
return if requirement.satisfied_by?(rails_version)
prompt = "This template requires Rails #{RAILS_REQUIREMENT}. "\
"You are using #{rails_version}. Continue anyway?"
exit 1 if no?(prompt)
end
# Bail out if user has passed in contradictory generator options.
def assert_valid_options
valid_options = {
:skip_gemfile => false,
:skip_bundle => false,
:skip_git => false,
:skip_test_unit => false,
:edge => false
}
valid_options.each do |key, expected|
next unless options.key?(key)
actual = options[key]
unless actual == expected
fail Rails::Generators::Error, "Unsupported option: #{key}=#{actual}"
end
end
end
def assert_postgresql
return if IO.read("Gemfile") =~ /^\s*gem ['"]pg['"]/
fail Rails::Generators::Error,
"This template requires PostgreSQL, "\
"but the pg gem isn’t present in your Gemfile."
end
apply_template!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment