Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Spree 5 sandbox script (#12452)

Damian Legawiec346a8eca6059
diff --git a/admin/lib/generators/install/install_generator.rb b/admin/lib/generators/install/install_generator.rb
deleted file mode 100644
index ee1bca6228..0000000000
--- a/admin/lib/generators/install/install_generator.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module Spree
- module Admin
- module Generators
- class InstallGenerator < Rails::Generators::Base
- desc 'Installs Spree Admin Dashboard'
-
- def self.source_paths
- [
- File.expand_path('templates', __dir__),
- File.expand_path('../templates', "../#{__FILE__}"),
- File.expand_path('../templates', "../../#{__FILE__}")
- ]
- end
-
- def install
- template 'app/assets/stylesheets/spree/admin/applications.scss'
- end
- end
- end
- end
-end
diff --git a/admin/lib/generators/spree/admin/install/install_generator.rb b/admin/lib/generators/spree/admin/install/install_generator.rb
new file mode 100644
index 0000000000..7ab76cddb7
--- /dev/null
+++ b/admin/lib/generators/spree/admin/install/install_generator.rb
@@ -0,0 +1,42 @@
+require 'rails/generators'
+
+module Spree
+ module Admin
+ module Generators
+ class InstallGenerator < Rails::Generators::Base
+ desc 'Installs Spree Admin Dashboard'
+
+ def self.source_paths
+ [
+ File.expand_path('templates', __dir__),
+ File.expand_path('../templates', "../#{__FILE__}"),
+ File.expand_path('../templates', "../../#{__FILE__}")
+ ]
+ end
+
+ def install
+ if Rails.root && Rails.root.join("Procfile.dev").exist?
+ append_to_file 'Procfile.dev', "\nadmin_css: bin/rails dartsass:watch" unless File.read('Procfile.dev').include?('admin_css:')
+ else
+ create_file 'Procfile.dev', "admin_css: bin/rails dartsass:watch\n"
+ end
+
+ say "Add bin/dev to start foreman"
+ copy_file "dev", "bin/dev", force: true
+ chmod "bin/dev", 0755, verbose: false
+
+ empty_directory Rails.root.join('app/assets/builds') if Rails.root
+
+ unless File.exist?('app/assets/config/manifest.js')
+ create_file 'app/assets/config/manifest.js', "//= link_tree ../builds\n"
+
+ say "Ensure foreman is installed"
+ run "gem install foreman"
+ else
+ append_to_file 'app/assets/config/manifest.js', "\n//= link_tree ../builds" unless File.read('app/assets/config/manifest.js').include?('//= link_tree ../builds')
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/admin/lib/generators/spree/admin/install/templates/dev b/admin/lib/generators/spree/admin/install/templates/dev
new file mode 100644
index 0000000000..26c6feb97e
--- /dev/null
+++ b/admin/lib/generators/spree/admin/install/templates/dev
@@ -0,0 +1,16 @@
+#!/usr/bin/env sh
+
+if ! gem list foreman -i --silent; then
+ echo "Installing foreman..."
+ gem install foreman
+fi
+
+# Default to port 3000 if not specified
+export PORT="${PORT:-3000}"
+
+# Let the debug gem allow remote connections,
+# but avoid loading until `debugger` is called
+export RUBY_DEBUG_OPEN="true"
+export RUBY_DEBUG_LAZY="true"
+
+exec foreman start -f Procfile.dev "$@"
\ No newline at end of file
diff --git a/bin/sandbox.sh b/bin/sandbox.sh
index 1b8714876b..8d4d2bbb61 100755
--- a/bin/sandbox.sh
+++ b/bin/sandbox.sh
@@ -35,6 +35,7 @@ fi
cd ./sandbox
cat <<RUBY >> Gemfile
+gem 'devise'
gem 'spree', path: '..'
gem 'spree_emails', path: '../emails'
gem 'spree_sample', path: '../sample'
@@ -46,6 +47,7 @@ group :test, :development do
gem 'bullet'
gem 'pry-byebug'
gem 'awesome_print'
+ gem 'letter_opener'
end
RUBY
@@ -68,8 +70,24 @@ bin/rails stimulus:install
bin/rails db:drop || true
bin/rails db:create
-bin/rails g spree:install --auto-accept --user_class=Spree::User --sample=true
-bin/rails g spree:emails:install
-bin/rails g spree:admin:install
-bin/rails g spree:storefront:install
-bin/rake acts_as_taggable_on_engine:install:migrations
+
+# setup devise
+bin/rails g devise:install
+bin/rails g devise Spree::User
+
+# setup spree
+bin/rails g spree:install --auto-accept --user_class=Spree::User --authentication=devise --install_storefront=true --install_admin=true
+
+# setup letter_opener
+cat <<RUBY >> config/environments/development.rb
+Rails.application.config.action_mailer.delivery_method = :letter_opener
+Rails.application.config.action_mailer.perform_deliveries = true
+RUBY
+
+# add web to Procfile.dev
+echo "web: bin/rails s -p 3000" >> Procfile.dev
+
+# add root to config/routes.rb
+sed -i '' -e '$i\
+ root "spree/home#index"\
+' config/routes.rb
diff --git a/core/lib/generators/spree/authentication/devise/devise_generator.rb b/core/lib/generators/spree/authentication/devise/devise_generator.rb
new file mode 100644
index 0000000000..34202b0aaa
--- /dev/null
+++ b/core/lib/generators/spree/authentication/devise/devise_generator.rb
@@ -0,0 +1,57 @@
+require 'rails/generators'
+
+module Spree
+ module Authentication
+ class DeviseGenerator < Rails::Generators::Base
+ desc 'Set up a Spree installation with Devise as authentication'
+
+ def self.source_paths
+ paths = superclass.source_paths
+ paths << File.expand_path('templates', __dir__)
+ paths.flatten
+ end
+
+ def generate
+ template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'
+
+ file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
+ send(file_action, 'config/initializers/spree.rb') do
+ %Q{
+ Rails.application.config.to_prepare do
+ require_dependency 'spree/authentication_helpers'
+ end\n}
+ end
+
+ user_class_file = Rails.root.join('app', 'models', "#{Spree.user_class.name.underscore}.rb")
+
+ if File.exist?(user_class_file)
+ inject_into_file user_class_file, after: "class #{Spree.user_class.name} < ApplicationRecord\n" do
+ <<-RUBY
+ # Spree modules
+ include Spree::UserAddress
+ include Spree::UserMethods
+ include Spree::UserPaymentSource
+ RUBY
+ end
+ say "Successfully added Spree user modules into #{user_class_file}"
+ else
+ say "Could not locate user model file at #{user_class_file}. Please add these lines manually:", :red
+ say <<~RUBY
+ # Spree modules
+ include Spree::UserAddress
+ include Spree::UserMethods
+ include Spree::UserPaymentSource
+ RUBY
+ end
+
+ append_file 'config/initializers/spree.rb' do
+ %Q{
+ if defined?(Devise) && Devise.respond_to?(:parent_controller)
+ Devise.parent_controller = "Spree::StoreController"
+ Devise.parent_mailer = "Spree::BaseMailer"
+ end\n}
+ end
+ end
+ end
+ end
+end
diff --git a/core/lib/generators/spree/custom_authentication/templates/authentication_helpers.rb.tt b/core/lib/generators/spree/authentication/devise/templates/authentication_helpers.rb.tt
similarity index 57%
rename from core/lib/generators/spree/custom_authentication/templates/authentication_helpers.rb.tt
rename to core/lib/generators/spree/authentication/devise/templates/authentication_helpers.rb.tt
index a67e85ebfd..e5f8c7e49c 100644
--- a/core/lib/generators/spree/custom_authentication/templates/authentication_helpers.rb.tt
+++ b/core/lib/generators/spree/authentication/devise/templates/authentication_helpers.rb.tt
@@ -9,42 +9,42 @@ module Spree
:spree_forgot_password_path,
:spree_edit_password_path,
:spree_admin_login_path,
- :spree_admin_logout_path,
+ :spree_admin_logout_path
)
end
def spree_current_user
- current_user
+ send("current_#{Spree.user_class.model_name.singular_route_key}")
end
def spree_login_path(opts = {})
- main_app.login_path(opts)
+ new_session_path(Spree.user_class.model_name.singular_route_key, opts)
end
def spree_signup_path(opts = {})
- main_app.signup_path(opts)
+ new_registration_path(Spree.user_class.model_name.singular_route_key, opts)
end
def spree_logout_path(opts = {})
- main_app.logout_path(opts)
+ destroy_session_path(Spree.user_class.model_name.singular_route_key, opts)
end
def spree_forgot_password_path(opts = {})
- main_app.forgot_password_path(opts)
+ new_password_path(Spree.user_class.model_name.singular_route_key, opts)
end
def spree_edit_password_path(opts = {})
- main_app.edit_password_path(opts)
+ edit_registration_path(Spree.user_class.model_name.singular_route_key, opts)
end
def spree_admin_login_path(opts = {})
- main_app.admin_login_path(opts)
+ spree_login_path(opts)
end
def spree_admin_logout_path(opts = {})
- main_app.admin_logout_path(opts)
+ spree_logout_path(opts)
end
end
end
-ApplicationController.include Spree::AuthenticationHelpers if defined?(ApplicationController)
+ApplicationController.include Spree::AuthenticationHelpers if defined?(ApplicationController)
\ No newline at end of file
diff --git a/core/lib/generators/spree/custom_authentication/custom_authentication_generator.rb b/core/lib/generators/spree/custom_authentication/custom_authentication_generator.rb
deleted file mode 100644
index 3d2eb72f03..0000000000
--- a/core/lib/generators/spree/custom_authentication/custom_authentication_generator.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-module Spree
- class CustomAuthenticationGenerator < Rails::Generators::Base
- desc 'Set up a Spree installation with a custom authentication helpers'
-
- def self.source_paths
- paths = superclass.source_paths
- paths << File.expand_path('templates', __dir__)
- paths.flatten
- end
-
- def generate
- template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'
-
- file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
- send(file_action, 'config/initializers/spree.rb') do
- %Q{
- Rails.application.config.to_prepare do
- require_dependency 'spree/authentication_helpers'
- end\n}
- end
- end
- end
-end
diff --git a/core/lib/generators/spree/custom_user/custom_user_generator.rb b/core/lib/generators/spree/custom_user/custom_user_generator.rb
deleted file mode 100644
index 402bbc6f35..0000000000
--- a/core/lib/generators/spree/custom_user/custom_user_generator.rb
+++ /dev/null
@@ -1,46 +0,0 @@
-module Spree
- class CustomUserGenerator < Rails::Generators::NamedBase
- include Rails::Generators::ResourceHelpers
- include Rails::Generators::Migration
-
- desc 'Set up a Spree installation with a custom User class'
-
- def self.source_paths
- paths = superclass.source_paths
- paths << File.expand_path('templates', __dir__)
- paths.flatten
- end
-
- def check_for_constant
- klass
- rescue NameError
- @shell.say "Couldn't find #{class_name}. Are you sure that this class exists within your application and is loaded?", :red
- exit(1)
- end
-
- def generate
- migration_template 'migration.rb.tt', 'db/migrate/add_spree_fields_to_custom_user_table.rb'
- template 'current_user_helpers.rb.tt', 'lib/spree/current_user_helpers.rb'
-
- file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
- send(file_action, 'config/initializers/spree.rb') do
- %Q{
- Rails.application.config.to_prepare do
- require_dependency 'spree/current_user_helpers'
- end\n}
- end
- end
-
- def self.next_migration_number(dirname)
- format('%.3d', (current_migration_number(dirname) + 1))
- end
-
- def klass
- class_name.constantize
- end
-
- def table_name
- klass.table_name
- end
- end
-end
diff --git a/core/lib/generators/spree/custom_user/templates/current_user_helpers.rb.tt b/core/lib/generators/spree/custom_user/templates/current_user_helpers.rb.tt
deleted file mode 100644
index b3fca885b7..0000000000
--- a/core/lib/generators/spree/custom_user/templates/current_user_helpers.rb.tt
+++ /dev/null
@@ -1,15 +0,0 @@
-module Spree
- module CurrentUserHelpers
- def self.included(receiver)
- receiver.send :helper_method, :spree_current_user
- end
-
- def spree_current_user
- current_user
- end
- end
-end
-
-
-ApplicationController.include Spree::CurrentUserHelpers if defined?(ApplicationController)
-Spree::Api::BaseController.include Spree::CurrentUserHelpers if defined?(Spree::Api::BaseController)
diff --git a/core/lib/generators/spree/custom_user/templates/initializer.rb.tt b/core/lib/generators/spree/custom_user/templates/initializer.rb.tt
deleted file mode 100644
index b0b4643868..0000000000
--- a/core/lib/generators/spree/custom_user/templates/initializer.rb.tt
+++ /dev/null
@@ -1 +0,0 @@
-Spree.user_class = "<%= class_name %>"
diff --git a/core/lib/generators/spree/custom_user/templates/migration.rb.tt b/core/lib/generators/spree/custom_user/templates/migration.rb.tt
deleted file mode 100644
index b94abe38ee..0000000000
--- a/core/lib/generators/spree/custom_user/templates/migration.rb.tt
+++ /dev/null
@@ -1,7 +0,0 @@
-class AddSpreeFieldsToCustomUserTable < ActiveRecord::Migration[4.2]
- def up
- add_column <%= table_name.inspect %>, :spree_api_key, :string, limit: 48
- add_column <%= table_name.inspect %>, :ship_address_id, :bigint
- add_column <%= table_name.inspect %>, :bill_address_id, :bigint
- end
-end
diff --git a/core/lib/generators/spree/install/install_generator.rb b/core/lib/generators/spree/install/install_generator.rb
index c255fdfc93..d8a789492d 100644
--- a/core/lib/generators/spree/install/install_generator.rb
+++ b/core/lib/generators/spree/install/install_generator.rb
@@ -19,6 +19,7 @@ module Spree
class_option :admin_password, type: :string
class_option :lib_name, type: :string, default: 'spree'
class_option :enforce_available_locales, type: :boolean, default: nil
+ class_option :authentication, type: :string, default: 'devise'
def self.source_paths
paths = superclass.source_paths
@@ -35,6 +36,7 @@ module Spree
@install_storefront = options[:install_storefront]
@install_admin = options[:install_admin]
@copy_storefront = options[:copy_storefront]
+ @authentication = options[:authentication]
unless @run_migrations
@load_seed_data = false
@@ -64,8 +66,13 @@ module Spree
ROBOTS
end
- def create_overrides_directory
- empty_directory 'app/overrides'
+ # Currently we only support devise, in the future we will also add support for default Rails authentication
+ def install_authentication
+ if @authentication == 'devise'
+ generate 'spree:authentication:devise'
+ elsif @authentication == 'dummy'
+ # this is for dummy / test app
+ end
end
def install_storefront
@@ -117,6 +124,7 @@ module Spree
say_status :copying, 'migrations'
silence_stream(STDOUT) do
silence_warnings { rake 'railties:install:migrations' }
+ silence_warnings { rake 'acts_as_taggable_on_engine:install:migrations' }
end
end
diff --git a/core/lib/spree/testing_support/common_rake.rb b/core/lib/spree/testing_support/common_rake.rb
index c2b6a18686..785e3d8f05 100644
--- a/core/lib/spree/testing_support/common_rake.rb
+++ b/core/lib/spree/testing_support/common_rake.rb
@@ -8,7 +8,12 @@ require 'generators/spree/dummy_model/dummy_model_generator'
desc 'Generates a dummy app for testing'
namespace :common do
task :test_app, :user_class do |_t, args|
- args.with_defaults(user_class: 'Spree::LegacyUser', install_storefront: 'false', install_admin: 'false')
+ args.with_defaults(
+ user_class: 'Spree::LegacyUser',
+ install_storefront: 'false',
+ install_admin: 'false',
+ authentication: 'dummy'
+ )
require ENV['LIB_NAME'].to_s
ENV['RAILS_ENV'] = 'test'
@@ -39,7 +44,8 @@ namespace :common do
'--copy_storefront=false',
"--install_storefront=#{args[:install_storefront]}",
"--install_admin=#{args[:install_admin]}",
- "--user_class=#{args[:user_class]}"
+ "--user_class=#{args[:user_class]}",
+ "--authentication=#{args[:authentication]}"
]
puts 'Setting up dummy database...'
diff --git a/storefront/app/views/themes/default/spree/user_passwords/edit.html.erb b/storefront/app/views/devise/passwords/edit.html.erb
similarity index 96%
rename from storefront/app/views/themes/default/spree/user_passwords/edit.html.erb
rename to storefront/app/views/devise/passwords/edit.html.erb
index 36a501b922..b2235fbd9a 100644
--- a/storefront/app/views/themes/default/spree/user_passwords/edit.html.erb
+++ b/storefront/app/views/devise/passwords/edit.html.erb
@@ -18,5 +18,5 @@
<%= f.submit Spree.t(:reset_password), class: 'btn btn-primary' %>
</div>
<% end %>
- <%= render "spree/shared/devise/links" %>
+ <%= render "devise/shared/links" %>
</div>
diff --git a/storefront/app/views/themes/default/spree/user_passwords/new.html.erb b/storefront/app/views/devise/passwords/new.html.erb
similarity index 94%
rename from storefront/app/views/themes/default/spree/user_passwords/new.html.erb
rename to storefront/app/views/devise/passwords/new.html.erb
index d04678adc8..67c416b1c5 100644
--- a/storefront/app/views/themes/default/spree/user_passwords/new.html.erb
+++ b/storefront/app/views/devise/passwords/new.html.erb
@@ -11,6 +11,6 @@
<%= f.submit Spree.t(:reset_password), class: 'btn btn-primary w-full' %>
</div>
<% end %>
- <%= render "spree/shared/devise/links" %>
+ <%= render "devise/shared/links" %>
</div>
<% end %>
diff --git a/storefront/app/views/themes/default/spree/user_registrations/_form.html.erb b/storefront/app/views/devise/registrations/_form.html.erb
similarity index 100%
rename from storefront/app/views/themes/default/spree/user_registrations/_form.html.erb
rename to storefront/app/views/devise/registrations/_form.html.erb
diff --git a/storefront/app/views/themes/default/spree/user_registrations/edit.html.erb b/storefront/app/views/devise/registrations/edit.html.erb
similarity index 100%
rename from storefront/app/views/themes/default/spree/user_registrations/edit.html.erb
rename to storefront/app/views/devise/registrations/edit.html.erb
diff --git a/storefront/app/views/themes/default/spree/user_registrations/new.html.erb b/storefront/app/views/devise/registrations/new.html.erb
similarity index 96%
rename from storefront/app/views/themes/default/spree/user_registrations/new.html.erb
rename to storefront/app/views/devise/registrations/new.html.erb
index 5f1db0703d..2d4258cba4 100755
--- a/storefront/app/views/themes/default/spree/user_registrations/new.html.erb
+++ b/storefront/app/views/devise/registrations/new.html.erb
@@ -22,6 +22,6 @@
<%= f.submit Spree.t(:sign_up), class: 'btn btn-primary w-full' %>
</div>
<% end %>
- <%= render "spree/shared/devise/links" %>
+ <%= render "devise/shared/links" %>
</div>
<% end%>
diff --git a/storefront/app/views/themes/default/spree/user_sessions/new.html.erb b/storefront/app/views/devise/sessions/new.html.erb
similarity index 86%
rename from storefront/app/views/themes/default/spree/user_sessions/new.html.erb
rename to storefront/app/views/devise/sessions/new.html.erb
index b4a17b716b..2249cb5a91 100644
--- a/storefront/app/views/themes/default/spree/user_sessions/new.html.erb
+++ b/storefront/app/views/devise/sessions/new.html.erb
@@ -1,7 +1,7 @@
<%= turbo_frame_tag :login do %>
<div class="card-dialog max-w-xl mx-auto py-10 px-4">
<h2 class="text-xl lg:text-2xl font-medium mb-4"><%= Spree.t(:login) %></h2>
- <%= form_for(resource, as: resource_name, url: session_path(resource_name), data: { turbo: false }) do |f| %>
+ <%= form_for(resource, as: resource_name, url: spree_login_path, data: { turbo: false }) do |f| %>
<div class="flex flex-col mb-4">
<%= f.label :email, Spree.t(:email) %>
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'text-input', required: true %>
@@ -20,6 +20,6 @@
<%= f.submit Spree.t(:login), class: 'btn btn-primary w-full' %>
</div>
<% end %>
- <%= render "spree/shared/devise/links" %>
+ <%= render "devise/shared/links" %>
</div>
<% end %>
diff --git a/storefront/app/views/themes/default/spree/shared/devise/_links.html.erb b/storefront/app/views/devise/shared/_links.html.erb
similarity index 100%
rename from storefront/app/views/themes/default/spree/shared/devise/_links.html.erb
rename to storefront/app/views/devise/shared/_links.html.erb
diff --git a/storefront/lib/generators/spree/storefront/install/install_generator.rb b/storefront/lib/generators/spree/storefront/install/install_generator.rb
index c471555249..af60a43d0b 100644
--- a/storefront/lib/generators/spree/storefront/install/install_generator.rb
+++ b/storefront/lib/generators/spree/storefront/install/install_generator.rb
@@ -18,14 +18,23 @@ module Spree
template 'application.tailwind.css', 'app/assets/stylesheets/application.tailwind.css'
template 'tailwind.config.js', 'config/tailwind.config.js'
- unless File.exist?('Procfile.dev')
- create_file 'Procfile.dev', "storefront_css: bin/rails tailwindcss:watch\n"
- else
+ if Rails.root && Rails.root.join("Procfile.dev").exist?
append_to_file 'Procfile.dev', "\nstorefront_css: bin/rails tailwindcss:watch" unless File.read('Procfile.dev').include?('storefront_css:')
+ else
+ create_file 'Procfile.dev', "storefront_css: bin/rails tailwindcss:watch\n"
end
+ say "Add bin/dev to start foreman"
+ copy_file "dev", "bin/dev", force: true
+ chmod "bin/dev", 0755, verbose: false
+
+ empty_directory Rails.root.join('app/assets/builds') if Rails.root
+
unless File.exist?('app/assets/config/manifest.js')
create_file 'app/assets/config/manifest.js', "//= link_tree ../builds\n"
+
+ say "Ensure foreman is installed"
+ run "gem install foreman"
else
append_to_file 'app/assets/config/manifest.js', "\n//= link_tree ../builds" unless File.read('app/assets/config/manifest.js').include?('//= link_tree ../builds')
end
diff --git a/storefront/lib/generators/spree/storefront/install/templates/dev b/storefront/lib/generators/spree/storefront/install/templates/dev
new file mode 100755
index 0000000000..26c6feb97e
--- /dev/null
+++ b/storefront/lib/generators/spree/storefront/install/templates/dev
@@ -0,0 +1,16 @@
+#!/usr/bin/env sh
+
+if ! gem list foreman -i --silent; then
+ echo "Installing foreman..."
+ gem install foreman
+fi
+
+# Default to port 3000 if not specified
+export PORT="${PORT:-3000}"
+
+# Let the debug gem allow remote connections,
+# but avoid loading until `debugger` is called
+export RUBY_DEBUG_OPEN="true"
+export RUBY_DEBUG_LAZY="true"
+
+exec foreman start -f Procfile.dev "$@"
\ No newline at end of file