mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Create ActionText install generator
This commit is contained in:
parent
dcaa388bad
commit
0875c94b09
6 changed files with 84 additions and 65 deletions
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in a new issue