Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to choose a custom routes' mount point during install #5533

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Allow to choose a custom routes' mount point during install
Since we moved the frontend as an injectable templating system,
there are some manual steps to do to make the application work
when a user wants to change the mount point of where Solidus is
installed.

With this change, along with solidusio/solidus_starter_frontend#359,
we allow the user to select the mount point during the install, and
all the changes required are handled by the install script.
  • Loading branch information
kennyadsl authored and elia committed Dec 5, 2023
commit f4f3950e9f9b17dddb676307047b3422be7f238c
12 changes: 10 additions & 2 deletions admin/lib/generators/solidus_admin/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InstallGenerator < Rails::Generators::Base

def install_solidus_core_support
route <<~RUBY
mount SolidusAdmin::Engine, at: '/admin', constraints: ->(req) {
mount SolidusAdmin::Engine, at: '#{solidus_mount_point}admin', constraints: ->(req) {
req.cookies['solidus_admin'] != 'false' &&
req.params['solidus_admin'] != 'false'
}
Expand Down Expand Up @@ -37,7 +37,15 @@ def install_lookbook
gem "actioncable"
end

route "mount Lookbook::Engine, at: '/lookbook' if Rails.env.development?"
route "mount Lookbook::Engine, at: '#{solidus_mount_point}lookbook' if Rails.env.development?"
end

private

def solidus_mount_point
mount_point = Spree::Core::Engine.routes.find_script_name({})
mount_point += "/" unless mount_point.end_with?("/")
mount_point
end
end
end
Expand Down
67 changes: 52 additions & 15 deletions core/lib/generators/solidus/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class InstallGenerator < Rails::Generators::AppBase
class_option :user_class, type: :string
class_option :admin_email, type: :string
class_option :admin_password, type: :string
class_option :mount_point, type: :string, desc: "Indicates where Solidus should be mounted. Defaults to '/'"

class_option :frontend, type: :string, enum: FRONTENDS.map { _1[:name] }, default: nil, desc: "Indicates which frontend to install."
class_option :authentication, type: :string, enum: AUTHENTICATIONS.map { _1[:name] }, default: nil, desc: "Indicates which authentication system to install."
Expand Down Expand Up @@ -87,6 +88,36 @@ def prepare_options
ENV['SOLIDUS_SKIP_MIGRATIONS_CHECK'] = 'true'
end

def install_routes
if Pathname(app_path).join('config', 'routes.rb').read.include? CORE_MOUNT_ROUTE
say_status :route_exist, CORE_MOUNT_ROUTE, :blue
else
say_status :installing, "solidus routes"
mount_point = options[:mount_point] || ask_with_default(
desc: 'Where would you like to mount Solidus? (E.g. "/store" or "/shop")',
default: '/',
)

shell.mute do
route <<~RUBY
# This line mounts Solidus's routes at the root of your application.
#
# Unless you manually picked only a subset of Solidus components, this will mount routes for:
# - solidus_backend
# - solidus_api
# This means, any requests to URLs such as /admin/products, will go to Spree::Admin::ProductsController.
#
# If you are using the Starter Frontend as your frontend, be aware that all the storefront routes are defined
# separately in this file and are not part of the Solidus::Core::Engine engine.
#
# If you would like to change where this engine is mounted, simply change the :at option to something different.
# We ask that you don't use the :as option here, as Solidus relies on it being the default of "spree"
#{CORE_MOUNT_ROUTE}, at: '#{mount_point}'
RUBY
end
end
end

def add_files
template 'config/initializers/spree.rb.tt', 'config/initializers/spree.rb'
end
Expand Down Expand Up @@ -134,21 +165,6 @@ def create_database
rake 'db:create'
end

def install_routes
if Pathname(app_path).join('config', 'routes.rb').read.include? CORE_MOUNT_ROUTE
say_status :route_exist, CORE_MOUNT_ROUTE, :blue
else
route <<~RUBY
# This line mounts Solidus's routes at the root of your application.
# This means, any requests to URLs such as /products, will go to Spree::ProductsController.
# If you would like to change where this engine is mounted, simply change the :at option to something different.
#
# We ask that you don't use the :as option here, as Solidus relies on it being the default of "spree"
#{CORE_MOUNT_ROUTE}, at: '/'
RUBY
end
end

def run_migrations
if @run_migrations
say_status :running, "migrations"
Expand Down Expand Up @@ -209,6 +225,27 @@ def bundle_command(command, env = {})
Bundler.reset_paths!
end

def ask_with_default(desc:, default:)
if options[:auto_accept]
say_status :using, "#{default} (default)"
return default
end

default_label = " (#{set_color :default, :bold}: \"#{default}\")" if default

say_status :question, "#{desc}#{default_label}.", :yellow
answer = ask(set_color("answer:".rjust(13), :blue, :bold)).to_s.downcase.presence

case answer
when nil
say_status :using, "#{default} (default)"
default
else
say_status :using, answer
answer
end
end

def ask_with_description(desc:, limited_to:, default:)
loop do
say_status :question, desc, :yellow
Expand Down
Loading