Created
February 6, 2019 12:39
-
-
Save sideshowbandana/a341bf8e0ede4c739c8abbd49809b908 to your computer and use it in GitHub Desktop.
generator test template
This file contains 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
Thor::Base.shell = Thor::Shell::Color | |
require 'yaml' | |
def truthy?(statement) | |
val = ask(statement) | |
['y', 'yes', ''].include?(val) | |
end | |
def eval_template(name) | |
instance_eval(File.read(File.dirname(__FILE__) + "/#{name}.rb")) | |
end | |
def update_config!(attrs) | |
config = File.exists?('.graphiticfg.yml') ? YAML.load_file('.graphiticfg.yml') : {} | |
config.merge!(attrs) | |
File.open('.graphiticfg.yml', 'w') { |f| f.write(config.to_yaml) } | |
end | |
def api_namespace | |
@api_namespace ||= begin | |
ns = prompt \ | |
header: "What is your API namespace?", | |
description: "This will be used as a route prefix, e.g. if you want the route '/books_api/v1/authors' your namespace would be '/books_api/v1'", | |
default: '/api/v1' | |
update_config!('namespace' => ns) | |
ns | |
end | |
end | |
def prompt(header: nil, description: nil, default: nil) | |
say(set_color("\n#{header}", :magenta, :bold)) if header | |
say("\n#{description}") if description | |
answer = ask(set_color("\n(default: #{default}):", :magenta, :bold)) | |
answer = default if answer.blank? && default != 'nil' | |
say(set_color("\nGot it!\n", :white, :bold)) | |
answer | |
end | |
welcome = <<-STR | |
\n | |
Welcome to the Graphiti generator! | |
======================================= | |
This will take care of some boilerplate for you, like adding gem dependencies and rspec helpers. | |
If you're worried there might be too much magic here, feel free to run 'git diff' at the end to see what happened. You can also learn more at our documentation website, https://jsonapi-suite.github.io/jsonapi_suite | |
STR | |
say(set_color(welcome.rstrip, :cyan, :bold)) | |
api_namespace | |
gsub_file 'Gemfile', /gem 'sqlite3'/ do |match| | |
match << ", '~> 1.3.0'" | |
end | |
gem 'graphiti', path: "../graphiti" | |
gem 'vandal_ui' | |
gem 'kaminari', '~> 1.0' | |
gem 'responders', '~> 2.4' | |
gem_group :development, :test do | |
gem 'rspec-rails', '~> 3.5.2' | |
gem 'factory_bot_rails', '~> 4.0' | |
gem 'faker', '~> 1.7' # keep here for seeds.rb | |
gem 'graphiti_spec_helpers' | |
end | |
gem_group :test do | |
gem 'database_cleaner', '~> 1.6' | |
end | |
after_bundle do | |
run 'bin/spring stop' | |
git :init | |
git add: '.' | |
run "bundle binstub rspec-core" | |
rails_command "generate rspec:install" | |
run "rm -rf test" | |
insert_into_file "spec/rails_helper.rb", :after => "require 'rspec/rails'\n" do | |
"require 'graphiti_spec_helpers/rspec'\n" | |
end | |
insert_into_file "spec/rails_helper.rb", :after => "RSpec.configure do |config|\n" do | |
<<-STR | |
# bootstrap database cleaner | |
config.before(:suite) do | |
DatabaseCleaner.strategy = :transaction | |
DatabaseCleaner.clean_with(:truncation) | |
end | |
config.around(:each) do |example| | |
begin | |
DatabaseCleaner.cleaning do | |
example.run | |
end | |
ensure | |
DatabaseCleaner.clean | |
end | |
end | |
STR | |
end | |
insert_into_file "spec/rails_helper.rb", :after => "RSpec.configure do |config|\n" do | |
<<-STR | |
config.before :each do | |
GraphitiErrors.disable! | |
end | |
STR | |
end | |
insert_into_file "spec/rails_helper.rb", :after => "RSpec.configure do |config|\n" do | |
" config.include GraphitiSpecHelpers::Sugar\n" | |
end | |
insert_into_file "spec/rails_helper.rb", :after => "RSpec.configure do |config|\n" do | |
" config.include GraphitiSpecHelpers::RSpec\n" | |
end | |
insert_into_file "spec/rails_helper.rb", :after => "RSpec.configure do |config|\n" do | |
" config.include FactoryBot::Syntax::Methods\n" | |
end | |
gsub_file "spec/rails_helper.rb", 'config.fixture_path = "#{::Rails.root}/spec/fixtures"' do |match| | |
"# #{match}" | |
end | |
gsub_file "spec/rails_helper.rb", 'config.use_transactional_fixtures = true' do |match| | |
"# #{match}" | |
end | |
run "mkdir spec/factories" | |
rails_command('generate graphiti:install') | |
run 'bundle binstubs bundler --force' | |
rake('vandal:install') | |
say(set_color("\nProject Created | |
", :green, :bold)) | |
say(set_color("\nCreate Models | |
", :green, :bold)) | |
run('bundle exec rails generate model user email:string') | |
run('bundle exec rails generate model post user:references') | |
say(set_color("\nMigrate | |
", :green, :bold)) | |
rails_command("db:migrate", env: 'development') | |
rails_command("db:migrate", env: 'test') | |
say(set_color("\nCreate Resources | |
", :green, :bold)) | |
run('bundle exec rails generate graphiti:resource user email:string') | |
run('bundle exec rails generate graphiti:resource post user_id:integer') | |
say(set_color("\nUpdate Post Factory to generate association by defualt | |
", :green, :bold)) | |
gsub_file "spec/factories/posts.rb", 'user { nil }', 'user' | |
run("cat spec/factories/posts.rb") | |
say(set_color("\nRun specs | |
", :green, :bold)) | |
run('rspec') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment