Skip to content

Commit f656d13

Browse files
committed
merged updates to rails installers and docs
2 parents 1351858 + 3311b9a commit f656d13

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

docs/installation/installation.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Add the rails-hyperstack gem to your Gemfile, and then bundle install. This can
3232
bundle add 'rails-hyperstack', --version "~> 1.0.alpha1.0"
3333
```
3434

35-
> If you want to use the unreleased edge branch your gem specification will be:
35+
> If you want to use the unreleased edge branch your gem specification will be:
3636
```ruby
3737
gem 'rails-hyperstack',
3838
git: 'git://github.com/hyperstack-org/hyperstack.git',
@@ -57,14 +57,24 @@ Each of these pieces can be skipped or installed independently either using
5757
built in installers and generators, or manually as explained in the
5858
following sections.
5959

60+
At this point, you're fully installed.
61+
62+
If you're installing into a new rails app, you can run bundle exec foreman start
63+
to start the server, and "app" will display on the top left hand side of the page.
64+
65+
If you're installing into an existing Rails apps, you can run bundle exec foreman start
66+
to start the server, and your app should funcion exactly as it always has. From here, you'll need to
67+
[add a Hypercomponent](#<user-content-adding-a-single-component-to-your-application>)
68+
to have Hyperstack funcionality.
69+
6070
## Summary of Installers and Generators
6171

6272
```
6373
bundle exec ... # for best results always use bundle exec!
6474
rails hyperstack:install # full system install
65-
rails hyperstack:install:webpack-only # just add webpack
75+
rails hyperstack:install:webpack # just add webpack
6676
rails hyperstack:install:skip-webpack # all but webpack
67-
rails hyperstack:install:hyper-model-only # just add hyper-model
77+
rails hyperstack:install:hyper-model # just add hyper-model
6878
rails hyperstack:install:skip-hyper-model # all but hyper-model
6979
rails hyperstack:install:hotloader-only # just add the hotloader
7080
rails hyperstack:install:skip-hotloader # skip the hotloader
@@ -98,8 +108,8 @@ rails g hyper:router CompName # adds a router component
98108
# Note that hyperstack:install is the same as running:
99109
100110
rails g hyper:component App --add-route
101-
rails hyperstack:install:webpack-only
102-
rails hyperstack:install:hyper-model-only
111+
rails hyperstack:install:webpack
112+
rails hyperstack:install:hyper-model
103113
rails hyperstack:install:hotloader-only
104114
```
105115

@@ -113,9 +123,9 @@ The following subdirectories are standard:
113123
app/
114124
...
115125
hyperstack/
116-
components/
117-
models/
118-
operations/
126+
components/
127+
models/
128+
operations/
119129
stores/
120130
shared/
121131
lib/
@@ -158,8 +168,8 @@ class HyperComponent
158168
param_accessor_style :accessors
159169
end
160170
```
161-
> Note: You can override the base class name using the `--base-class` switch:
162-
> For example if you prefer ApplicationComponent rather than HyperComponent
171+
> Note: You can override the base class name using the `--base-class` switch:
172+
> For example if you prefer ApplicationComponent rather than HyperComponent
163173
```bash
164174
bundle exec rails g hyper:component Test --base-class=ApplicationComponent
165175
```
@@ -200,7 +210,7 @@ and override the default layout
200210

201211
For example
202212
```erb
203-
<% render_component 'Dashboard', user: @user %>
213+
<%= react_component 'Dashboard', user: @user %>
204214
```
205215
> Note that you may have several component trees mounted in a single page using the `render_component` helper. While this is not typical for a clean sheet Hyperstack design, it is useful when mixing Hyperstack with legacy applications.
206216
@@ -253,7 +263,7 @@ various routing methods.
253263

254264
> Note that in a large app you may have several single page apps working together,
255265
along with some legacy routes.
256-
In such an application the router structure might look like this:
266+
In such an application the router structure might look like this:
257267
```ruby
258268
Rails.application.routes.draw do
259269
... # legacy routes
@@ -332,7 +342,7 @@ Webpacker uses manifests to determine how to package up assets. Hyperstack depe
332342
>
333343
> This means that page load time is comparable to any other Rails view, and that Rails can cache the pages like any other view.
334344
>
335-
> But to make this work packages that rely on the `browser` object cannot be used during prerendering. Well structured packages that depend on the `browser` object will have a way to run in the prerendering environment.
345+
> But to make this work packages that rely on the `browser` object cannot be used during prerendering. Well structured packages that depend on the `browser` object will have a way to run in the prerendering environment.
336346
337347
These two files look like this and are placed in the `app/javascript/packs` directory:
338348
```javascript
@@ -387,13 +397,13 @@ include them in the sprockets asset pipeline.
387397
To add the packages using yarn run these commands:
388398

389399
```bash
390-
yarn 'react', '16'
391-
yarn 'react-dom', '16'
392-
yarn 'react-router', '^5.0.0'
393-
yarn 'react-router-dom', '^5.0.0'
394-
yarn 'react_ujs', '^2.5.0'
400+
yarn add react@16
401+
yarn add react-dom@16
402+
yarn add react-router@^5.0.0
403+
yarn add react-router-dom@^5.0.0
404+
yarn add react_ujs@^2.5.0
405+
yarn add jquery@^3.4.1
395406
```
396-
397407
And then add a `cancel_import` directive to the `hyperstack.rb` initializer in your the Rails `config/initializers` directory:
398408

399409
```ruby
@@ -464,7 +474,7 @@ policies for legacy parts of your system.
464474

465475
#### Moving the `application_record.rb` File.
466476

467-
Once you have a basic Policy defined the client can access your Rails models. For your ActiveRecord model class definitions to be visible on the client you need to move them to the `app/hyperstack/models` directory. This directory (along with `app/hyperstack/operations` and `app/hyperstack/shared`) are *isomorphic* directories. The code in these directories will be accessible on both the client and the server.
477+
Once you have a basic Policy defined the client can access your Rails models. For your ActiveRecord model class definitions to be visible on the client you need to move them to the `app/hyperstack/models` directory. This directory (along with `app/hyperstack/operations` and `app/hyperstack/shared`) are *isomorphic* directories. The code in these directories will be accessible on both the client and the server.
468478
> Moving the files is necessary both because of the way Rails is structured, but its also very useful when evolving legacy systems to Hyperstack. Until a class
469479
definition is moved to the `hyperstack/models` directory it will be ignored by Hyperstack.
470480

@@ -537,14 +547,14 @@ bundle exec hyperstack-hotloader -p 25222 -d app/hyperstack
537547
This will tell the Hotloader to use port 25222, and to scan the
538548
`app/hyperstack` directory.
539549

540-
> If for some reason you cannot use port 25222 you can change it, but you need to also configure this in the Hyperstack initializer file:
550+
> If for some reason you cannot use port 25222 you can change it, but you need to also configure this in the Hyperstack initializer file:
541551
```ruby
542552
...
543553
config.hotloader_port = 12345 # override default of 25222
544554
...
545555
```
546556

547-
Now that the Hotloader is running, you can start your Rails server the normal way, and refresh your browser page.
557+
Now that the Hotloader is running, you can start your Rails server the normal way, and refresh your browser page.
548558
You should now see `require 'hyperstack/hotloader' # CLIENT ONLY` added to the manifest,
549559
and you will also see a message indicating that your browser has connected to the Hotloader.
550560

@@ -629,7 +639,7 @@ Hyperstack.configuration do |config|
629639
config.import 'path/to/file', client_only: true # but only in the client manifest
630640
config.import 'path/to/file', server_only: true # but only on the server_only manifest
631641

632-
# The cancel_import directive removes an import. This is used when we replace
642+
# The cancel_import directive removes an import. This is used when we replace
633643
# assets from the rails side with those loaded by webpacker
634644
config.cancel_import 'path/to/file'
635645
end

ruby/rails-hyperstack/lib/generators/hyperstack/install_generator.rb

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def add_hotloader
2525
unless Hyperstack.imported? 'hyperstack/hotloader'
2626
inject_into_initializer(
2727
"Hyperstack.import 'hyperstack/hotloader', "\
28-
"client_only: true if Rails.env.development?"
28+
'client_only: true if Rails.env.development?'
2929
)
3030
end
3131
create_file 'Procfile', <<-TEXT
@@ -80,7 +80,6 @@ def add_webpacker_manifests
8080
config.assets.paths << Rails.root.join('public', 'packs-test', 'js').to_s
8181
RUBY
8282
end
83-
8483
end
8584

8685
def add_webpacks
@@ -98,24 +97,24 @@ def cancel_react_source_import
9897
return if skip_webpack?
9998
inject_into_initializer(
10099
"Hyperstack.cancel_import 'react/react-source-browser' "\
101-
"# bring your own React and ReactRouter via Yarn/Webpacker"
100+
'# bring your own React and ReactRouter via Yarn/Webpacker'
102101
)
103102
end
104103

105104
def install_webpacker
106105
return if skip_webpack?
107106
gem 'webpacker'
108107
Bundler.with_clean_env do
109-
run "bundle install"
108+
run 'bundle install'
110109
end
111110
run 'bundle exec rails webpacker:install'
112111
end
113112

114113
def create_policies_directory
115114
return if skip_hyper_model?
116-
policy_file = File.join('app', 'hyperstack', 'models', 'application_record.rb')
117-
unless File.exists? policy_file
118-
create_file 'policy_file', <<-RUBY
115+
policy_file = File.join('app', 'policies', 'application_policy.rb')
116+
unless File.exist? policy_file
117+
create_file policy_file, <<-RUBY
119118
# #{policy_file}
120119
121120
# Policies regulate access to your public models
@@ -141,7 +140,7 @@ def move_and_update_application_record
141140
return if skip_hyper_model?
142141
rails_app_record_file = File.join('app', 'models', 'application_record.rb')
143142
hyper_app_record_file = File.join('app', 'hyperstack', 'models', 'application_record.rb')
144-
unless File.exists? hyper_app_record_file
143+
unless File.exist? hyper_app_record_file
145144
empty_directory File.join('app', 'hyperstack', 'models')
146145
`mv #{rails_app_record_file} #{hyper_app_record_file}`
147146
create_file rails_app_record_file, <<-RUBY
@@ -162,21 +161,24 @@ def add_engine_route
162161
def report
163162
say "\n\n"
164163
unless skip_adding_component?
165-
say "🎢 Top Level App Component successfully installed at app/hyperstack/components/app.rb 🎢", :green
164+
say '🎢 Top Level App Component successfully installed at app/hyperstack/components/app.rb 🎢', :green
165+
end
166+
if !new_rails_app?
167+
say '🎢 Top Level App Component skipped, you can manually generate it later 🎢', :green
166168
end
167169
unless skip_webpack?
168-
say "📦 Webpack integrated with Hyperstack. "\
169-
"Add javascript assets to app/javascript/packs/client_only.js and /client_and_server.js 📦", :green
170+
say '📦 Webpack integrated with Hyperstack. '\
171+
'Add javascript assets to app/javascript/packs/client_only.js and /client_and_server.js 📦', :green
170172
end
171173
unless skip_hyper_model?
172-
say "👩‍✈️ Basic development policy defined. See app/policies/application_policy.rb 👨🏽‍✈️", :green
173-
say "💽 HyperModel installed. Move any Active Record models to the app/hyperstack/models to access them from the client 📀", :green
174+
say '👩‍✈️ Basic development policy defined. See app/policies/application_policy.rb 👨🏽‍✈️', :green
175+
say '💽 HyperModel installed. Move any Active Record models to the app/hyperstack/models to access them from the client 📀', :green
174176
end
175-
if File.exists?(init = File.join('config', 'initializers', 'hyperstack.rb'))
177+
if File.exist?(init = File.join('config', 'initializers', 'hyperstack.rb'))
176178
say "☑️ Check #{init} for other configuration options. ☑️", :green
177179
end
178180
unless skip_hotloader?
179-
say "🚒 Hyperstack Hotloader installed - use bundle exec foreman start and visit localhost:5000 🚒", :green
181+
say '🚒 Hyperstack Hotloader installed - use bundle exec foreman start and visit localhost:5000 🚒', :green
180182
end
181183

182184
say "\n\n"
@@ -185,7 +187,7 @@ def report
185187
private
186188

187189
def skip_adding_component?
188-
options['hotloader-only'] || options['webpack-only'] || options['hyper-model-only']
190+
options['hotloader-only'] || options['webpack-only'] || options['hyper-model-only'] || !new_rails_app?
189191
end
190192

191193
def skip_hotloader?
@@ -200,9 +202,24 @@ def skip_hyper_model?
200202
options['hotloader-only'] || options['webpack-only'] || options['skip-hyper-model']
201203
end
202204

205+
def new_rails_app?
206+
# check to see if there are any routes set up and remember it, cause we might add a route in the process
207+
@new_rails_app ||= begin
208+
route_file = File.join('config', 'routes.rb')
209+
count = File.foreach(route_file).inject(0) do |c, line|
210+
line = line.strip
211+
next c if line.empty?
212+
next c if line.start_with?('#')
213+
next c if line.start_with?('mount')
214+
c + 1
215+
end
216+
count <= 2
217+
end
218+
end
219+
203220
def inject_into_initializer(s)
204221
file_name = File.join('config', 'initializers', 'hyperstack.rb')
205-
if File.exists?(file_name)
222+
if File.exist?(file_name)
206223
prepend_to_file(file_name) { "#{s}\n" }
207224
else
208225
create_file file_name, <<-RUBY

ruby/rails-hyperstack/lib/generators/hyperstack/install_generator_base.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def create_component_file(template)
2626

2727

2828
def clear_cache
29-
run 'rm -rf tmp/cache' unless Dir.exists?(File.join('app', 'hyperstack'))
29+
run 'rm -rf tmp/cache' unless Dir.exist?(File.join('app', 'hyperstack'))
3030
end
3131

3232
def insure_hyperstack_loader_installed
@@ -54,17 +54,16 @@ def insure_hyperstack_loader_installed
5454
end
5555
end
5656

57-
5857
def insure_base_component_class_exists
5958
@component_base_class = options['base-class'] || Hyperstack.component_base_class
6059
file_name = File.join(
6160
'app', 'hyperstack', 'components', "#{@component_base_class.underscore}.rb"
6261
)
63-
template 'hyper_component_template.rb', file_name unless File.exists? file_name
62+
template 'hyper_component_template.rb', file_name unless File.exist? file_name
6463
end
6564

6665
def add_to_manifest(manifest, &block)
67-
if File.exists? "app/javascript/packs/#{manifest}"
66+
if File.exist? "app/javascript/packs/#{manifest}"
6867
append_file "app/javascript/packs/#{manifest}", &block
6968
else
7069
create_file "app/javascript/packs/#{manifest}", &block
@@ -82,11 +81,11 @@ def add_route
8281
end
8382
action_name = (@modules+[@file_name.underscore]).join('__')
8483
path = options['add-route'] == 'add-route' ? '/(*others)' : options['add-route']
85-
routing_code = "get '#{path}', to: 'hyperstack##{action_name}'"
84+
routing_code = "get '#{path}', to: 'hyperstack##{action_name}'\n"
8685
log :route, routing_code
8786
[/mount\s+Hyperstack::Engine[^\n]+\n/m, /\.routes\.draw do\s*\n/m].each do |sentinel|
8887
in_root do
89-
x = inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
88+
inject_into_file 'config/routes.rb', routing_code.indent(2), after: sentinel, verbose: false, force: false
9089
end
9190
end
9291
end

ruby/rails-hyperstack/lib/tasks/hyperstack/install.rake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ namespace :hyperstack do
1313
task "hotloader-only" do
1414
exec "#{RbConfig.ruby} #{bin_path}/rails g hyperstack:install --hotloader-only"
1515
end
16-
task "webpack-only" do
16+
task "webpack" do
1717
exec "#{RbConfig.ruby} #{bin_path}/rails g hyperstack:install --webpack-only"
1818
end
19-
task "hyper-model-only" do
19+
task "hyper-model" do
2020
exec "#{RbConfig.ruby} #{bin_path}/rails g hyperstack:install --hyper-model-only"
2121
end
2222
task "skip-hotloader" do

0 commit comments

Comments
 (0)