Skip to content

Commit

Permalink
Merge pull request #4087 from nebulab/waiting-for-dev/update_task
Browse files Browse the repository at this point in the history
Introduce Solidus update process
  • Loading branch information
kennyadsl authored Aug 9, 2021
2 parents 64ec398 + c9a4e61 commit 5cd7e9c
Show file tree
Hide file tree
Showing 17 changed files with 479 additions and 27 deletions.
4 changes: 4 additions & 0 deletions api/lib/spree/api/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class Engine < Rails::Engine
# Leave initializer empty for backwards-compatibility. Other apps
# might still rely on this event.
initializer "spree.api.environment", before: :load_config_initializers do; end

config.after_initialize do
Spree::Api::Config.check_load_defaults_called('Spree::Api::Config')
end
end
end
end
4 changes: 4 additions & 0 deletions backend/lib/spree/backend/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Engine < ::Rails::Engine
# Leave initializer empty for backwards-compatability. Other apps
# might still rely on this event.
initializer "spree.backend.environment", before: :load_config_initializers do; end

config.after_initialize do
Spree::Backend::Config.check_load_defaults_called('Spree::Backend::Config')
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Configure Solidus Preferences
# See http://docs.solidus.io/Spree/AppConfiguration.html for details

# Solidus version defaults for preferences that are not overridden
Spree.load_defaults '<%= Spree.solidus_version %>'

Spree.config do |config|
# Core:

# Solidus version defaults for preferences that are not overridden
config.load_defaults '<%= Spree.solidus_version %>'

# Default currency for new sites
config.currency = "USD"

Expand Down Expand Up @@ -65,14 +64,12 @@ end

<% if defined?(Spree::Frontend::Engine) -%>
Spree::Frontend::Config.configure do |config|
config.load_defaults '<%= Spree.solidus_version %>'
config.locale = 'en'
end
<% end -%>

<% if defined?(Spree::Backend::Engine) -%>
Spree::Backend::Config.configure do |config|
config.load_defaults '<%= Spree.solidus_version %>'
config.locale = 'en'

# Uncomment and change the following configuration if you want to add
Expand All @@ -88,7 +85,6 @@ end

<% if defined?(Spree::Api::Engine) -%>
Spree::Api::Config.configure do |config|
config.load_defaults '<%= Spree.solidus_version %>'
config.requires_authentication = true
end
<% end -%>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This initializer lets you preview the defaults that have changed on the new
# Solidus version.
#
# It allows you to enable them one by one while you adapt your application.
# When you're done with all of them, you can safely remove this file and add
# the updated `load_defaults` calls to the top of the config blocks in your
# Solidus main initializer. You can also call `Spree.load_defaults(version)` to
# target all components at once.

Spree.config do |config|
<%= @core_changes %>
end

<% if defined?(Spree::Frontend::Engine) -%>
Spree::Frontend::Config.configure do |config|
<%= @frontend_changes %>
end
<% end -%>

<% if defined?(Spree::Backend::Engine) -%>
Spree::Backend::Config.configure do |config|
<%= @backend_changes %>
end
<% end -%>

<% if defined?(Spree::Api::Engine) -%>
Spree::Api::Config.configure do |config|
<%= @api_changes %>
end
<% end -%>
96 changes: 96 additions & 0 deletions core/lib/generators/solidus/update/update_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: true

require 'spree/preferences/preference_differentiator'
require 'rails/generators'

module Solidus
# @private
class UpdateGenerator < ::Rails::Generators::Base
FROM = Spree.previous_solidus_minor_version

desc 'Generates a new initializer to preview the new defaults for current Solidus version'

source_root File.expand_path('templates', __dir__)

class_option :initializer_basename,
type: :string,
default: 'new_solidus_defaults',
banner: 'The name for the new initializer'

class_option :previous_version_prompt,
type: :boolean,
default: true,
banner: 'Prompt to warn about only previous version support'

class_option :from,
type: :string,
default: FROM,
banner: 'Solidus version from which you are upgrading'

class_option :to,
type: :string,
default: Spree.solidus_version,
hide: true

class_option :initializer_directory,
type: :string,
default: 'config/initializers/',
hide: true

def create_new_defaults_initializer
previous_version_prompt = options[:previous_version_prompt]
return if previous_version_prompt && !yes?(<<~MSG, :red)
The update process is only supported if you are coming from version #{FROM}. If this is not the case, please, skip it and update your application to use Solidus #{FROM} before retrying.
If you are confident you want to upgrade from a previous version, you must rerun the generator with the "--from={OLD_VERSION}" argument.
Are you sure you want to continue? (y/N)
MSG

from = options[:from]
to = options[:to]
@from = from
@core_changes = core_changes_template(from, to)
@frontend_changes = frontend_changes_template(from, to)
@backend_changes = backend_changes_template(from, to)
@api_changes = api_changes_template(from, to)

template 'config/initializers/new_solidus_defaults.rb.tt',
File.join(options[:initializer_directory], "#{options[:initializer_basename]}.rb")
end

private

def core_changes_template(from, to)
changes_template_for(Spree::AppConfiguration, from, to)
end

def frontend_changes_template(from, to)
return '' unless defined?(Spree::Frontend::Engine)

changes_template_for(Spree::FrontendConfiguration, from, to)
end

def backend_changes_template(from, to)
return '' unless defined?(Spree::Backend::Engine)

changes_template_for(Spree::BackendConfiguration, from, to)
end

def api_changes_template(from, to)
return '' unless defined?(Spree::Api::Engine)

changes_template_for(Spree::ApiConfiguration, from, to)
end

def changes_template_for(klass, from, to)
changes = Spree::Preferences::PreferenceDifferentiator.new(klass).call(from: from, to: to)
return '# No changes' if changes.empty?

[
["config.load_defaults('#{from}')"] +
changes.map do |pref_key, change|
" # config.#{pref_key} = #{change[:to]}"
end.flatten
].join("\n")
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ Bundler.require(*Rails.groups(assets: %w(development test)))
require '<%= lib_name %>'

<%= application_definition %>

17 changes: 17 additions & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def self.user_class
end
end

# Load the same version defaults for all available Solidus components
#
# @see Spree::Preferences::Configuration#load_defaults
def self.load_defaults(version)
Spree::Config.load_defaults(version)
Spree::Frontend::Config.load_defaults(version) if defined?(Spree::Frontend::Config)
Spree::Backend::Config.load_defaults(version) if defined?(Spree::Backend::Config)
Spree::Api::Config.load_defaults(version) if defined?(Spree::Api::Config)
end

# Used to configure Spree.
#
# Example:
Expand All @@ -52,6 +62,13 @@ def self.config(&_block)
end

module Core
def self.has_install_generator_been_run?
(Rails.env.test? && Rails.application.class.name == 'DummyApp::Application') ||
Rails.application.paths['config/initializers'].paths.any? do |path|
File.exist?(path.join('spree.rb'))
end
end

class GatewayError < RuntimeError; end
end
end
Expand Down
4 changes: 4 additions & 0 deletions core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class Engine < ::Rails::Engine
app.config.action_mailer.preview_path = "{#{original_preview_path},#{solidus_preview_path}}"
ActionMailer::Base.preview_path = app.config.action_mailer.preview_path
end

config.after_initialize do
Spree::Config.check_load_defaults_called('Spree::Config')
end
end
end
end
4 changes: 4 additions & 0 deletions core/lib/spree/core/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def self.solidus_version
VERSION
end

def self.previous_solidus_minor_version
'3.0'
end

def self.solidus_gem_version
Gem::Version.new(solidus_version)
end
Expand Down
18 changes: 18 additions & 0 deletions core/lib/spree/preferences/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,36 @@ class Configuration
# defaults. Set via {#load_defaults}
attr_reader :loaded_defaults

attr_reader :load_defaults_called

def initialize
@loaded_defaults = Spree.solidus_version
@load_defaults_called = false
end

# @param [String] Solidus version from which take defaults when not
# overriden.
# @see #load_defaults
def load_defaults(version)
@loaded_defaults = version
@load_defaults_called = true
reset
end

def check_load_defaults_called(instance_constant_name = nil)
return if load_defaults_called || !Spree::Core.has_install_generator_been_run?

target_name = instance_constant_name || "#{self.class.name}.new"
Spree::Deprecation.warn <<~MSG
It's recommended that you explicitly load the default configuration for
your current Solidus version. You can do it by adding the following call
to your Solidus initializer within the #{target_name} block:
config.load_defaults('#{Spree.solidus_version}')
MSG
end

# @yield [config] Yields this configuration object to a block
def configure
yield(self)
Expand Down
28 changes: 28 additions & 0 deletions core/lib/spree/preferences/preference_differentiator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Spree
module Preferences
class PreferenceDifferentiator
attr_reader :config_class

def initialize(config_class)
@config_class = config_class
end

def call(from:, to:)
preferences_from = config_class.new.load_defaults(from)
preferences_to = config_class.new.load_defaults(to)
preferences_from.reduce({}) do |changes, (pref_key, value_from)|
value_to = preferences_to[pref_key]
if value_from == value_to
changes
else
changes.merge(
pref_key => { from: value_from, to: value_to }
)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions core/lib/spree/testing_support/dummy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class Application < ::Rails::Application
end

Spree.user_class = 'Spree::LegacyUser'
Spree.load_defaults(Spree.solidus_version)
Spree.config do |config|
config.mails_from = "store@example.com"

Expand Down
Loading

0 comments on commit 5cd7e9c

Please sign in to comment.