Class: Hyperloop::InstallGenerator

Inherits:
Rails::Generators::Base show all
Defined in:
lib/generators/hyperloop/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_frameworkObject



111
112
113
114
115
# File 'lib/generators/hyperloop/install_generator.rb', line 111

def add_framework
  framework = options['add-framework']
  return unless framework
  generate "hyperloop:install_#{framework}", "--no-build"
end

#add_gemsObject



161
162
163
164
165
166
167
# File 'lib/generators/hyperloop/install_generator.rb', line 161

def add_gems
  # gem 'hyper-model', Hyperloop::VERSION
  # gem 'hyper-router', Hyperloop::ROUTERVERSION
  # gem 'opal-rails', '~> 0.9.4'
  # gem 'opal-jquery'
  # gem "opal-jquery", git: "https://github.com/opal/opal-jquery.git", branch: "master"
end

#add_opal_hot_reloaderObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/generators/hyperloop/install_generator.rb', line 144

def add_opal_hot_reloader
  return if options['skip-hot-reloader']
  create_file 'Procfile', <<-TEXT
web:        bundle exec rails s -b 0.0.0.0
hot-loader: bundle exec opal-hot-reloader -d app/hyperloop
  TEXT
  append_file 'app/assets/javascripts/application.js' do
    <<-RUBY
Opal.OpalHotReloader.$listen() // optional (port, false, poll_seconds) i.e. (8081, false, 1)
    RUBY
  end
  gem_group :development do
    gem 'opal_hot_reloader'
    gem 'foreman'
  end
end

#add_routerObject



68
69
70
# File 'lib/generators/hyperloop/install_generator.rb', line 68

def add_router
  generate "hyper:router", "App"
end

#add_webpacker_manifestsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/hyperloop/install_generator.rb', line 72

def add_webpacker_manifests
  return if skip_webpack?
  create_file 'app/javascript/packs/client_and_server.js', <<-JAVASCRIPT
//app/javascript/packs/client_and_server.js
// these packages will be loaded both during prerendering and on the client
React = require('react');                      // react-js library
History = require('history');                  // react-router history library
ReactRouter = require('react-router');         // react-router js library
ReactRouterDOM = require('react-router-dom');  // react-router DOM interface
ReactRailsUJS = require('react_ujs');          // interface to react-rails
// to add additional NPM packages call run yarn package-name@version
// then add the require here.
  JAVASCRIPT
  create_file 'app/javascript/packs/client_only.js', <<-JAVASCRIPT
//app/javascript/packs/client_only.js
// add any requires for packages that will run client side only
ReactDOM = require('react-dom');               // react-js client side code
jQuery = require('jquery');
// to add additional NPM packages call run yarn package-name@version
// then add the require here.
  JAVASCRIPT
  append_file 'config/initializers/assets.rb' do
    <<-RUBY
Rails.application.config.assets.paths << Rails.root.join('public', 'packs').to_s
    RUBY
  end
end

#add_webpacksObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/hyperloop/install_generator.rb', line 100

def add_webpacks
  return if skip_webpack?
  yarn 'react', '16'
  yarn 'react-dom', '16'
  yarn 'react-router', '4.2'
  yarn 'react-router-dom', '4.2'
  yarn 'history', '4.2'
  yarn 'react_ujs'
  yarn 'jquery'
end

#build_webpackObject



117
118
119
# File 'lib/generators/hyperloop/install_generator.rb', line 117

def build_webpack
  system('bin/webpack')
end

#create_hyperloop_directoriesObject



27
28
29
30
31
32
# File 'lib/generators/hyperloop/install_generator.rb', line 27

def create_hyperloop_directories
  create_file 'app/hyperloop/components/.keep', ''
  create_file 'app/hyperloop/operations/.keep', ''
  create_file 'app/hyperloop/stores/.keep', ''
  create_file 'app/hyperloop/models/.keep', ''
end

#create_initializerObject

all generators should be run before the initializer due to the opal-rails opal-jquery conflict



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/generators/hyperloop/install_generator.rb', line 124

def create_initializer
  create_file 'config/initializers/hyperloop.rb', <<-RUBY
# config/initializers/hyperloop.rb
# If you are not using ActionCable, see http://ruby-hyperloop.io/docs/models/configuring-transport/
Hyperloop.configuration do |config|
  config.transport = :action_cable # or :pusher or :simpler_poller or :none
  config.prerendering = :off # or :on
  config.import 'reactrb/auto-import' # will automatically bridge js components to hyperloop components
#{"  config.import 'jquery', client_only: true  # remove this line if you don't need jquery" if skip_webpack?}
  config.import 'hyper-component/jquery', client_only: true # remove this line if you don't need jquery
#{"  config.import 'opal_hot_reloader' if Rails.env.development?" unless options['skip-hot-reloader']}
end
    RUBY
end

#create_policies_directoryObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/hyperloop/install_generator.rb', line 46

def create_policies_directory
  create_file 'app/policies/application_policy.rb', <<-RUBY
# app/policies/application_policy

# Policies regulate access to your public models
# The following policy will open up full access (but only in development)
# The policy system is very flexible and powerful.  See the documentation
# for complete details.
class Hyperloop::ApplicationPolicy
  # Allow any session to connect:
  always_allow_connection
  # Send all attributes from all public models
  regulate_all_broadcasts { |policy| policy.send_all }
  # Allow all changes to public models
  allow_change(to: :all, on: [:create, :update, :destroy]) { true }
  # allow remote access to all scopes - i.e. you can count or get a list of ids
  # for any scope or relationship
  ApplicationRecord.regulate_scope :all
end unless Rails.env.production?
  RUBY
end

#inject_engine_to_routesObject



139
140
141
142
# File 'lib/generators/hyperloop/install_generator.rb', line 139

def inject_engine_to_routes
  # this needs to be the first route, thus it must be the last method executed
  route 'mount Hyperloop::Engine => \'/hyperloop\''  # this route should be first in the routes file so it always matches
end

#inject_react_file_jsObject



19
20
21
22
23
24
25
# File 'lib/generators/hyperloop/install_generator.rb', line 19

def inject_react_file_js
  append_file 'app/assets/javascripts/application.js' do
    <<-'JS'
//= require hyperloop-loader
    JS
  end
end

#installObject



169
170
171
172
173
# File 'lib/generators/hyperloop/install_generator.rb', line 169

def install
  Bundler.with_clean_env do
    run "bundle install"
  end
end

#insure_yarn_loadedObject



9
10
11
12
13
14
15
16
17
# File 'lib/generators/hyperloop/install_generator.rb', line 9

def insure_yarn_loaded
  return if skip_webpack?
  begin
    yarn_version = `yarn --version`
    raise Errno::ENOENT if yarn_version.blank?
  rescue Errno::ENOENT
    raise Thor::Error.new("please insure the yarn command is available if using webpacker")
  end
end

#move_and_update_application_recordObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/hyperloop/install_generator.rb', line 34

def move_and_update_application_record
  unless File.exists? 'app/hyperloop/models/application_record.rb'
    `mv app/models/application_record.rb app/hyperloop/models/application_record.rb`
    create_file 'app/models/application_record.rb', <<-RUBY
# app/models/application_record.rb
# the presence of this file prevents rails migrations from recreating application_record.rb see https://github.com/rails/rails/issues/29407

require 'models/application_record.rb'
    RUBY
  end
end