Merge pull request #35085 from vinistock/check_testing_framework_in_action_text_installer

Check testing framework in action text installer
This commit is contained in:
Kasper Timm Hansen 2019-11-17 22:01:33 +01:00 committed by GitHub
commit fe094cb475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 65 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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