From de710c16a7801927b64b1c50c64fbfbe5277df31 Mon Sep 17 00:00:00 2001 From: Jason Meller Date: Tue, 12 Sep 2023 09:28:41 -0400 Subject: [PATCH 1/3] Use bun for installation if applicable --- lib/install/turbo_with_bun.rb | 9 +++++++++ lib/tasks/turbo_tasks.rake | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/install/turbo_with_bun.rb diff --git a/lib/install/turbo_with_bun.rb b/lib/install/turbo_with_bun.rb new file mode 100644 index 00000000..33e94fd6 --- /dev/null +++ b/lib/install/turbo_with_bun.rb @@ -0,0 +1,9 @@ +if (js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist? + say "Import Turbo" + append_to_file "app/javascript/application.js", %(import "@hotwired/turbo-rails"\n) +else + say "You must import @hotwired/turbo-rails in your JavaScript entrypoint file", :red +end + +say "Install Turbo" +run "bun add @hotwired/turbo-rails" diff --git a/lib/tasks/turbo_tasks.rake b/lib/tasks/turbo_tasks.rake index 9e936f74..7ca75271 100644 --- a/lib/tasks/turbo_tasks.rake +++ b/lib/tasks/turbo_tasks.rake @@ -4,7 +4,7 @@ end def redis_installed? Gem.win_platform? ? - system('where redis-server > NUL 2>&1') : + system('where redis-server > NUL 2>&1') : system('which redis-server > /dev/null') end @@ -16,11 +16,21 @@ def switch_on_redis_if_available end end +def using_bun? + Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) +end + +def tool_exists?(tool) + system "command -v #{tool} > /dev/null" +end + namespace :turbo do desc "Install Turbo into the app" task :install do if Rails.root.join("config/importmap.rb").exist? Rake::Task["turbo:install:importmap"].invoke + elsif Rails.root.join("package.json").exist? && using_bun? + Rake::Task["turbo:install:bun"].invoke elsif Rails.root.join("package.json").exist? Rake::Task["turbo:install:node"].invoke else @@ -41,6 +51,12 @@ namespace :turbo do switch_on_redis_if_available end + desc "Install Turbo into the app with bun" + task :bun do + run_turbo_install_template "turbo_with_bun" + switch_on_redis_if_available + end + desc "Switch on Redis and use it in development" task :redis do run_turbo_install_template "turbo_needs_redis" From 942c5e11b267314288776412b1405a2f7f9097bc Mon Sep 17 00:00:00 2001 From: Jason Meller Date: Wed, 13 Sep 2023 09:17:38 -0400 Subject: [PATCH 2/3] Namespace rake helpers so they don't conflict --- lib/tasks/turbo_tasks.rake | 61 +++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/lib/tasks/turbo_tasks.rake b/lib/tasks/turbo_tasks.rake index 7ca75271..94c00329 100644 --- a/lib/tasks/turbo_tasks.rake +++ b/lib/tasks/turbo_tasks.rake @@ -1,35 +1,42 @@ -def run_turbo_install_template(path) - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" -end +module Turbo + module Tasks + extend self + def run_turbo_install_template(path) + system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" + end -def redis_installed? - Gem.win_platform? ? - system('where redis-server > NUL 2>&1') : - system('which redis-server > /dev/null') -end + def redis_installed? + Gem.win_platform? ? + system('where redis-server > NUL 2>&1') : + system('which redis-server > /dev/null') + end + + def switch_on_redis_if_available + if redis_installed? + Rake::Task["turbo:install:redis"].invoke + else + puts "Run turbo:install:redis to switch on Redis and use it in development for turbo streams" + end + end -def switch_on_redis_if_available - if redis_installed? - Rake::Task["turbo:install:redis"].invoke - else - puts "Run turbo:install:redis to switch on Redis and use it in development for turbo streams" + def using_bun? + Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) + end + + def tool_exists?(tool) + system "command -v #{tool} > /dev/null" + end end end -def using_bun? - Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) -end -def tool_exists?(tool) - system "command -v #{tool} > /dev/null" -end namespace :turbo do desc "Install Turbo into the app" task :install do if Rails.root.join("config/importmap.rb").exist? Rake::Task["turbo:install:importmap"].invoke - elsif Rails.root.join("package.json").exist? && using_bun? + elsif Rails.root.join("package.json").exist? && Turbo::Tasks.using_bun? Rake::Task["turbo:install:bun"].invoke elsif Rails.root.join("package.json").exist? Rake::Task["turbo:install:node"].invoke @@ -41,25 +48,25 @@ namespace :turbo do namespace :install do desc "Install Turbo into the app with asset pipeline" task :importmap do - run_turbo_install_template "turbo_with_importmap" - switch_on_redis_if_available + Turbo::Tasks.run_turbo_install_template "turbo_with_importmap" + Turbo::Tasks.switch_on_redis_if_available end desc "Install Turbo into the app with webpacker" task :node do - run_turbo_install_template "turbo_with_node" - switch_on_redis_if_available + Turbo::Tasks.run_turbo_install_template "turbo_with_node" + Turbo::Tasks.switch_on_redis_if_available end desc "Install Turbo into the app with bun" task :bun do - run_turbo_install_template "turbo_with_bun" - switch_on_redis_if_available + Turbo::Tasks.run_turbo_install_template "turbo_with_bun" + Turbo::Tasks.switch_on_redis_if_available end desc "Switch on Redis and use it in development" task :redis do - run_turbo_install_template "turbo_needs_redis" + Turbo::Tasks.run_turbo_install_template "turbo_needs_redis" end end end From 48719a5bcff0887bb3869e5c5b0351726347b9fa Mon Sep 17 00:00:00 2001 From: Jason Meller Date: Sat, 23 Sep 2023 09:27:19 -0400 Subject: [PATCH 3/3] Implement review suggestions --- lib/tasks/turbo_tasks.rake | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/tasks/turbo_tasks.rake b/lib/tasks/turbo_tasks.rake index 94c00329..de72ac8f 100644 --- a/lib/tasks/turbo_tasks.rake +++ b/lib/tasks/turbo_tasks.rake @@ -20,17 +20,11 @@ module Turbo end def using_bun? - Rails.root.join("bun.lockb").exist? || (tool_exists?('bun') && !Rails.root.join("yarn.lock").exist?) - end - - def tool_exists?(tool) - system "command -v #{tool} > /dev/null" + Rails.root.join("bun.config.js").exist? end end end - - namespace :turbo do desc "Install Turbo into the app" task :install do