Class: InitReact
- Inherits:
-
Object
- Object
- InitReact
- Defined in:
- lib/generators/init_react.rb
Instance Method Summary collapse
- #add_gitignore ⇒ Object
- #add_react_wrapper ⇒ Object
- #add_to_routes ⇒ Object
- #create_app_jsx ⇒ Object
- #create_babelrc ⇒ Object
- #create_bootstrap_jsx ⇒ Object
- #create_package_json ⇒ Object
- #create_react_controller ⇒ Object
- #create_react_view ⇒ Object
- #create_webpack_config ⇒ Object
- #init ⇒ Object
-
#initialize(app_name) ⇒ InitReact
constructor
A new instance of InitReact.
Constructor Details
#initialize(app_name) ⇒ InitReact
Returns a new instance of InitReact.
4 5 6 7 |
# File 'lib/generators/init_react.rb', line 4 def initialize app_name @app_name = app_name FileUtils::mkdir_p "#{@app_name}/app/jsx" end |
Instance Method Details
#add_gitignore ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/generators/init_react.rb', line 41 def add_gitignore gitignore = "node_modules\napp/assets/javascripts/react-app.js\n" File.open("#{@app_name}/.gitignore", "a+") do |f| f.write(gitignore) end end |
#add_react_wrapper ⇒ Object
102 103 104 105 106 107 |
# File 'lib/generators/init_react.rb', line 102 def add_react_wrapper file_name = "#{@app_name}/app/views/layouts/application.html.erb" text = File.read(file_name) new_contents = text.gsub(/<%= yield %>/, '<div id="react-wrapper"></div>') File.open(file_name, "w") {|f| f.write(new_contents)} end |
#add_to_routes ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/generators/init_react.rb', line 199 def add_to_routes route = "root \"react_app#home\"\nget \"*path\", to: \"react_app#home\"\n" routes = File.readlines("#{@app_name}/config/routes.rb") i = routes.length while i > 1 do routes[i] = routes[i-1] i -= 1 end routes[1] = route File.open("#{@app_name}/config/routes.rb", "w") do |f| f.write(routes.join) end end |
#create_app_jsx ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/generators/init_react.rb', line 52 def create_app_jsx app_jsx = "import getMuiTheme from \"material-ui/styles/getMuiTheme\";\nimport MuiThemeProvider from \"material-ui/styles/MuiThemeProvider\";\n\nexport default class App extends React.Component {\n render() {\n return (\n <MuiThemeProvider muiTheme={getMuiTheme()}>\n <mui.Drawer open={true}>\n <mui.MenuItem>FAQ</mui.MenuItem>\n </mui.Drawer>\n </MuiThemeProvider>\n );\n }\n}\n" File.open("#{@app_name}/app/jsx/app.jsx", "w") do |f| f.write(app_jsx) end end |
#create_babelrc ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/generators/init_react.rb', line 23 def create_babelrc babelrc = "{\npresets: [\n \"es2017\",\n \"react\",\n],\n\"plugins\": [\n \"transform-class-properties\",\n],\n}\n" File.open("#{@app_name}/.babelrc", "w") do |f| f.write(babelrc) end end |
#create_bootstrap_jsx ⇒ Object
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 |
# File 'lib/generators/init_react.rb', line 75 def create_bootstrap_jsx bootstrap_jsx = "import ReactDOM from \"react-dom\";\nimport {Router, Route, Link, browserHistory} from \"react-router\"\n\nimport injectTapEventPlugin from \"react-tap-event-plugin\";\ninjectTapEventPlugin();\n\nimport App from \"./app\";\n\nconst router = (\n<Router history={browserHistory}>\n <Route path=\"/\" component={App}>\n </Route>\n</Router>\n);\n\n$(document).on(\"ready page:load\", function() {\n ReactDOM.render(router, document.getElementById(\"react-wrapper\"));\n});\n" File.open("#{@app_name}/app/jsx/bootstrap.jsx", "w") do |f| f.write(bootstrap_jsx) end end |
#create_package_json ⇒ Object
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 |
# File 'lib/generators/init_react.rb', line 109 def create_package_json package_json = "{\n\"private\": true,\n\"dependencies\": {\n \"babel-core\": \"^6.18.2\",\n \"babel-loader\": \"^6.2.7\",\n \"babel-preset-es2017\": \"^1.4.0\",\n \"babel-preset-react-hmre\": \"^1.1.1\",\n \"babel-preset-react\": \"^6.16.0\",\n \"babel-plugin-add-module-exports\": \"~0.2.1\",\n \"babel-plugin-transform-class-properties\": \"~6.19.0\",\n \"material-ui\": \"^0.16.0\",\n \"react\": \"^15.4.0\",\n \"react-addons-update\": \"^15.4.0\",\n \"react-dom\": \"^15.4.0\",\n \"react-router\": \"^3.0.0\",\n \"react-tap-event-plugin\": \"^2.0.0\",\n \"react-toastr\": \"^2.8.0\",\n \"webpack\": \"~1.13.3\"\n}\n}\n" File.open("#{@app_name}/package.json", "w") do |f| f.write(package_json) end end |
#create_react_controller ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/generators/init_react.rb', line 181 def create_react_controller controller = "class ReactAppController < ApplicationController\ndef home\nend\nend\n" File.open("#{@app_name}/app/controllers/react_app_controller.rb", "w") do |f| f.write(controller) end end |
#create_react_view ⇒ Object
194 195 196 197 |
# File 'lib/generators/init_react.rb', line 194 def create_react_view FileUtils::mkdir_p "#{@app_name}/app/views/react_app" File.open("#{@app_name}/app/views/react_app/home.html.erb", "w") end |
#create_webpack_config ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/generators/init_react.rb', line 138 def create_webpack_config webpack_config = "var webpack = require(\"webpack\");\n\nconst paths = {\njs: \"./app/assets/javascripts/\",\njsx: \"./app/jsx/\",\n}\n\nvar alias = {\n\"react/lib/CSSPropertyOperations\": \"react-dom/lib/CSSPropertyOperations\"\n};\n\nmodule.exports = {\nentry: paths.jsx + \"bootstrap.jsx\",\noutput: {\n path: paths.js,\n filename: \"react-app.js\",\n},\nresolve: {\n extensions: ['', '.js', '.jsx'],\n alias: alias,\n},\nmodule: {\n loaders: [{\n test: /\\.jsx?$/,\n loaders: ['babel?cacheDirectory'],\n }]\n},\nplugins: [\n new webpack.ProvidePlugin({\n React: \"react\",\n mui: \"material-ui\",\n })\n],\n}\n" File.open("#{@app_name}/webpack.config.js", "w") do |f| f.write(webpack_config) end end |
#init ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/generators/init_react.rb', line 9 def init create_babelrc add_gitignore create_app_jsx create_bootstrap_jsx add_react_wrapper create_package_json create_webpack_config create_react_controller create_react_view add_to_routes end |