Sha256: 61c98eb869da23ae9c4ab1d649313f51652785f11a43cc3409a341319d52c164

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Solidus
  class InstallGenerator < Rails::Generators::AppBase
    class InstallFrontend
      attr_reader :bundler_context,
                  :generator_context

      def initialize(bundler_context:, generator_context:)
        @bundler_context = bundler_context
        @generator_context = generator_context
      end

      def call(frontend)
        case frontend
        when 'solidus_frontend'
          install_solidus_frontend
        when 'solidus_starter_frontend'
          install_solidus_starter_frontend
        else
          install_custom_frontend
        end
      end

      private

      def install_solidus_frontend
        unless @bundler_context.component_in_gemfile?(:frontend)
          # `Rails::Generator::AppBase#bundle_command` is protected so have to `send` it.
          # See https://api.rubyonrails.org/v3.2.16/classes/Rails/Generators/AppBase.html#method-i-run_bundle
          @generator_context.send :bundle_command, 'add solidus_frontend'
        end

        # Solidus bolt will be handled in the installer as a payment method.
        begin
          skip_solidus_bolt = ENV['SKIP_SOLIDUS_BOLT']
          ENV['SKIP_SOLIDUS_BOLT'] = 'true'
          @generator_context.generate("solidus_frontend:install #{@generator_context.options[:auto_accept] ? '--auto-accept' : ''}")
        ensure
          ENV['SKIP_SOLIDUS_BOLT'] = skip_solidus_bolt
        end
      end

      def install_solidus_starter_frontend
        remove_solidus_frontend
        @generator_context.apply "https://raw.githubusercontent.com/solidusio/solidus_starter_frontend/v3.2/template.rb"
      end

      def install_custom_frontend
        remove_solidus_frontend
        @generator_context.apply ENV['FRONTEND'] or abort("Frontend installation failed.")
      end

      def remove_solidus_frontend
        return unless @bundler_context.component_in_gemfile?(:frontend)

        @bundler_context.remove(['solidus_frontend'])
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_core-3.2.9 lib/generators/solidus/install/install_generator/install_frontend.rb
solidus_core-3.2.8 lib/generators/solidus/install/install_generator/install_frontend.rb
solidus_core-3.2.7 lib/generators/solidus/install/install_generator/install_frontend.rb
solidus_core-3.2.6 lib/generators/solidus/install/install_generator/install_frontend.rb
solidus_core-3.2.5 lib/generators/solidus/install/install_generator/install_frontend.rb