Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/install/cable_ready_entrypoint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
if (js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist?
entrypoint_content = File.read(js_entrypoint_path)

say "Import CableReady"
append_to_file js_entrypoint_path, %(import CableReady from "cable_ready"\n)

say "Initializing CableReady with ActionCable consumer"

# Check if there's a channels file, i.e. we're in a webpacker or esbuild setup
if Rails.root.join("app/javascript/channels/index.js").exist?
unless entrypoint_content.match?(/import consumer/)
append_to_file js_entrypoint_path, %(import consumer from "./channels/consumer"\n)
end

append_to_file js_entrypoint_path, %(CableReady.initialize({ consumer })\n)

# else we could use the cable.consumer from turbo-rails, if present
elsif entrypoint_content.match?(/import.*@hotwired\/turbo-rails/)
unless entrypoint_content.match?(/import.*cable.*@hotwired\/turbo-rails/)
append_to_file js_entrypoint_path, %(import { cable } from "@hotwired\/turbo-rails"\n)
end

append_to_file js_entrypoint_path, <<~JS
(async () => {
const consumer = await cable.getConsumer()
CableReady.initialize({ consumer });
})()
JS

# else remind the user to initialize CableReady
else
say "Please initialize CableReady in #{js_entrypoint_path} with an ActionCable consumer.", :red
end
else
say "You must import and initialize cable_ready in your JavaScript entrypoint file", :red
end
2 changes: 2 additions & 0 deletions lib/install/cable_ready_with_importmap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
say "Pin CableReady"
run "bin/importmap pin cable_ready"
2 changes: 2 additions & 0 deletions lib/install/cable_ready_with_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
say "Install CableReady"
run "yarn add cable_ready"
17 changes: 17 additions & 0 deletions lib/install/check_cable_and_redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
say "Checking ActionCable and Redis"

say "ActionCable configuration (config/cable.yml) is missing. CableReady depends on ActionCable, please provide a config file.", :red unless Rails.root.join("config/cable.yml").exist?

gemfile_content = File.read(Rails.root.join("Gemfile"))
redis_pattern = /gem ['"]redis['"]/

if gemfile_content.match?(redis_pattern)
if gemfile_content.match?(/\s*#\s*#{redis_pattern}/) && yes?("Redis seems to be commented out in your Gemfile. Do you want CableReady to uncomment it?")
uncomment_lines "Gemfile", redis_pattern
end
elsif yes?("Redis doesn't seem to be installed in your Rails app. Would you like CableReady to install it?")
append_file "Gemfile", "\n# Use Redis for Action Cable"
gem "redis", "~> 4.0"
end

run_bundle
36 changes: 36 additions & 0 deletions lib/tasks/cable_ready_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def run_install_template(template)
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{template}.rb", __dir__)}"
end

def check_cable_and_redis
run_install_template("check_cable_and_redis")
end

namespace :cable_ready do
desc "Install CableReady into the app"
task :install do
if Rails.root.join("config/importmap.rb").exist?
Rake::Task["cable_ready:install:importmap"].invoke
elsif Rails.root.join("package.json").exist?
Rake::Task["cable_ready:install:node"].invoke
else
puts "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem."
end
end

namespace :install do
desc "Install CableReady into the app with asset pipeline"
task :importmap do
check_cable_and_redis
run_install_template("cable_ready_with_importmap")
run_install_template("cable_ready_entrypoint")
end

desc "Install CableReady into the app with node"
task :node do
check_cable_and_redis
run_install_template("cable_ready_with_node")
run_install_template("cable_ready_entrypoint")
end
end
end