From 0875c94b0997309040a85a60458e065b0ac1d91b Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 26 Sep 2019 14:56:22 -0400 Subject: [PATCH 1/4] Create ActionText install generator --- .../action_text/install/install_generator.rb | 71 +++++++++++++++++++ .../install}/templates/actiontext.scss | 0 .../generators/test_unit/install_generator.rb | 13 ++++ .../test_unit}/templates/fixtures.yml | 0 actiontext/lib/tasks/actiontext.rake | 20 ------ actiontext/lib/templates/installer.rb | 45 ------------ 6 files changed, 84 insertions(+), 65 deletions(-) create mode 100644 actiontext/lib/generators/action_text/install/install_generator.rb rename actiontext/lib/{ => generators/action_text/install}/templates/actiontext.scss (100%) create mode 100644 actiontext/lib/rails/generators/test_unit/install_generator.rb rename actiontext/lib/{ => rails/generators/test_unit}/templates/fixtures.yml (100%) delete mode 100644 actiontext/lib/tasks/actiontext.rake delete mode 100644 actiontext/lib/templates/installer.rb diff --git a/actiontext/lib/generators/action_text/install/install_generator.rb b/actiontext/lib/generators/action_text/install/install_generator.rb new file mode 100644 index 0000000000..72328842cf --- /dev/null +++ b/actiontext/lib/generators/action_text/install/install_generator.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "pathname" +require "json" + +module ActionText + module Generators + class InstallGenerator < ::Rails::Generators::Base + source_root File.expand_path("templates", __dir__) + + def install_javascript_dependencies + template "#{__dir__}/../../../../../railties/lib/rails/generators/rails/app/templates/bin/yarn.tt", + "bin/yarn" + + say "Installing JavaScript dependencies" + run "yarn add #{js_dependencies.map { |name, version| "#{name}@#{version}" }.join(" ")}", + abort_on_failure: true, capture: true + end + + def append_dependencies_to_package_file + app_javascript_pack_path = Pathname.new("app/javascript/packs/application.js") + + if app_javascript_pack_path.exist? + js_dependencies.keys.each do |name| + line = %[require("#{name}")] + + unless app_javascript_pack_path.read.include? line + say "Adding #{name} to #{app_javascript_pack_path}" + append_to_file app_javascript_pack_path, "\n#{line}" + end + end + else + warn <<~WARNING + WARNING: Action Text can't locate your JavaScript bundle to add its package dependencies. + + Add these lines to any bundles: + + require("trix") + require("@rails/actiontext") + + Alternatively, install and setup the webpacker gem then rerun `bin/rails action_text:install` + to have these dependencies added automatically. + WARNING + end + end + + def create_actiontext_files + template "actiontext.scss", "app/assets/stylesheets/actiontext.scss" + + copy_file "#{__dir__}/../../../../app/views/active_storage/blobs/_blob.html.erb", + "app/views/active_storage/blobs/_blob.html.erb" + end + + def create_migrations + run "rake active_storage:install:migrations" + run "rake railties:install:migrations" + run "rake action_text:install:migrations" + end + + def js_dependencies + package_contents = File.read(Pathname.new("#{__dir__}/../../../../package.json")) + js_package = JSON.load(package_contents) + + js_package["peerDependencies"].dup.merge \ + js_package["name"] => "^#{js_package["version"]}" + end + + hook_for :test_framework + end + end +end diff --git a/actiontext/lib/templates/actiontext.scss b/actiontext/lib/generators/action_text/install/templates/actiontext.scss similarity index 100% rename from actiontext/lib/templates/actiontext.scss rename to actiontext/lib/generators/action_text/install/templates/actiontext.scss diff --git a/actiontext/lib/rails/generators/test_unit/install_generator.rb b/actiontext/lib/rails/generators/test_unit/install_generator.rb new file mode 100644 index 0000000000..1224791639 --- /dev/null +++ b/actiontext/lib/rails/generators/test_unit/install_generator.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module TestUnit + module Generators + class InstallGenerator < ::Rails::Generators::Base + source_root File.expand_path("templates", __dir__) + + def create_test_files + template "fixtures.yml", "test/fixtures/action_text/rich_texts.yml" + end + end + end +end diff --git a/actiontext/lib/templates/fixtures.yml b/actiontext/lib/rails/generators/test_unit/templates/fixtures.yml similarity index 100% rename from actiontext/lib/templates/fixtures.yml rename to actiontext/lib/rails/generators/test_unit/templates/fixtures.yml diff --git a/actiontext/lib/tasks/actiontext.rake b/actiontext/lib/tasks/actiontext.rake deleted file mode 100644 index 4f90e4930c..0000000000 --- a/actiontext/lib/tasks/actiontext.rake +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -namespace :action_text do - # Prevent migration installation task from showing up twice. - Rake::Task["install:migrations"].clear_comments - - desc "Copy over the migration, stylesheet, and JavaScript files" - task install: %w( environment run_installer copy_migrations ) - - task :run_installer do - installer_template = File.expand_path("../templates/installer.rb", __dir__) - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{installer_template}" - end - - task :copy_migrations do - Rake::Task["active_storage:install:migrations"].invoke - Rake::Task["railties:install:migrations"].reenable # Otherwise you can't run 2 migration copy tasks in one invocation - Rake::Task["action_text:install:migrations"].invoke - end -end diff --git a/actiontext/lib/templates/installer.rb b/actiontext/lib/templates/installer.rb deleted file mode 100644 index a15ada92bb..0000000000 --- a/actiontext/lib/templates/installer.rb +++ /dev/null @@ -1,45 +0,0 @@ -require "pathname" -require "json" - -APPLICATION_PACK_PATH = Pathname.new("app/javascript/packs/application.js") -JS_PACKAGE_PATH = Pathname.new("#{__dir__}/../../package.json") - -JS_PACKAGE = JSON.load(JS_PACKAGE_PATH) -JS_DEPENDENCIES = JS_PACKAGE["peerDependencies"].dup.merge \ - JS_PACKAGE["name"] => "^#{JS_PACKAGE["version"]}" - -say "Copying actiontext.scss to app/assets/stylesheets" -copy_file "#{__dir__}/actiontext.scss", "app/assets/stylesheets/actiontext.scss" - -say "Copying fixtures to test/fixtures/action_text/rich_texts.yml" -copy_file "#{__dir__}/fixtures.yml", "test/fixtures/action_text/rich_texts.yml" - -say "Copying blob rendering partial to app/views/active_storage/blobs/_blob.html.erb" -copy_file "#{__dir__}/../../app/views/active_storage/blobs/_blob.html.erb", - "app/views/active_storage/blobs/_blob.html.erb" - -say "Installing JavaScript dependencies" -run "yarn add #{JS_DEPENDENCIES.map { |name, version| "#{name}@#{version}" }.join(" ")}" - -if APPLICATION_PACK_PATH.exist? - JS_DEPENDENCIES.keys.each do |name| - line = %[require("#{name}")] - unless APPLICATION_PACK_PATH.read.include? line - say "Adding #{name} to #{APPLICATION_PACK_PATH}" - append_to_file APPLICATION_PACK_PATH, "\n#{line}" - end - end -else - warn <<~WARNING - WARNING: Action Text can't locate your JavaScript bundle to add its package dependencies. - - Add these lines to any bundles: - - require("trix") - require("@rails/actiontext") - - Alternatively, install and setup the webpacker gem then rerun `bin/rails action_text:install` - to have these dependencies added automatically. - - WARNING -end From b1f6be8f49102ed7118baf76c02a11aea37fc1cb Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Wed, 9 Oct 2019 14:45:44 -0400 Subject: [PATCH 2/4] Create GEM_ROOT and make js_dependencies private --- .../action_text/install/install_generator.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/actiontext/lib/generators/action_text/install/install_generator.rb b/actiontext/lib/generators/action_text/install/install_generator.rb index 72328842cf..5768e2a956 100644 --- a/actiontext/lib/generators/action_text/install/install_generator.rb +++ b/actiontext/lib/generators/action_text/install/install_generator.rb @@ -9,7 +9,7 @@ module ActionText source_root File.expand_path("templates", __dir__) def install_javascript_dependencies - template "#{__dir__}/../../../../../railties/lib/rails/generators/rails/app/templates/bin/yarn.tt", + template "#{GEM_ROOT}/../railties/lib/rails/generators/rails/app/templates/bin/yarn.tt", "bin/yarn" say "Installing JavaScript dependencies" @@ -47,7 +47,7 @@ module ActionText def create_actiontext_files template "actiontext.scss", "app/assets/stylesheets/actiontext.scss" - copy_file "#{__dir__}/../../../../app/views/active_storage/blobs/_blob.html.erb", + copy_file "#{GEM_ROOT}/app/views/active_storage/blobs/_blob.html.erb", "app/views/active_storage/blobs/_blob.html.erb" end @@ -57,15 +57,19 @@ module ActionText run "rake action_text:install:migrations" end + hook_for :test_framework + + private + + GEM_ROOT = "#{__dir__}/../../../.." + def js_dependencies - package_contents = File.read(Pathname.new("#{__dir__}/../../../../package.json")) + package_contents = File.read(Pathname.new("#{GEM_ROOT}/package.json")) js_package = JSON.load(package_contents) js_package["peerDependencies"].dup.merge \ js_package["name"] => "^#{js_package["version"]}" end - - hook_for :test_framework end end end From 0bc9c16ffcbe3bdc2f971684b27844662fcedefc Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 10 Oct 2019 12:19:59 -0400 Subject: [PATCH 3/4] Fix rubocop offenses --- .../action_text/install/install_generator.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/actiontext/lib/generators/action_text/install/install_generator.rb b/actiontext/lib/generators/action_text/install/install_generator.rb index 5768e2a956..ad96639e25 100644 --- a/actiontext/lib/generators/action_text/install/install_generator.rb +++ b/actiontext/lib/generators/action_text/install/install_generator.rb @@ -60,16 +60,15 @@ module ActionText hook_for :test_framework private + GEM_ROOT = "#{__dir__}/../../../.." - GEM_ROOT = "#{__dir__}/../../../.." + def js_dependencies + package_contents = File.read(Pathname.new("#{GEM_ROOT}/package.json")) + js_package = JSON.load(package_contents) - def js_dependencies - package_contents = File.read(Pathname.new("#{GEM_ROOT}/package.json")) - js_package = JSON.load(package_contents) - - js_package["peerDependencies"].dup.merge \ - js_package["name"] => "^#{js_package["version"]}" - end + js_package["peerDependencies"].dup.merge \ + js_package["name"] => "^#{js_package["version"]}" + end end end end From 028d1611cdbc36b2650f80661cad6c79bb104051 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Mon, 28 Oct 2019 13:31:44 -0400 Subject: [PATCH 4/4] Run app:update:bin from actiontext generator --- .../lib/generators/action_text/install/install_generator.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/actiontext/lib/generators/action_text/install/install_generator.rb b/actiontext/lib/generators/action_text/install/install_generator.rb index ad96639e25..e1f5679647 100644 --- a/actiontext/lib/generators/action_text/install/install_generator.rb +++ b/actiontext/lib/generators/action_text/install/install_generator.rb @@ -9,8 +9,7 @@ module ActionText source_root File.expand_path("templates", __dir__) def install_javascript_dependencies - template "#{GEM_ROOT}/../railties/lib/rails/generators/rails/app/templates/bin/yarn.tt", - "bin/yarn" + run "rake app:update:bin" say "Installing JavaScript dependencies" run "yarn add #{js_dependencies.map { |name, version| "#{name}@#{version}" }.join(" ")}",