11# Hyperstack
2- WITH_PRERENDERING = true
32
43# ----------------------------------- Commit so we have good history of these changes
54
1716
1817# ----------------------------------- Ensure Sqlite has a valid version
1918
20- gsub_file 'Gemfile' , /gem\s +'sqlite3'(?!,\s +'.*')\n / , "gem 'sqlite3', '~> 1.3.6'\n "
19+ # gsub_file 'Gemfile', /gem\s+'sqlite3'(?!,\s+'.*')\n/, "gem 'sqlite3', '~> 1.3.6'\n"
20+ # gsub_file 'Gemfile', /gem\s+'sqlite3'(?!,\s+'.*')\n/, "gem 'pg'"
2121
2222# ----------------------------------- Create the folders
2323
@@ -60,7 +60,8 @@ class ApplicationRecord < ActiveRecord::Base
6060# the presence of this file prevents rails migrations from recreating application_record.rb
6161# see https://github.com/rails/rails/issues/29407
6262
63- require 'models/application_record.rb'
63+ # BH - I am not sure this is right
64+ # require 'models/application_record.rb'
6465CODE
6566
6667# ----------------------------------- Create the Hyperstack config
@@ -90,6 +91,26 @@ def self.on_error(operation, err, params, formatted_error_message)
9091end if Rails.env.development?
9192CODE
9293
94+ # ----------------------------------- Create thyperstack.js
95+
96+ file 'app/javascript/packs/hyperstack.js' , <<-CODE
97+ // Import all the modules
98+ import React from 'react';
99+ import ReactDOM from 'react-dom';
100+ // for opal/hyperloop modules to find React and others they must explicitly be saved
101+ // to the global space, otherwise webpack will encapsulate them locally here
102+ global.React = React;
103+ global.ReactDOM = ReactDOM;
104+ CODE
105+
106+ # ----------------------------------- View template
107+
108+ inject_into_file 'app/views/layouts/application.html.erb' , before : %r{<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>} do
109+ <<-CODE
110+ <%= javascript_pack_tag 'hyperstack' %>
111+ CODE
112+ end
113+
93114# ----------------------------------- Add a default policy
94115
95116file 'app/policies/application_policy.rb' , <<-CODE
@@ -109,102 +130,32 @@ class Hyperstack::ApplicationPolicy
109130 ApplicationRecord.regulate_scope :all
110131end unless Rails.env.production?
111132# don't forget to provide a policy before production...
112- raise " You need to define a Hyperstack policy for production" if Rails.env.production?
133+ ::Rails.logger.debug("WARNING: You need to define a Hyperstack policy for production") if Rails.env.production?
113134CODE
114135
115136# ----------------------------------- Add NPM modules
116137
117138run 'yarn add react@16'
118139run 'yarn add react-dom@16'
119140run 'yarn add react-router@^5.0.0'
120- if WITH_PRERENDERING
121141run 'yarn add react-router-dom@^5.0.0'
122142# run 'yarn add history' # this will be brought in by react-router
123143run 'yarn add react_ujs@^2.5.0'
124144run 'yarn add jquery@^3.4.1'
125- end
126-
127- if !WITH_PRERENDERING
128- # ----------------------------------- Create hyperstack.js
129-
130- file 'app/javascript/packs/hyperstack.js' , <<-CODE
131- // Import all the modules
132- import React from 'react';
133- import ReactDOM from 'react-dom';
134-
135- // for opal/hyperstack modules to find React and others they must explicitly be saved
136- // to the global space, otherwise webpack will encapsulate them locally here
137- global.React = React;
138- global.ReactDOM = ReactDOM;
139- CODE
140-
141- # ----------------------------------- View template
142-
143- inject_into_file 'app/views/layouts/application.html.erb' , before : %r{<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>} do
144- <<-CODE
145- <%= javascript_pack_tag 'hyperstack' %>
146- CODE
147- end
148145
149146 # ----------------------------------- application.js
150147
151- inject_into_file 'app/assets/javascripts/application.js' , before : %r{//= require_tree .} do
148+ inject_into_file 'app/assets/javascripts/application.js' , before : %r{//= require_tree .} do
152149<<-CODE
153150//= require jquery
154151//= require jquery_ujs
155152//= require hyperstack-loader
156153CODE
157- end
158- else
159- # ----------------------------------- Create client_and_server.js
160-
161- file 'app/javascript/packs/client_and_server.js' , <<-CODE
162- //app/javascript/packs/client_and_server.js
163- // these packages will be loaded both during prerendering and on the client
164- React = require('react'); // react-js library
165- History = require('history'); // react-router history library
166- ReactRouter = require('react-router'); // react-router js library
167- ReactRouterDOM = require('react-router-dom'); // react-router DOM interface
168- ReactRailsUJS = require('react_ujs'); // interface to react-rails
169- // to add additional NPM packages call run add yarn package-name@version
170- // then add the require here.
171- CODE
172-
173- # ----------------------------------- Create client_only.js
174-
175- file 'app/javascript/packs/client_only.js' , <<-CODE
176- //app/javascript/packs/client_only.js
177- // add any requires for packages that will run client side only
178- ReactDOM = require('react-dom'); // react-js client side code
179- jQuery = require('jquery');
180- // to add additional NPM packages call run add yarn package-name@version
181- // then add the require here.
182- CODE
183-
184- # ----------------------------------- add asset paths
185-
186- # note before this was just public/packs now its public/paths/js. WHY???
187- append_file 'config/initializers/assets.rb' do
188- <<-RUBY
189- Rails.application.config.assets.paths << Rails.root.join('public', 'packs', 'js').to_s
190- RUBY
191- end
192- inject_into_file 'config/environments/test.rb' , before : /^end/ do
193- <<-RUBY
194-
195- # added by hyperstack installer
196- config.assets.paths << Rails.root.join('public', 'packs-test', 'js').to_s
197- RUBY
154+ end
198155
156+ # ----------------------------------- Uglifier for ES6
199157
200- # ----------------------------------- application.js
201-
202- inject_into_file 'app/assets/javascripts/application.js' , before : %r{//= require_tree .} do
203- <<-CODE
204- //= require hyperstack-loader
205- CODE
206- end
207- end
158+ gsub_file 'config/environments/production.rb' , /(?:\: uglifier)\n / , "Uglifier.new(harmony: true)\n "
208159
209160# ----------------------------------- Procfile
210161
@@ -236,8 +187,11 @@ class App < HyperComponent
236187 # Route('/user/:id/name', mounts: UserName) : path segments beginning with a colon will be captured in the match param
237188 # see the hyper-router gem documentation for more details
238189
239- render do
190+ render(DIV) do
240191 H1 { "Hello world from Hyperstack!" }
192+ BUTTON {"Click me"}.on(:click) do
193+ alert("All working!")
194+ end
241195 end
242196end
243197CODE
@@ -247,10 +201,12 @@ class App < HyperComponent
247201# must be inserted AFTER route get ... so it ends up before in the route file!
248202route "mount Hyperstack::Engine => '/hyperstack'"
249203
250- # ----------------------------------- Commit Hyperstack setup
204+
205+ # ----------------------------------- Commit Hyperstack setup and create db
251206
252207after_bundle do
253208 run 'bundle exec rails webpacker:install'
254209 git add : "."
255210 git commit : "-m 'Hyperstack config complete'"
211+ run 'rake db:create'
256212end
0 commit comments