Class: WebpackRailsReact::InstallGenerator

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_foreman_to_gemfileObject



10
11
12
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 10

def add_foreman_to_gemfile
  gem 'foreman'
end

#add_to_gitignoreObject



97
98
99
100
101
102
103
104
105
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 97

def add_to_gitignore
  append_to_file ".gitignore" do
    "    # Added by webpack-rails\n    /node_modules\n    /public/webpack\n    EOF\n  end\nend\n".strip_heredoc

#copy_package_jsonObject



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

def copy_package_json
  copy_file "package.json", "package.json"

  if options[:router]
    insert_into_file './package.json', after: /dependencies\": {\n/ do
      "\"react-router\": \"^2.4.1\",\n      RUBY\n    end\n  end\n\n  if options[:redux]\n    insert_into_file './package.json', after: /dependencies\\\": {\\n/ do\n      <<-'RUBY'\n\"react-redux\": \"^4.4.5\",\n\"redux\": \"^3.5.2\",\n\"redux-thunk\": \"^2.1.0\",\n      RUBY\n    end\n  end\n\n  if options[:router] && options[:redux]\n      insert_into_file './package.json', after: /dependencies\\\": {\\n/ do\n      <<-'RUBY'\n\"react-router-redux\": \"^4.0.5\",\n      RUBY\n     end\n  end\nend\n"

#copy_procfileObject



14
15
16
17
18
19
20
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 14

def copy_procfile
  if File.exists? "Procfile"
    append_file "Procfile", "webpack: ./node_modules/.bin/webpack-dev-server --config config/webpack.config.js"
  else
    copy_file "Procfile", "Procfile"
  end
end

#copy_webpack_confObject



52
53
54
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 52

def copy_webpack_conf
  copy_file "webpack.config.js", "config/webpack.config.js"
end

#create_webpack_application_jsObject



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

def create_webpack_application_js
  empty_directory "webpack"
  empty_directory "webpack/containers"
  empty_directory "webpack/components"

  if options[:router] && options[:redux]
    copy_file "boilerplate/router_redux/application.js", "webpack/application.js"
    copy_file "boilerplate/routes.js", "webpack/routes.js"
    copy_file "boilerplate/router_redux/store.js", "webpack/store.js"
    copy_file "boilerplate/router_redux/reducers.js", "webpack/reducers/index.js"
    create_file "webpack/actions.js"
    copy_file "boilerplate/router/App.js", "webpack/containers/App.js"
    copy_file "boilerplate/router/NoMatch.js", "webpack/components/NoMatch.js"
  elsif options[:router]
    copy_file "boilerplate/router/application.js", "webpack/application.js"
    copy_file "boilerplate/routes.js", "webpack/routes.js"
    copy_file "boilerplate/router/App.js", "webpack/containers/App.js"
    copy_file "boilerplate/router/NoMatch.js", "webpack/components/NoMatch.js"
  elsif options[:redux]
    copy_file "boilerplate/redux/application.js", "webpack/application.js"
    copy_file "boilerplate/redux/store.js", "webpack/store.js"
    copy_file "boilerplate/redux/reducers.js", "webpack/reducers/index.js"
    create_file "webpack/actions.js"
    copy_file "boilerplate/App.js", "webpack/containers/App.js"
  else
    copy_file "boilerplate/application.js", "webpack/application.js"
    copy_file "boilerplate/App.js", "webpack/containers/App.js"
  end

  insert_into_file 'app/views/layouts/application.html.erb', before: /<\/body>/ do
      "<%= javascript_include_tag *webpack_asset_paths('application') %>\n      RUBY\n  end\n  insert_into_file 'app/views/layouts/application.html.erb', before: /<\\/head>/ do\n      <<-'RUBY'\n<script src=\"http://localhost:3808/webpack-dev-server.js\"></script>\n      RUBY\n  end\nend\n"

#run_bundle_installObject



111
112
113
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 111

def run_bundle_install
  run "bundle install" if yes?("Would you like us to run 'bundle install' for you?")
end

#run_npm_installObject



107
108
109
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 107

def run_npm_install
  run "npm install" if yes?("Would you like us to run 'npm install' for you?")
end

#whats_nextObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/generators/webpack_rails_react/install_generator.rb', line 115

def whats_next
  puts "\n    We've set up the basics of webpack-rails-react for you, but you'll still\n    need to:\n\n      1. Add an element with an id of 'app' to your layout\n      2. To disable hot module replacement remove <script src=\"http://localhost:3808/webpack-dev-server.js\"></script> from layout\n      3. Run 'foreman start' to run the webpack-dev-server and rails server\n      4. If you are using react-router and want to sync server routes add:\n         get '*unmatched_route', to: <your client controller>#<default action>\n         This must be the very last route in your routes.rb file\n         e.g. get '*unmatched_route', to: 'home#index'\n\n    See the README.md for this gem at\n    https://github.com/cottonwoodcoding/webpack-rails-react/blob/master/README.md\n    for more info.\n\n    Thanks for using webpack-rails-react!\n\n  EOF\nend\n".strip_heredoc