From d2879a118d3e6cd8dd7fc7bb046d37e876c60a95 Mon Sep 17 00:00:00 2001 From: dbwinger Date: Wed, 9 Jun 2021 11:31:14 -0400 Subject: [PATCH] Backport #2115 to v5.0 --- lib/alchemy/upgrader/five_point_zero.rb | 31 +++++++++++++++++++++++++ lib/tasks/alchemy/upgrade.rake | 20 ++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/alchemy/upgrader/five_point_zero.rb b/lib/alchemy/upgrader/five_point_zero.rb index 338df576ea..a5abd211e9 100644 --- a/lib/alchemy/upgrader/five_point_zero.rb +++ b/lib/alchemy/upgrader/five_point_zero.rb @@ -1,9 +1,18 @@ # frozen_string_literal: true require_relative "tasks/harden_gutentag_migrations" +require "rails/generators" +require "thor" +require "alchemy/install/tasks" module Alchemy class Upgrader::FivePointZero < Upgrader + include Rails::Generators::Actions + include Thor::Base + include Thor::Actions + + source_root File.expand_path("../../generators/alchemy/install/files", __dir__) + class << self def install_gutentag_migrations desc "Install Gutentag migrations" @@ -36,6 +45,28 @@ def remove_root_page log "Root page not found.", :skip end end + + def run_webpacker_installer + # Webpacker does not create a package.json, but we need one + unless File.exist? app_root.join("package.json") + in_root { run "echo '{}' > package.json" } + end + new.rake("webpacker:install", abort_on_failure: true) + end + + def add_npm_package + new.run "yarn add @alchemy_cms/admin" + end + + def copy_alchemy_entry_point + webpack_config = YAML.load_file(app_root.join("config", "webpacker.yml"))[Rails.env] + new.copy_file "alchemy_admin.js", + app_root.join(webpack_config["source_path"], webpack_config["source_entry_path"], "alchemy/admin.js") + end + + def app_root + @_app_root ||= Rails.root + end end end end diff --git a/lib/tasks/alchemy/upgrade.rake b/lib/tasks/alchemy/upgrade.rake index 45c134dcc8..0c0831ddb8 100644 --- a/lib/tasks/alchemy/upgrade.rake +++ b/lib/tasks/alchemy/upgrade.rake @@ -42,6 +42,9 @@ namespace :alchemy do "alchemy:upgrade:5.0:install_gutentag_migrations", "alchemy:upgrade:5.0:remove_layout_roots", "alchemy:upgrade:5.0:remove_root_page", + "alchemy:upgrade:5.0:run_webpacker_installer", + "alchemy:upgrade:5.0:add_npm_package", + "alchemy:upgrade:5.0:copy_alchemy_entry_point", ] desc "Install Gutentag migrations" @@ -58,6 +61,23 @@ namespace :alchemy do task remove_root_page: [:environment] do Alchemy::Upgrader::FivePointZero.remove_root_page end + + desc "Run webpacker installer" + task run_webpacker_installer: [:environment] do + Alchemy::Upgrader::FivePointZero.run_webpacker_installer + end + + desc "Add NPM package" + task add_npm_package: [:environment] do + puts "adding npm_package..." + Alchemy::Upgrader::FivePointZero.add_npm_package + end + + desc "Copy alchemy entry point" + task copy_alchemy_entry_point: [:environment] do + puts "copying alchemy entry point" + Alchemy::Upgrader::FivePointZero.copy_alchemy_entry_point + end end end end