Class: Repack::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/repack/install_generator.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_to_gitignoreObject



134
135
136
137
138
139
140
141
142
# File 'lib/generators/repack/install_generator.rb', line 134

def add_to_gitignore
  append_to_file ".gitignore" do
    <<-EOF.strip_heredoc
    /node_modules
    /public/webpack
    npm-debug.log
    EOF
  end
end

#check_god_modeObject



11
12
13
14
15
16
17
# File 'lib/generators/repack/install_generator.rb', line 11

def check_god_mode
  if options[:god]
    unless yes?('Is this is new application?')
      raise 'GOD MODE is for new apps only. Run again at your own risk!'
    end
  end
end

#copy_package_jsonObject



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
# File 'lib/generators/repack/install_generator.rb', line 19

def copy_package_json
  copy_file "package.json", "package.json"
  if options[:router] || options[:god]
    insert_into_file './package.json', after: /dependencies\": {\n/ do
      <<-'RUBY'
"react-router": "^2.4.1",
      RUBY
    end
  end
  if options[:redux] || options[:god]
    insert_into_file './package.json', after: /dependencies\": {\n/ do
      <<-'RUBY'
"react-redux": "^4.4.5",
"redux": "^3.5.2",
"redux-thunk": "^2.1.0",
      RUBY
    end
  end
  if options[:router] && options[:redux] || options[:god]
      insert_into_file './package.json', after: /dependencies\": {\n/ do
      <<-'RUBY'
"react-router-redux": "^4.0.5",
"redux-auth-wrapper": "^1.0.0",
      RUBY
     end
  end
end

#copy_webpack_confObject



47
48
49
50
51
52
53
# File 'lib/generators/repack/install_generator.rb', line 47

def copy_webpack_conf
  copy_file "webpack.config.js", "config/webpack.config.js"
  if yes?('Are you going to be deploying to heroku? (yes \ no)')
    puts 'copying heroku webpack config!'
    copy_file "webpack.config.heroku.js", "config/webpack.config.heroku.js"
  end
end

#create_webpack_application_jsObject



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
# File 'lib/generators/repack/install_generator.rb', line 55

def create_webpack_application_js
  empty_directory "client"
  empty_directory "client/containers"
  empty_directory "client/components"
  empty_directory "client/__tests__"
  empty_directory "client/__tests__/__mocks__"
  copy_file "babelrc", "./.babelrc"
  copy_file "styleMock.js", "client/__tests__/__mocks__/styleMock.js"
  copy_file "fileMock.js", "client/__tests__/__mocks__/fileMock.js"
  if options[:router] && options[:redux]
    copy_file "boilerplate/router_redux/application.js", "client/application.js"
    copy_file "boilerplate/routes.js", "client/routes.js"
    copy_file "boilerplate/router_redux/store.js", "client/store.js"
    copy_file "boilerplate/router_redux/reducers.js", "client/reducers/index.js"
    create_file "client/actions.js"
    copy_file "boilerplate/router/App.js", "client/containers/App.js"
    copy_file "boilerplate/router/NoMatch.js", "client/components/NoMatch.js"
  elsif options[:router]
    copy_file "boilerplate/router/application.js", "client/application.js"
    copy_file "boilerplate/routes.js", "client/routes.js"
    copy_file "boilerplate/router/App.js", "client/containers/App.js"
    copy_file "boilerplate/router/NoMatch.js", "client/components/NoMatch.js"
  elsif options[:redux]
    copy_file "boilerplate/redux/application.js", "client/application.js"
    copy_file "boilerplate/redux/store.js", "client/store.js"
    copy_file "boilerplate/redux/reducers.js", "client/reducers/index.js"
    create_file "client/actions.js"
    copy_file "boilerplate/App.js", "client/containers/App.js"
  else
    unless options[:god]
      copy_file "boilerplate/application.js", "client/application.js"
      copy_file "boilerplate/App.js", "client/containers/App.js"
    end
  end

  haml_installed = Gem.loaded_specs.has_key? 'haml-rails'
  layouts_dir = 'app/views/layouts'
       haml_installed = Gem.loaded_specs.has_key? 'haml-rails'
  layouts_dir = 'app/views/layouts'

  application_view = haml_installed ? "#{layouts_dir}/application.html.haml" : "#{layouts_dir}/application.html.erb"

  if haml_installed
    if yes?('Convert all existing ERB views into HAML? (yes / no)')
      begin
        require 'html2haml'
      rescue LoadError
        `gem install html2haml`
      end
      `find . -name \*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 html2haml`
      `rm #{layouts_dir}/application.html.erb`
    end

    insert_into_file application_view, before: /%body/ do
      <<-'RUBY'
- if Rails.env.development?
  %script{:src => "http://localhost:3808/webpack-dev-server.js"}
      RUBY
    end

    insert_into_file application_view, after: /= yield/ do
        <<-'RUBY'

= javascript_include_tag *webpack_asset_paths('application')
        RUBY
    end
  else
    insert_into_file application_view, before: /<\/head>/ do <<-'RUBY'
  <% if Rails.env.development? %>
<script src="http://localhost:3808/webpack-dev-server.js"></script>
  <% end %>
    RUBY
    end
    insert_into_file application_view, before: /<\/body>/ do <<-'RUBY'
<%= javascript_include_tag *webpack_asset_paths('application') %>
    RUBY
    end
  end
end

#finishing_god_moveObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/generators/repack/install_generator.rb', line 159

def finishing_god_move
  if options[:god]
    copy_file "boilerplate/router_redux/application.js", "client/application.js"
    copy_file "boilerplate/god_mode/routes.js", "client/routes.js"
    copy_file "boilerplate/router_redux/store.js", "client/store.js"
    copy_file "boilerplate/router/NoMatch.js", "client/components/NoMatch.js"
    copy_file "boilerplate/god_mode/actions/auth.js", "client/actions/auth.js"
    copy_file "boilerplate/god_mode/actions/flash.js", "client/actions/flash.js"
    copy_file "boilerplate/god_mode/components/Navbar.js", "client/components/Navbar.js"
    copy_file "boilerplate/god_mode/components/FlashMessage.js", "client/components/FlashMessage.js"
    copy_file "boilerplate/god_mode/components/Login.js", "client/components/Login.js"
    copy_file "boilerplate/god_mode/components/SignUp.js", "client/components/SignUp.js"
    copy_file "boilerplate/god_mode/components/Loading.js", "client/components/Loading.js"
    copy_file "boilerplate/god_mode/containers/App.js", "client/containers/App.js"
    copy_file "boilerplate/god_mode/reducers/auth.js", "client/reducers/auth.js"
    copy_file "boilerplate/god_mode/reducers/flash.js", "client/reducers/flash.js"
    copy_file "boilerplate/god_mode/reducers/index.js", "client/reducers/index.js"
    copy_file "boilerplate/god_mode/controllers/api/users_controller.rb", "app/controllers/api/users_controller.rb"
    copy_file "boilerplate/god_mode/scss/alert.css.scss", "app/assets/stylesheets/alert.css.scss"
    gem "devise"
    Bundler.with_clean_env do
      run "bundle install"
    end
    run 'bin/spring stop'
    generate "devise:install"
    run "bundle exec rake db:create"
    model_name = ask("What would you like the user model to be called? [user]")
    model_name = "user" if model_name.blank?
    generate "devise", model_name
    generate "devise:controllers #{model_name.pluralize}"

    insert_into_file 'config/routes.rb', after: /devise_for :users/ do <<-'RUBY'
, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations'
  }
  namespace :api do
get 'logged_in_user', to: 'users#logged_in_user'
  end
    RUBY
    end
    ['./app/controllers/users/sessions_controller.rb', './app/controllers/users/registrations_controller.rb'].each do |c|
      insert_into_file c, after: /Devise::\W*.*\n/ do <<-'RUBY'
skip_before_action :verify_authenticity_token
respond_to :json
        RUBY
      end
    end
  end
  run 'bundle exec rake db:migrate'
end

#install_yarnObject



144
145
146
147
148
149
# File 'lib/generators/repack/install_generator.rb', line 144

def install_yarn
  if yes?('Do you want to global install and use yarn as your package manager? (yes / no)')
    @yarn_installed = true
    run "npm install yarn -g"
  end
end

#run_package_manager_installObject



151
152
153
154
155
156
157
# File 'lib/generators/repack/install_generator.rb', line 151

def run_package_manager_install
  if @yarn_installed
    run "yarn install" if yes?("Would you like us to run 'yarn install' for you?")
  else
    run "npm install" if yes?("Would you like us to run 'npm install' for you?")
  end
end

#whats_nextObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/generators/repack/install_generator.rb', line 211

def whats_next
  puts <<-EOF.strip_heredoc
    We've set up the basics of repack for you, but you'll still
    need to:
      1. Add an element with an id of 'app' to your layout
      2. To disable hot module replacement remove <script src="http://localhost:3808/webpack-dev-server.js"></script> from layout
      3. Run 'yarn run dev_server' to run the webpack-dev-server
      4. Run 'bundle exec rails s' to run the rails server (both servers must be running)
      5. If you are using react-router and want to sync server routes add:
         get '*unmatched_route', to: <your client controller>#<default action>
         This must be the very last route in your routes.rb file
         e.g. get '*unmatched_route', to: 'home#index'
      FOR HEROKU DEPLOYS:
      1.  yarn run heroku-setup
      2.  Push to heroku the post-build hook will take care of the rest
    See the README.md for this gem at
    https://github.com/cottonwoodcoding/repack/blob/master/README.md
    for more info.
    Thanks for using repack!
  EOF
end