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..e1f5679647 --- /dev/null +++ b/actiontext/lib/generators/action_text/install/install_generator.rb @@ -0,0 +1,73 @@ +# 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 + run "rake app:update:bin" + + 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 "#{GEM_ROOT}/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 + + hook_for :test_framework + + private + GEM_ROOT = "#{__dir__}/../../../.." + + 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 + 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