cable_ready 5.0.0.pre9 → 5.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -1
  3. data/Gemfile.lock +119 -100
  4. data/README.md +12 -15
  5. data/app/assets/javascripts/cable_ready.js +465 -155
  6. data/app/assets/javascripts/cable_ready.umd.js +449 -169
  7. data/app/channels/cable_ready/stream.rb +7 -5
  8. data/app/helpers/cable_ready/view_helper.rb +58 -0
  9. data/app/jobs/cable_ready/broadcast_job.rb +15 -0
  10. data/app/models/concerns/cable_ready/updatable/collection_updatable_callbacks.rb +2 -0
  11. data/app/models/concerns/cable_ready/updatable/collections_registry.rb +31 -5
  12. data/app/models/concerns/cable_ready/updatable/model_updatable_callbacks.rb +9 -4
  13. data/app/models/concerns/cable_ready/updatable.rb +107 -39
  14. data/app/models/concerns/extend_has_many.rb +2 -0
  15. data/cable_ready.gemspec +4 -6
  16. data/lib/cable_ready/broadcaster.rb +2 -0
  17. data/lib/cable_ready/cable_car.rb +2 -0
  18. data/lib/cable_ready/channel.rb +12 -4
  19. data/lib/cable_ready/channels.rb +3 -1
  20. data/lib/cable_ready/config.rb +17 -2
  21. data/lib/cable_ready/engine.rb +33 -14
  22. data/lib/cable_ready/identifiable.rb +23 -5
  23. data/lib/cable_ready/importmap.rb +3 -1
  24. data/lib/cable_ready/installer.rb +224 -0
  25. data/lib/cable_ready/operation_builder.rb +1 -1
  26. data/lib/cable_ready/sanity_checker.rb +1 -31
  27. data/lib/cable_ready/updatable/memory_cache_debounce_adapter.rb +22 -0
  28. data/lib/cable_ready/version.rb +1 -1
  29. data/lib/cable_ready.rb +5 -8
  30. data/lib/cable_ready_helper.rb +13 -0
  31. data/lib/generators/cable_ready/channel_generator.rb +51 -12
  32. data/lib/generators/cable_ready/templates/config/initializers/cable_ready.rb +15 -6
  33. data/lib/install/action_cable.rb +144 -0
  34. data/lib/install/broadcaster.rb +109 -0
  35. data/lib/install/bundle.rb +54 -0
  36. data/lib/install/compression.rb +51 -0
  37. data/lib/install/config.rb +39 -0
  38. data/lib/install/development.rb +34 -0
  39. data/lib/install/esbuild.rb +101 -0
  40. data/lib/install/importmap.rb +96 -0
  41. data/lib/install/initializers.rb +15 -0
  42. data/lib/install/mrujs.rb +121 -0
  43. data/lib/install/npm_packages.rb +13 -0
  44. data/lib/install/shakapacker.rb +65 -0
  45. data/lib/install/spring.rb +54 -0
  46. data/lib/install/updatable.rb +34 -0
  47. data/lib/install/vite.rb +66 -0
  48. data/lib/install/webpacker.rb +93 -0
  49. data/lib/install/yarn.rb +56 -0
  50. data/lib/tasks/cable_ready/cable_ready.rake +247 -0
  51. data/package.json +36 -22
  52. data/{rollup.config.js → rollup.config.mjs} +7 -25
  53. data/web-test-runner.config.mjs +12 -0
  54. data/yarn.lock +3058 -398
  55. metadata +39 -167
  56. data/IMPLEMENTATION.md +0 -93
  57. data/LATEST +0 -1
  58. data/app/assets/javascripts/cable_ready.min.js +0 -2
  59. data/app/assets/javascripts/cable_ready.min.js.map +0 -1
  60. data/app/assets/javascripts/cable_ready.umd.min.js +0 -2
  61. data/app/assets/javascripts/cable_ready.umd.min.js.map +0 -1
  62. data/app/helpers/cable_ready_helper.rb +0 -26
  63. data/app/jobs/cable_ready_broadcast_job.rb +0 -14
  64. data/lib/generators/cable_ready/helpers_generator.rb +0 -43
  65. data/lib/generators/cable_ready/initializer_generator.rb +0 -14
  66. data/test/dummy/app/channels/application_cable/channel.rb +0 -4
  67. data/test/dummy/app/channels/application_cable/connection.rb +0 -4
  68. data/test/dummy/app/controllers/application_controller.rb +0 -2
  69. data/test/dummy/app/helpers/application_helper.rb +0 -2
  70. data/test/dummy/app/jobs/application_job.rb +0 -7
  71. data/test/dummy/app/mailers/application_mailer.rb +0 -4
  72. data/test/dummy/app/models/application_record.rb +0 -3
  73. data/test/dummy/app/models/dugong.rb +0 -4
  74. data/test/dummy/app/models/global_idable_entity.rb +0 -16
  75. data/test/dummy/app/models/post.rb +0 -4
  76. data/test/dummy/app/models/section.rb +0 -6
  77. data/test/dummy/app/models/team.rb +0 -6
  78. data/test/dummy/app/models/topic.rb +0 -4
  79. data/test/dummy/app/models/user.rb +0 -7
  80. data/test/dummy/config/application.rb +0 -22
  81. data/test/dummy/config/boot.rb +0 -5
  82. data/test/dummy/config/environment.rb +0 -5
  83. data/test/dummy/config/environments/development.rb +0 -76
  84. data/test/dummy/config/environments/production.rb +0 -120
  85. data/test/dummy/config/environments/test.rb +0 -59
  86. data/test/dummy/config/initializers/application_controller_renderer.rb +0 -8
  87. data/test/dummy/config/initializers/assets.rb +0 -12
  88. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -8
  89. data/test/dummy/config/initializers/cable_ready.rb +0 -18
  90. data/test/dummy/config/initializers/content_security_policy.rb +0 -28
  91. data/test/dummy/config/initializers/cookies_serializer.rb +0 -5
  92. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -6
  93. data/test/dummy/config/initializers/inflections.rb +0 -16
  94. data/test/dummy/config/initializers/mime_types.rb +0 -4
  95. data/test/dummy/config/initializers/permissions_policy.rb +0 -11
  96. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  97. data/test/dummy/config/puma.rb +0 -43
  98. data/test/dummy/config/routes.rb +0 -3
  99. data/test/dummy/db/migrate/20210902154139_create_users.rb +0 -9
  100. data/test/dummy/db/migrate/20210902154153_create_posts.rb +0 -10
  101. data/test/dummy/db/migrate/20210904081930_create_topics.rb +0 -9
  102. data/test/dummy/db/migrate/20210904093607_create_sections.rb +0 -9
  103. data/test/dummy/db/migrate/20210913191735_create_teams.rb +0 -8
  104. data/test/dummy/db/migrate/20210913191759_add_team_reference_to_users.rb +0 -5
  105. data/test/dummy/db/migrate/20220329222959_create_dugongs.rb +0 -8
  106. data/test/dummy/db/migrate/20220329230221_create_active_storage_tables.active_storage.rb +0 -36
  107. data/test/dummy/db/schema.rb +0 -84
  108. data/test/dummy/test/models/dugong_test.rb +0 -7
  109. data/test/dummy/test/models/post_test.rb +0 -7
  110. data/test/dummy/test/models/section_test.rb +0 -7
  111. data/test/dummy/test/models/team_test.rb +0 -7
  112. data/test/dummy/test/models/topic_test.rb +0 -7
  113. data/test/dummy/test/models/user_test.rb +0 -7
  114. data/test/lib/cable_ready/cable_car_test.rb +0 -50
  115. data/test/lib/cable_ready/compoundable_test.rb +0 -26
  116. data/test/lib/cable_ready/helper_test.rb +0 -25
  117. data/test/lib/cable_ready/identifiable_test.rb +0 -69
  118. data/test/lib/cable_ready/operation_builder_test.rb +0 -189
  119. data/test/lib/cable_ready/updatable_test.rb +0 -135
  120. data/test/lib/generators/cable_ready/channel_generator_test.rb +0 -157
  121. data/test/support/generator_test_helpers.rb +0 -28
  122. data/test/test_helper.rb +0 -18
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cable_ready/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus["']:/ }
10
+ add_package "@hotwired/stimulus@^3.2"
11
+ else
12
+ say "⏩ @hotwired/stimulus npm package is already present. Skipping."
13
+ end
14
+
15
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
16
+ add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
17
+ else
18
+ say "⏩ @hotwired/stimulus-webpack-helpers npm package is already present. Skipping."
19
+ end
20
+
21
+ step_path = "/app/javascript/controllers/"
22
+ application_js_src = fetch(step_path, "application.js.tt")
23
+ application_js_path = controllers_path / "application.js"
24
+ index_src = fetch(step_path, "index.js.shakapacker.tt")
25
+ index_path = controllers_path / "index.js"
26
+
27
+ # create entrypoint/controllers, as well as the index, application and application_controller
28
+ empty_directory controllers_path unless controllers_path.exist?
29
+
30
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
31
+ copy_file(index_src, index_path) unless index_path.exist?
32
+
33
+ controllers_pattern = /import ['"]controllers['"]/
34
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
35
+
36
+ if pack.match?(controllers_pattern)
37
+ if pack.match?(controllers_commented_pattern)
38
+ proceed = if options.key? "uncomment"
39
+ options["uncomment"]
40
+ else
41
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
42
+ end
43
+
44
+ if proceed
45
+ # uncomment_lines only works with Ruby comments 🙄
46
+ lines = pack_path.readlines
47
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
48
+ lines[lines.index(matches.last).to_i] = "import \"controllers\"\n"
49
+ pack_path.write lines.join
50
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
51
+ else
52
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
53
+ end
54
+ else
55
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
56
+ end
57
+ else
58
+ lines = pack_path.readlines
59
+ matches = lines.select { |line| line =~ /^import / }
60
+ lines.insert lines.index(matches.last).to_i + 1, "import \"controllers\"\n"
61
+ pack_path.write lines.join
62
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
63
+ end
64
+
65
+ complete_step :shakapacker
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cable_ready/installer"
4
+
5
+ spring_pattern = /^[^#]*gem ["']spring["']/
6
+
7
+ proceed = false
8
+ lines = gemfile_path.readlines
9
+
10
+ if lines.index { |line| line =~ spring_pattern }
11
+ proceed = if options.key? "spring"
12
+ options["spring"]
13
+ else
14
+ !no?("✨ Would you like to disable the spring gem? \nIt's been removed from Rails 7, and is the frequent culprit behind countless mystery bugs. (Y/n)")
15
+ end
16
+ else
17
+ say "⏩ Spring is not installed."
18
+ end
19
+
20
+ if proceed
21
+ spring_watcher_pattern = /^[^#]*gem ["']spring-watcher-listen["']/
22
+ bin_rails_pattern = /^[^#]*load File.expand_path\("spring", __dir__\)/
23
+
24
+ if (index = lines.index { |line| line =~ spring_pattern })
25
+ remove_gem :spring
26
+
27
+ bin_spring = Rails.root.join("bin/spring")
28
+ if bin_spring.exist?
29
+ run "bin/spring binstub --remove --all"
30
+ say "✅ Removed spring binstubs"
31
+ end
32
+
33
+ bin_rails = Rails.root.join("bin/rails")
34
+ bin_rails_content = bin_rails.readlines
35
+ if (index = bin_rails_content.index { |line| line =~ bin_rails_pattern })
36
+ backup(bin_rails) do
37
+ bin_rails_content[index] = "# #{bin_rails_content[index]}"
38
+ bin_rails.write bin_rails_content.join
39
+ end
40
+ say "✅ Removed spring from bin/rails"
41
+ end
42
+ create_file "tmp/cable_ready_installer/kill_spring", verbose: false
43
+ else
44
+ say "✅ spring has been successfully removed"
45
+ end
46
+
47
+ if lines.index { |line| line =~ spring_watcher_pattern }
48
+ remove_gem "spring-watcher-listen"
49
+ end
50
+ else
51
+ say "⏩ Skipping."
52
+ end
53
+
54
+ complete_step :spring
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cable_ready/installer"
4
+
5
+ if application_record_path.exist?
6
+ lines = application_record_path.readlines
7
+
8
+ if !lines.index { |line| line =~ /^\s*include CableReady::Updatable/ }
9
+ proceed = if options.key? "updatable"
10
+ options["updatable"]
11
+ else
12
+ !no?("✨ Include CableReady::Updatable in Active Record model classes? (Y/n)")
13
+ end
14
+
15
+ unless proceed
16
+ complete_step :updatable
17
+
18
+ puts "⏩ Skipping."
19
+ return
20
+ end
21
+
22
+ index = lines.index { |line| line.include?("class ApplicationRecord < ActiveRecord::Base") }
23
+ lines.insert index + 1, " include CableReady::Updatable\n"
24
+ application_record_path.write lines.join
25
+
26
+ say "✅ included CableReady::Updatable in ApplicationRecord"
27
+ else
28
+ say "⏩ CableReady::Updatable has already been included in Active Record model classes. Skipping."
29
+ end
30
+ else
31
+ say "⏩ ApplicationRecord doesn't exist. Skipping."
32
+ end
33
+
34
+ complete_step :updatable
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cable_ready/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus["']:/ }
10
+ add_package "@hotwired/stimulus@^3.2"
11
+ else
12
+ say "⏩ @hotwired/stimulus npm package is already present. Skipping."
13
+ end
14
+
15
+ if !lines.index { |line| line =~ /^\s*["']stimulus-vite-helpers["']: ["']\^3["']/ }
16
+ add_package "stimulus-vite-helpers@^3"
17
+ else
18
+ say "⏩ @stimulus-vite-helpers npm package is already present. Skipping."
19
+ end
20
+
21
+ step_path = "/app/javascript/controllers/"
22
+ application_js_src = fetch(step_path, "application.js.tt")
23
+ application_js_path = controllers_path / "application.js"
24
+ index_src = fetch(step_path, "index.js.vite.tt")
25
+ index_path = controllers_path / "index.js"
26
+
27
+ # create entrypoint/controllers, as well as the index, application and application_controller
28
+ empty_directory controllers_path unless controllers_path.exist?
29
+
30
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
31
+ copy_file(index_src, index_path) unless index_path.exist?
32
+
33
+ controllers_pattern = /import ['"](\.\.\/)?controllers['"]/
34
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
35
+ prefix = "..\/" # standard:disable Style/RedundantStringEscape
36
+
37
+ if pack.match?(controllers_pattern)
38
+ if pack.match?(controllers_commented_pattern)
39
+ proceed = if options.key? "uncomment"
40
+ options["uncomment"]
41
+ else
42
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
43
+ end
44
+
45
+ if proceed
46
+ # uncomment_lines only works with Ruby comments 🙄
47
+ lines = pack_path.readlines
48
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
49
+ lines[lines.index(matches.last).to_i] = "import \"#{prefix}controllers\"\n"
50
+ pack_path.write lines.join
51
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
52
+ else
53
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
54
+ end
55
+ else
56
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
57
+ end
58
+ else
59
+ lines = pack_path.readlines
60
+ matches = lines.select { |line| line =~ /^import / }
61
+ lines.insert lines.index(matches.last).to_i + 1, "import \"#{prefix}controllers\"\n"
62
+ pack_path.write lines.join
63
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
64
+ end
65
+
66
+ complete_step :vite
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cable_ready/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+ if !lines.index { |line| line =~ /^\s*["']webpack["']: ["']\^4.46.0["']/ }
10
+ add_package "webpack@^4.46.0"
11
+ else
12
+ say "⏩ webpack npm package is already present. Skipping."
13
+ end
14
+
15
+ if !lines.index { |line| line =~ /^\s*["']webpack-cli["']: ["']\^3.3.12["']/ }
16
+ add_package "webpack-cli@^3.3.12"
17
+ else
18
+ say "⏩ webpack-cli npm package is already present. Skipping."
19
+ end
20
+
21
+ if !lines.index { |line| line =~ /^\s*["']@rails\/webpacker["']: ["']\^5.4.3["']/ }
22
+ add_package "@rails/webpacker@^5.4.3"
23
+ else
24
+ say "⏩ @rails/webpacker npm package is already present. Skipping."
25
+ end
26
+
27
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus["']:/ }
28
+ add_package "@hotwired/stimulus@^3.2"
29
+ else
30
+ say "⏩ @hotwired/stimulus npm package is already present. Skipping."
31
+ end
32
+
33
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
34
+ add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
35
+ else
36
+ say "⏩ @hotwired/stimulus-webpack-helpers npm package is already present. Skipping."
37
+ end
38
+
39
+ if !lines.index { |line| line =~ /^\s*["']webpack-dev-server["']: ["']\^3.11.3["']/ }
40
+ add_dev_package "webpack-dev-server@^3.11.3"
41
+ else
42
+ say "⏩ @webpack-dev-server is already present. Skipping."
43
+ end
44
+
45
+ step_path = "/app/javascript/controllers/"
46
+ application_js_src = fetch(step_path, "application.js.tt")
47
+ application_js_path = controllers_path / "application.js"
48
+ index_src = fetch(step_path, "index.js.webpacker.tt")
49
+ index_path = controllers_path / "index.js"
50
+
51
+ # create entrypoint/controllers, as well as the index, application and application_controller
52
+ empty_directory controllers_path unless controllers_path.exist?
53
+
54
+ # webpacker 5.4 did not colloquially feature a controllers/application.js file
55
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
56
+ copy_file(index_src, index_path) unless index_path.exist?
57
+
58
+ controllers_pattern = /import ['"]controllers['"]/
59
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
60
+
61
+ if pack.match?(controllers_pattern)
62
+ if pack.match?(controllers_commented_pattern)
63
+ proceed = if options.key? "uncomment"
64
+ options["uncomment"]
65
+ else
66
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
67
+ end
68
+
69
+ if proceed
70
+ # uncomment_lines only works with Ruby comments 🙄
71
+ lines = pack_path.readlines
72
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
73
+ lines[lines.index(matches.last).to_i] = "import \"controllers\"\n"
74
+ pack_path.write lines.join
75
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
76
+ else
77
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
78
+ end
79
+ else
80
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
81
+ end
82
+ else
83
+ lines = pack_path.readlines
84
+ matches = lines.select { |line| line =~ /^import / }
85
+ lines.insert lines.index(matches.last).to_i + 1, "import \"controllers\"\n"
86
+ pack_path.write lines.join
87
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
88
+ end
89
+
90
+ # ensure webpacker is installed in the Gemfile
91
+ add_gem "webpacker@5.4.3"
92
+
93
+ complete_step :webpacker
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cable_ready/installer"
4
+
5
+ if !package_json.exist?
6
+ say "⏩ No package.json file found. Skipping."
7
+
8
+ return
9
+ end
10
+
11
+ # run yarn install only when packages are waiting to be added or removed
12
+ add = package_list.exist? ? package_list.readlines.map(&:chomp) : []
13
+ dev = dev_package_list.exist? ? dev_package_list.readlines.map(&:chomp) : []
14
+ drop = drop_package_list.exist? ? drop_package_list.readlines.map(&:chomp) : []
15
+
16
+ json = JSON.parse(package_json.read)
17
+
18
+ if add.present? || dev.present? || drop.present?
19
+
20
+ add.each do |package|
21
+ matches = package.match(/(.+)@(.+)/)
22
+ name, version = matches[1], matches[2]
23
+ json["dependencies"] = {} unless json["dependencies"]
24
+ json["dependencies"][name] = version
25
+ end
26
+
27
+ dev.each do |package|
28
+ matches = package.match(/(.+)@(.+)/)
29
+ name, version = matches[1], matches[2]
30
+ json["devDependencies"] = {} unless json["devDependencies"]
31
+ json["devDependencies"][name] = version
32
+ end
33
+
34
+ drop.each do |package|
35
+ json["dependencies"].delete(package)
36
+ json["devDependencies"].delete(package)
37
+ end
38
+
39
+ package_json.write JSON.pretty_generate(json)
40
+
41
+ system "yarn install --silent"
42
+ else
43
+ say "⏩ No yarn depdencies to add or remove. Skipping."
44
+
45
+ end
46
+
47
+ if bundler == "esbuild" && json["scripts"]["build"] != "node esbuild.config.mjs"
48
+ json["scripts"]["build:default"] = json["scripts"]["build"]
49
+ json["scripts"]["build"] = "node esbuild.config.mjs"
50
+ package_json.write JSON.pretty_generate(json)
51
+ say "✅ Your build script has been updated to use esbuild.config.mjs"
52
+ else
53
+ say "⏩ Your build script is already setup. Skipping."
54
+ end
55
+
56
+ complete_step :yarn
@@ -0,0 +1,247 @@
1
+ # frozen_string_literal: true
2
+
3
+ CR_STEPS = {
4
+ "action_cable" => "Action Cable",
5
+ "webpacker" => "Install CableReady using Webpacker",
6
+ "shakapacker" => "Install CableReady using Shakapacker",
7
+ "npm_packages" => "Install CableReady npm package",
8
+ "importmap" => "Install CableReady using importmaps",
9
+ "esbuild" => "Install CableReady using esbuild",
10
+ "config" => "Client initialization",
11
+ "initializers" => "Generate and configure initializer",
12
+ "development" => "development environment configuration",
13
+ "spring" => "Disable spring gem. Spring has been removed from Rails 7",
14
+ "mrujs" => "Swap out Rails UJS for mrujs",
15
+ "broadcaster" => "Make CableReady::Broadcaster available to channels, controllers, jobs and models",
16
+ "updatable" => "Include CableReady::Updatable in Active Record model classes",
17
+ "vite" => "Install CableReady using Vite",
18
+ "compression" => "Compress WebSocket traffic with gzip"
19
+ }
20
+
21
+ CR_BUNDLERS = {
22
+ "webpacker" => ["npm_packages", "webpacker", "config", "action_cable", "development", "initializers", "broadcaster", "updatable", "spring", "yarn", "bundle"],
23
+ "esbuild" => ["npm_packages", "esbuild", "config", "action_cable", "development", "initializers", "broadcaster", "updatable", "spring", "yarn", "bundle"],
24
+ "vite" => ["npm_packages", "vite", "config", "action_cable", "development", "initializers", "broadcaster", "updatable", "spring", "yarn", "bundle"],
25
+ "shakapacker" => ["npm_packages", "shakapacker", "config", "action_cable", "development", "initializers", "broadcaster", "updatable", "spring", "yarn", "bundle"],
26
+ "importmap" => ["config", "action_cable", "importmap", "development", "initializers", "broadcaster", "updatable", "spring", "bundle"]
27
+ }
28
+
29
+ def run_install_template(template, force: false, trace: false)
30
+ puts "--- [#{template}] ----"
31
+
32
+ if Rails.root.join("tmp/cable_ready_installer/halt").exist?
33
+ FileUtils.rm(Rails.root.join("tmp/cable_ready_installer/halt"))
34
+ puts "CableReady installation halted. Please fix the issues above and try again."
35
+ exit
36
+ end
37
+ if Rails.root.join("tmp/cable_ready_installer/#{template}").exist? && !force
38
+ puts "👍 Step #{template} already completed. Skipping."
39
+ return
40
+ end
41
+
42
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/#{template}.rb", __dir__)} SKIP_SANITY_CHECK=true #{"--trace" if trace}"
43
+
44
+ puts
45
+ end
46
+
47
+ namespace :cable_ready do
48
+ desc "✨ Install CableReady ✨"
49
+ task :install do
50
+ FileUtils.mkdir_p(Rails.root.join("tmp/cable_ready_installer/templates"))
51
+ FileUtils.mkdir_p(Rails.root.join("tmp/cable_ready_installer/working"))
52
+ install_complete = Rails.root.join("tmp/cable_ready_installer/complete")
53
+
54
+ bundler = nil
55
+ options = {}
56
+
57
+ ARGV.each do |arg|
58
+ # make sure we have a valid build tool specified, or proceed to automatic detection
59
+ if ["webpacker", "esbuild", "vite", "shakapacker", "importmap"].include?(arg)
60
+ bundler = arg
61
+ else
62
+ kv = arg.split("=")
63
+ if kv.length == 2
64
+ kv[1] = if kv[1] == "true"
65
+ true
66
+ else
67
+ (kv[1] == "false") ? false : kv[1]
68
+ end
69
+ options[kv[0]] = kv[1]
70
+ end
71
+ end
72
+ end
73
+
74
+ options_path = Rails.root.join("tmp/cable_ready_installer/options")
75
+ options_path.write(options.to_yaml)
76
+
77
+ if defined?(StimulusReflex)
78
+ puts "✨ \e[38;5;220mStimulusReflex\e[0m is present in this project ✨"
79
+ puts
80
+ puts "CableReady will be installed with StimulusReflex. Just run: \e[38;5;231mrails stimulus_reflex:install\e[0m"
81
+ puts
82
+ puts "Get help on Discord: \e[4;97mhttps://discord.gg/stimulus-reflex\e[0m. \e[38;5;196mWe are here for you.\e[0m 💙"
83
+ puts
84
+ exit
85
+ end
86
+
87
+ if install_complete.exist?
88
+ puts "✨ \e[38;5;220mCableReady\e[0m is already installed ✨"
89
+ puts
90
+ puts "To restart the installation process, run: \e[38;5;231mrails cable_ready:install:restart\e[0m"
91
+ puts
92
+ puts "To get started, check out \e[4;97mhttps://cableready.stimulusreflex.com/guide/cableready-101\e[0m"
93
+ puts "or get help on Discord: \e[4;97mhttps://discord.gg/stimulus-reflex\e[0m. \e[38;5;196mWe are here for you.\e[0m 💙"
94
+ puts
95
+ exit
96
+ end
97
+
98
+ # if there is an installation in progress, continue where we left off
99
+ cached_entrypoint = Rails.root.join("tmp/cable_ready_installer/entrypoint")
100
+
101
+ if cached_entrypoint.exist?
102
+ entrypoint = File.read(cached_entrypoint)
103
+ puts "✨ Resuming \e[38;5;220mCableReady\e[0m installation ✨"
104
+ puts
105
+ puts "If you have any setup issues, please consult \e[4;97mhttps://cableready.stimulusreflex.com/hello-world/setup\e[0m"
106
+ puts "or get help on Discord: \e[4;97mhttps://discord.gg/stimulus-reflex\e[0m. \e[38;5;196mWe are here for you.\e[0m 💙"
107
+ puts
108
+ puts "Resuming installation into \e[1m#{entrypoint}\e[22m"
109
+ puts "Run \e[1;94mrails cable_ready:install:restart\e[0m to restart the installation process"
110
+ puts
111
+ else
112
+ puts "✨ Installing \e[38;5;220mCableReady\e[0m ✨"
113
+ puts
114
+ puts "If you have any setup issues, please consult \e[4;97mhttps://cableready.stimulusreflex.com/hello-world/setup\e[0m"
115
+ puts "or get help on Discord: \e[4;97mhttps://discord.gg/stimulus-reflex\e[0m. \e[38;5;196mWe are here for you.\e[0m 💙"
116
+ if Rails.root.join(".git").exist?
117
+ puts
118
+ puts "We recommend running \e[1;94mgit commit\e[0m before proceeding. A diff will be generated at the end."
119
+ end
120
+
121
+ if options.key? "entrypoint"
122
+ entrypoint = options["entrypoint"]
123
+ else
124
+ entrypoint = [
125
+ "app/javascript",
126
+ "app/frontend"
127
+ ].find { |path| File.exist?(Rails.root.join(path)) } || "app/javascript"
128
+
129
+ puts
130
+ puts "Where do JavaScript files live in your app? Our best guess is: \e[1m#{entrypoint}\e[22m 🤔"
131
+ puts "Press enter to accept this, or type a different path."
132
+ print "> "
133
+ input = $stdin.gets.chomp
134
+ entrypoint = input unless input.blank?
135
+ end
136
+ File.write(cached_entrypoint, entrypoint)
137
+ end
138
+
139
+ # verify their bundler before starting, unless they explicitly specified on CLI
140
+ if !bundler
141
+ # auto-detect build tool based on existing packages and configuration
142
+ if Rails.root.join("config/importmap.rb").exist?
143
+ bundler = "importmap"
144
+ elsif Rails.root.join("package.json").exist?
145
+ package_json = File.read(Rails.root.join("package.json"))
146
+ bundler = "webpacker" if package_json.include?('"@rails/webpacker":')
147
+ bundler = "esbuild" if package_json.include?('"esbuild":')
148
+ bundler = "vite" if package_json.include?('"vite":')
149
+ bundler = "shakapacker" if package_json.include?('"shakapacker":')
150
+ if !bundler
151
+ puts "❌ You must be using a node-based bundler such as esbuild, webpacker, vite or shakapacker (package.json) or importmap (config/importmap.rb) to use CableReady."
152
+ exit
153
+ end
154
+ else
155
+ puts "❌ You must be using a node-based bundler such as esbuild, webpacker, vite or shakapacker (package.json) or importmap (config/importmap.rb) to use CableReady."
156
+ exit
157
+ end
158
+
159
+ puts
160
+ puts "It looks like you're using \e[1m#{bundler}\e[22m as your bundler. Is that correct? (Y/n)"
161
+ print "> "
162
+ input = $stdin.gets.chomp
163
+ if input.downcase == "n"
164
+ puts
165
+ puts "CableReady installation supports: esbuild, webpacker, vite, shakapacker and importmap."
166
+ puts "Please run \e[1;94mrails cable_ready:install [bundler]\e[0m to install CableReady."
167
+ exit
168
+ end
169
+ end
170
+
171
+ File.write("tmp/cable_ready_installer/bundler", bundler)
172
+ FileUtils.touch("tmp/cable_ready_installer/backups")
173
+ File.write("tmp/cable_ready_installer/template_src", File.expand_path("../../generators/cable_ready/templates/", __dir__))
174
+
175
+ # do the things
176
+ CR_BUNDLERS[bundler].each do |template|
177
+ run_install_template(template, trace: !!options["trace"])
178
+ end
179
+
180
+ puts
181
+ puts "🎉 \e[1;92mCableReady has been successfully installed!\e[22m 🎉"
182
+ puts
183
+ puts "👉 \e[4;97mhttps://cableready.stimulusreflex.com/guide/cableready-101\e[0m"
184
+ puts
185
+ puts "Join over 2000 CableReady developers on Discord: \e[4;97mhttps://discord.gg/stimulus-reflex\e[0m"
186
+ puts
187
+
188
+ backups = File.readlines("tmp/cable_ready_installer/backups").map(&:chomp)
189
+
190
+ if backups.any?
191
+ puts "🙆 The following files were modified during installation:"
192
+ puts
193
+ backups.each { |backup| puts " #{backup}" }
194
+ puts
195
+ puts "Each of these files has been backed up with a .bak extension. Please review the changes carefully."
196
+ puts "If you're happy with the changes, you can delete the .bak files."
197
+ puts
198
+ end
199
+
200
+ if Rails.root.join(".git").exist?
201
+ system "git diff > tmp/cable_ready_installer.diff"
202
+ puts "🏮 A diff of all changes has been saved to \e[1mtmp/cable_ready_installer.diff\e[22m"
203
+ puts
204
+ end
205
+
206
+ FileUtils.touch(install_complete)
207
+ `pkill -f spring` if Rails.root.join("tmp/cable_ready_installer/kill_spring").exist?
208
+ exit
209
+ end
210
+
211
+ namespace :install do
212
+ desc "Restart CableReady installation"
213
+ task :restart do
214
+ FileUtils.rm_rf Rails.root.join("tmp/cable_ready_installer")
215
+ system "rails cable_ready:install #{ARGV.join(" ")}"
216
+ exit
217
+ end
218
+
219
+ desc <<~DESC
220
+ Run specific CableReady install steps
221
+
222
+ #{CR_STEPS.sort.map { |step, description| "#{step.ljust(20)} #{description}" }.join("\n")}
223
+ DESC
224
+
225
+ task :step do
226
+ def warning(step = nil)
227
+ return if step.to_s.include?("=")
228
+ if step
229
+ puts "⚠️ #{step} is not a valid step. Valid steps are: #{CR_STEPS.keys.join(", ")}"
230
+ else
231
+ puts "❌ You must specify a step to re-run. Valid steps are: #{CR_STEPS.keys.join(", ")}"
232
+ puts "Example: \e[1;94mrails cable_ready:install:step initializers\e[0m"
233
+ end
234
+ end
235
+
236
+ warning if ARGV.empty?
237
+
238
+ ARGV.each do |step|
239
+ CR_STEPS.include?(step) ? run_install_template(step, force: true) : warning(step)
240
+ end
241
+
242
+ run_install_template(:bundle, force: true)
243
+ run_install_template(:yarn, force: true)
244
+ exit
245
+ end
246
+ end
247
+ end