-
Notifications
You must be signed in to change notification settings - Fork 6
/
all.rb
155 lines (123 loc) · 3.9 KB
/
all.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
require 'rails/version'
gem 'graphiti'
gem 'graphiti-rails'
gem 'vandal_ui'
gem 'kaminari', '~> 1.1'
if Rails::VERSION::MAJOR == 5
gem 'responders', '~> 2.4'
else
gem 'responders', '~> 3.0'
end
gem_group :development, :test do
if Rails::VERSION::MAJOR == 5
gem 'rspec-rails', '~> 3.5.2'
else
gem 'rspec-rails', '~> 4.0.0beta2'
end
gem 'factory_bot_rails', '~> 5.0'
gem 'faker', '~> 2.5' # keep here for seeds.rb
gem 'graphiti_spec_helpers'
end
gem_group :test do
gem 'database_cleaner', '~> 1.7'
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
handle_request_exceptions(false)
end
STR
end
insert_into_file "spec/rails_helper.rb", :after => "RSpec.configure do |config|\n" do
" config.include Graphiti::Rails::TestHelpers\n"
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("\nYou're all set!
", :green, :bold))end