1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actioncable/lib/rails/generators/channel/channel_generator.rb

112 lines
3.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2015-12-15 15:38:58 -05:00
module Rails
module Generators
class ChannelGenerator < NamedBase
source_root File.expand_path("templates", __dir__)
2015-12-15 15:38:58 -05:00
argument :actions, type: :array, default: [], banner: "method method"
class_option :assets, type: :boolean
2015-12-15 15:38:58 -05:00
check_class_collision suffix: "Channel"
2019-01-13 21:54:31 -05:00
hook_for :test_framework
def create_channel_files
create_shared_channel_files
create_channel_file
if using_javascript?
if first_setup_required?
create_shared_channel_javascript_files
import_channels_in_javascript_entrypoint
Replace webpack with importmapped Hotwire as default js (#42999) * Turbolinks is being replaced with Hotwire * Make --webpack opt-in * Don't use specific webpacker installers any more in preparation for next Webpacker * Update railties/lib/rails/app_updater.rb Co-authored-by: Alex Ghiculescu <alex@tanda.co> * Trailing whitespace * Convert to Turbo data attribute for tracking * Default is no webpack, no hotwire * Swap out turbolinks references for hotwire * Drop explicit return * Only generate package.json if using webpack * Only create package.json in webpack mode * Only create app/javascript in webpack mode * Generate correct style/js links based on js mode * Fix tests from changed output format Not sure why these are showing up in this PR, though. * Rubocopping * Stick with webpack for the test app for now * Adjust tests * Replace minitest-reporters with minitest-ci (#43016) minitest-reporters is used to create junit xml reports on CI. But when it loads before rails minitest plugin makes `Rails::TestUnitReporter` not being added as a reporter. minitest-ci is now only loaded at ci and does not interferes with rails minitest plugins. And keeps junit reports workings * Too heavy handed to actually run bundle Just like we don't auto-migrate * Pin js frameworks in importmap Instead of having importmap preconfigure it. * Match updated app/javascript path * No need for the explaining comment * Fixes test cases for replace webpack with importmapped Hotwire as default js (#42999) * Fix rubocop issues * Fix more railities test cases * Fix plugin generator railties shared test cases * Fix Action Text install generator asset pipeline spec * They're modules, not files * Let dev use the latest release as well So we don't have to replace unexisting dev releases with latest release * Make Webpack responsible for generating all the JS files it needs Webpacker 6 has already moved from app/javascript to app/packs. * Don't add rails/ujs by default any longer All the ajax/form functionality has been superseded by Turbo. The rest lives in a weird inbetween land we need to address through other means. * Use new importmap location * Switch to using turbo-rails and stimulus-rails directly The hotwire-rails gem does not offer enough value for its indirection * Use latest Webpacker * Prevent version resolution requests from getting swallowed * Use ESM syntax for imports * Move management of yarn, package.json, etc to Webpacker 6 * Update for Webpacker 6 * Move bin/setup addition to Webpacker as well * Remove dead tests * Bump to Webpacker 6.0.0.rc.2 * No longer relevant given the new default is no webpacker * Rely on Webpacker 6 * No longer relevant * No longer relevant * Make cable channel generator work for both webpacker and importmap setups * Fix tests * For tests testing importmap way * Use Webpacker 6 dummy * RuboCopping * One more bump to fix webpack-dev-server * Another bump. Hopefully the last one! * Also enough to not want turbo tracking on * Fix tests * Latest * Fix tests * Fix more tests * Fix tests Co-authored-by: Alex Ghiculescu <alex@tanda.co> Co-authored-by: André Luis Leal Cardoso Junior <andrehjr@gmail.com> Co-authored-by: Abhay Nikam <nikam.abhay1@gmail.com> Co-authored-by: Guillermo Iguaran <guilleiguaran@gmail.com>
2021-08-26 04:39:36 -04:00
if using_importmap?
pin_javascript_dependencies
elsif using_node?
install_javascript_dependencies
end
Replace webpack with importmapped Hotwire as default js (#42999) * Turbolinks is being replaced with Hotwire * Make --webpack opt-in * Don't use specific webpacker installers any more in preparation for next Webpacker * Update railties/lib/rails/app_updater.rb Co-authored-by: Alex Ghiculescu <alex@tanda.co> * Trailing whitespace * Convert to Turbo data attribute for tracking * Default is no webpack, no hotwire * Swap out turbolinks references for hotwire * Drop explicit return * Only generate package.json if using webpack * Only create package.json in webpack mode * Only create app/javascript in webpack mode * Generate correct style/js links based on js mode * Fix tests from changed output format Not sure why these are showing up in this PR, though. * Rubocopping * Stick with webpack for the test app for now * Adjust tests * Replace minitest-reporters with minitest-ci (#43016) minitest-reporters is used to create junit xml reports on CI. But when it loads before rails minitest plugin makes `Rails::TestUnitReporter` not being added as a reporter. minitest-ci is now only loaded at ci and does not interferes with rails minitest plugins. And keeps junit reports workings * Too heavy handed to actually run bundle Just like we don't auto-migrate * Pin js frameworks in importmap Instead of having importmap preconfigure it. * Match updated app/javascript path * No need for the explaining comment * Fixes test cases for replace webpack with importmapped Hotwire as default js (#42999) * Fix rubocop issues * Fix more railities test cases * Fix plugin generator railties shared test cases * Fix Action Text install generator asset pipeline spec * They're modules, not files * Let dev use the latest release as well So we don't have to replace unexisting dev releases with latest release * Make Webpack responsible for generating all the JS files it needs Webpacker 6 has already moved from app/javascript to app/packs. * Don't add rails/ujs by default any longer All the ajax/form functionality has been superseded by Turbo. The rest lives in a weird inbetween land we need to address through other means. * Use new importmap location * Switch to using turbo-rails and stimulus-rails directly The hotwire-rails gem does not offer enough value for its indirection * Use latest Webpacker * Prevent version resolution requests from getting swallowed * Use ESM syntax for imports * Move management of yarn, package.json, etc to Webpacker 6 * Update for Webpacker 6 * Move bin/setup addition to Webpacker as well * Remove dead tests * Bump to Webpacker 6.0.0.rc.2 * No longer relevant given the new default is no webpacker * Rely on Webpacker 6 * No longer relevant * No longer relevant * Make cable channel generator work for both webpacker and importmap setups * Fix tests * For tests testing importmap way * Use Webpacker 6 dummy * RuboCopping * One more bump to fix webpack-dev-server * Another bump. Hopefully the last one! * Also enough to not want turbo tracking on * Fix tests * Latest * Fix tests * Fix more tests * Fix tests Co-authored-by: Alex Ghiculescu <alex@tanda.co> Co-authored-by: André Luis Leal Cardoso Junior <andrehjr@gmail.com> Co-authored-by: Abhay Nikam <nikam.abhay1@gmail.com> Co-authored-by: Guillermo Iguaran <guilleiguaran@gmail.com>
2021-08-26 04:39:36 -04:00
end
create_channel_javascript_file
import_channel_in_javascript_entrypoint
end
2015-12-15 15:38:58 -05:00
end
private
def create_shared_channel_files
return if behavior != :invoke
copy_file "#{__dir__}/templates/application_cable/channel.rb",
"app/channels/application_cable/channel.rb"
copy_file "#{__dir__}/templates/application_cable/connection.rb",
"app/channels/application_cable/connection.rb"
end
def create_channel_file
template "channel.rb",
File.join("app/channels", class_path, "#{file_name}_channel.rb")
end
def create_shared_channel_javascript_files
template "javascript/index.js", "app/javascript/channels/index.js"
template "javascript/consumer.js", "app/javascript/channels/consumer.js"
end
def create_channel_javascript_file
channel_js_path = File.join("app/javascript/channels", class_path, "#{file_name}_channel")
js_template "javascript/channel", channel_js_path
gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" unless using_node?
end
def import_channels_in_javascript_entrypoint
2021-09-04 13:47:32 -04:00
append_to_file "app/javascript/application.js",
using_node? ? %(import "./channels"\n) : %(import "channels"\n)
end
def import_channel_in_javascript_entrypoint
append_to_file "app/javascript/channels/index.js",
2021-09-04 14:44:11 -04:00
using_node? ? %(import "./#{file_name}_channel"\n) : %(import "channels/#{file_name}_channel"\n)
end
def install_javascript_dependencies
say "Installing JavaScript dependencies", :green
run "yarn add @rails/actioncable"
end
def pin_javascript_dependencies
append_to_file "config/importmap.rb", <<-RUBY
pin "@rails/actioncable", to: "actioncable.esm.js"
pin_all_from "app/javascript/channels", under: "channels"
RUBY
end
2015-12-15 15:38:58 -05:00
def file_name
@_file_name ||= super.sub(/_channel\z/i, "")
2015-12-15 15:38:58 -05:00
end
def first_setup_required?
!root.join("app/javascript/channels/index.js").exist?
end
def using_javascript?
@using_javascript ||= options[:assets] && root.join("app/javascript").exist?
end
def using_node?
@using_node ||= root.join("package.json").exist?
end
def using_importmap?
@using_importmap ||= root.join("config/importmap.rb").exist?
end
def root
@root ||= Pathname(destination_root)
end
2015-12-15 15:38:58 -05:00
end
end
end