Class: InitReact

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/init_react.rb

Instance Method Summary collapse

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_gitignoreObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/init_react.rb', line 33

def add_gitignore
  gitignore = <<-HEREDOC
node_modules
app/assets/javascripts/react-app.js
HEREDOC

  File.open("#{@app_name}/.gitignore", "a+") do |f|
    f.write(gitignore)
  end
end

#add_react_wrapperObject



62
63
64
65
66
67
# File 'lib/generators/init_react.rb', line 62

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_routesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/generators/init_react.rb', line 101

def add_to_routes
  route = <<-HEREDOC
root "react_app#home"
get "*path", to: "react_app#home"
HEREDOC

  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_jsxObject



44
45
46
47
48
49
50
51
# File 'lib/generators/init_react.rb', line 44

def create_app_jsx
  file_path = File.join(File.dirname(__FILE__), "../templates/jsx/app.jsx")
  app_jsx = File.read(file_path)

  File.open("#{@app_name}/app/jsx/app.jsx", "w") do |f|
    f.write(app_jsx)
  end
end

#create_babelrcObject



24
25
26
27
28
29
30
31
# File 'lib/generators/init_react.rb', line 24

def create_babelrc
  file_path = File.join(File.dirname(__FILE__), "../templates/.babelrc")
  babelrc = File.read(file_path)

  File.open("#{@app_name}/.babelrc", "w") do |f|
    f.write(babelrc)
  end
end

#create_bootstrap_jsxObject



53
54
55
56
57
58
59
60
# File 'lib/generators/init_react.rb', line 53

def create_bootstrap_jsx
  file_path = File.join(File.dirname(__FILE__), "../templates/jsx/bootstrap.jsx")
  bootstrap_jsx = File.read(file_path)

  File.open("#{@app_name}/app/jsx/bootstrap.jsx", "w") do |f|
    f.write(bootstrap_jsx)
  end
end

#create_package_jsonObject



69
70
71
72
73
74
75
76
# File 'lib/generators/init_react.rb', line 69

def create_package_json
  file_path = File.join(File.dirname(__FILE__), "../templates/package.json")
  package_json = File.read(file_path)

  File.open("#{@app_name}/package.json", "w") do |f|
    f.write(package_json)
  end
end

#create_react_controllerObject



87
88
89
90
91
92
93
94
# File 'lib/generators/init_react.rb', line 87

def create_react_controller
  file_path = File.join(File.dirname(__FILE__), "../templates/controllers/react_app_controller.rb")
  controller = File.read(file_path)

  File.open("#{@app_name}/app/controllers/react_app_controller.rb", "w") do |f|
    f.write(controller)
  end
end

#create_react_viewObject



96
97
98
99
# File 'lib/generators/init_react.rb', line 96

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_configObject



78
79
80
81
82
83
84
85
# File 'lib/generators/init_react.rb', line 78

def create_webpack_config
  file_path = File.join(File.dirname(__FILE__), "../templates/webpack.config.js")
  webpack_config = File.read(file_path)

  File.open("#{@app_name}/webpack.config.js", "w") do |f|
    f.write(webpack_config)
  end
end

#initObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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
  init_api
end

#init_apiObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/generators/init_react.rb', line 119

def init_api
  FileUtils::mkdir_p "#{@app_name}/app/jsx/CallAPI"

  base_api_path = File.join(File.dirname(__FILE__), "../templates/jsx/CallAPI/BaseAPI.js")
  base_api = File.read(base_api_path)
  File.open("#{@app_name}/app/jsx/CallAPI/BaseAPI.js", "w") do |f|
    f.write(base_api)
  end

  index_api_path = File.join(File.dirname(__FILE__), "../templates/jsx/CallAPI/index.jsx")
  index_api = File.read(index_api_path)
  File.open("#{@app_name}/app/jsx/CallAPI/index.jsx", "w") do |f|
    f.write(index_api)
  end
end