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

More generator updates #198

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion lib/solidus_dev_support/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'thor'
require 'pathname'
require 'shellwords'

require 'solidus_dev_support/version'

Expand All @@ -16,7 +17,7 @@ class Extension < Thor
def generate(raw_path = '.')
self.path = raw_path

empty_directory path
run "git init #{path.shellescape} --quiet"

directory 'app', "#{path}/app"
directory 'lib', "#{path}/lib"
Expand Down
10 changes: 7 additions & 3 deletions lib/solidus_dev_support/rake_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ def install
end

def install_test_app_task
require 'rake/clean'
require 'spree/testing_support/extension_rake'

ENV['DUMMY_PATH'] = test_app_path.to_s
ENV['LIB_NAME'] = gemspec.name

# We need to skip solidus bolt for Solidus v3.2,
# otherwise it will try to install it when creating the dummy app.
ENV['SKIP_SOLIDUS_BOLT'] = 'true'

require 'rake/clean'
require 'spree/testing_support/extension_rake'

::CLOBBER.include test_app_path

namespace :extension do
Expand Down
32 changes: 27 additions & 5 deletions lib/solidus_dev_support/templates/extension/.circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,37 @@ jobs:
- browser-tools/install-chrome
- solidusio_extensions/run-tests
run-specs-with-postgres:
executor: solidusio_extensions/postgres
executor:
name: solidusio_extensions/postgres
ruby_version: '3.1'
steps:
- checkout
- browser-tools/install-chrome
- solidusio_extensions/run-tests
- solidusio_extensions/run-tests-solidus-master
- solidusio_extensions/store-test-results

run-specs-with-mysql:
executor: solidusio_extensions/mysql
executor:
name: solidusio_extensions/mysql
ruby_version: '3.0'
steps:
- checkout
- browser-tools/install-chrome
- solidusio_extensions/run-tests
- solidusio_extensions/run-tests-solidus-current
- solidusio_extensions/store-test-results

run-specs-with-sqlite:
executor:
name: solidusio_extensions/sqlite
ruby_version: '2.7'
steps:
- checkout
- browser-tools/install-chrome
- solidusio_extensions/run-tests-solidus-older
- solidusio_extensions/store-test-results

lint-code:
executor: solidusio_extensions/sqlite-memory
executor: solidusio_extensions/sqlite
steps:
- solidusio_extensions/lint-code

Expand All @@ -37,6 +57,7 @@ workflows:
- run-specs-with-sqlite
- run-specs-with-postgres
- run-specs-with-mysql
- run-specs-with-sqlite
- lint-code

"Weekly run specs against master":
Expand All @@ -51,3 +72,4 @@ workflows:
- run-specs-with-sqlite
- run-specs-with-postgres
- run-specs-with-mysql
- run-specs-with-sqlite
82 changes: 36 additions & 46 deletions lib/solidus_dev_support/templates/extension/bin/sandbox.tt
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
#!/usr/bin/env bash

# Set the extension name
extension_name="<%= file_name %>"

set -e
test -z "${DEBUG+empty_string}" || set -x

test -n "$DB" || echo "~~~> Use 'export DB=[postgres|mysql|sqlite3]' to control the DB adapter"
# Support CircleCI runs in which DB is set to "sqlite" by the extension ORB.
test "$DB" = "sqlite" && export DB="sqlite3"
echo '~~~> Using $DB as the database engine'

if [ -z "$SOLIDUS_BRANCH" ]
then
echo "~~> Use 'export SOLIDUS_BRANCH=[master|v3.2|...]' to control the Solidus branch"
SOLIDUS_BRANCH="master"
fi
echo "~~> Using branch $SOLIDUS_BRANCH of solidus"

if [ -z "$SOLIDUS_FRONTEND" ]
then
echo "~~> Use 'export SOLIDUS_FRONTEND=[solidus_frontend|solidus_starter_frontend]' to control the Solidus frontend"
SOLIDUS_FRONTEND="solidus_frontend"
fi
echo "~~> Using branch $SOLIDUS_FRONTEND as the solidus frontend"
test -n "$SOLIDUS_BRANCH" || echo "~~~> Use 'export SOLIDUS_BRANCH=[master|v3.2|...]' to control the Solidus branch"
echo '~~~> Using branch $SOLIDUS_BRANCH of solidus'

extension_name="<%= file_name %>"

# Stay away from the bundler env of the containing extension.
function unbundled {
ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
# Override bundle in order to stay away from the
# bundler env of the containing extension.
function with_unbundled_env {
ruby -rbundler -e'Bundler.with_unbundled_env { system *ARGV }' -- $@
}

echo "~~~> Removing the old sandbox"
rm -rf ./sandbox
unbundled bundle exec rails new sandbox \
--database="${DB:-sqlite3}" \
--skip-bundle \

echo "~~~> Creating a pristine Rails app"
with_unbundled_env rails new sandbox \
--database="$DB" \
--skip-git \
--skip-keeps \
--skip-rc \
Expand All @@ -43,36 +39,30 @@ if [ ! -d "sandbox" ]; then
fi

cd ./sandbox
cat <<RUBY >> Gemfile
gem 'solidus', github: 'solidusio/solidus', branch: '$SOLIDUS_BRANCH'
gem 'rails-i18n'
gem 'solidus_i18n'

gem '$extension_name', path: '..'
# Ensure any previous sandbox DB is recreated
bin/rails db:drop db:create

group :test, :development do
platforms :mri do
gem 'pry-byebug'
end
end
echo "~~~> Adding solidus to the Gemfile"
cat <<RUBY >> Gemfile
gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
gem 'pry-byebug', group: [:test, :development], platforms: [:mri]
RUBY

unbundled bundle install --gemfile Gemfile

unbundled bundle exec rake db:drop db:create
bundle config local.$extension_name "$PWD/.."
bundle config gemfile "$PWD/Gemfile"
with_unbundled_env bundle install

unbundled bundle exec rails generate solidus:install \
--auto-accept \
--user_class=Spree::User \
--enforce_available_locales=true \
--with-authentication=<%= file_name != 'solidus_auth_devise' %> \
--payment-method=none \
--frontend=${SOLIDUS_FRONTEND} \
$@
echo "~~~> Running the solidus:install generator"
with_unbundled_env bin/rails generate solidus:install --auto-accept $@

unbundled bundle exec rails generate solidus:auth:install --auto-run-migrations
unbundled bundle exec rails generate ${extension_name}:install --auto-run-migrations
# If it's already in the bundle we can assume it was installed by solidus:install
(with_unbundled_env bundle list | grep --quiet $extension_name) || (
echo "~~~> Installing the extension" &&
with_unbundled_env bundle add $extension_name --path '..' &&
with_unbundled_env ./bin/rails generate "${extension_name}:install" --auto-run-migrations
)

echo
echo "🚀 Sandbox app successfully created for $extension_name!"
echo "🧪 This app is intended for test purposes."
echo "~~~> 🚀 Sandbox app successfully created for $extension_name!"
echo "~~~> 🧪 This app is intended for test purposes."
2 changes: 1 addition & 1 deletion solidus_dev_support.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'solidus_core', ['>= 2.0', '< 4']
spec.add_dependency 'webdrivers', '>= 4.4'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'bundler', '>= 2.3.22'
spec.add_development_dependency 'rake'
end
33 changes: 12 additions & 21 deletions spec/features/create_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def step(method_name)

def check_solidus_cmd
cd(tmp_path) do
output = `#{solidus_cmd} -h`
expect($?).to be_success
output = sh "#{solidus_cmd} -h"
expect(output).to include('Commands:')
end
end
Expand All @@ -53,8 +52,7 @@ def check_gem_version

cd(tmp_path) do
gem_version_commands.each do |gem_version_cmd|
output = `#{solidus_cmd} #{gem_version_cmd}`
expect($?).to be_success
output = sh "#{solidus_cmd} #{gem_version_cmd}"
expect(output).to include("Solidus version #{solidus_version}")
expect(output).to include("Solidus Dev Support version #{gem_version}")
end
Expand All @@ -63,8 +61,7 @@ def check_gem_version

def check_create_extension
cd(tmp_path) do
output = `#{solidus_cmd} extension #{extension_name}`
expect($?).to be_success
output = sh "#{solidus_cmd} extension #{extension_name}"
expect(output).to include(gemspec_name)
expect(output).to include('.circleci')
end
Expand Down Expand Up @@ -96,7 +93,7 @@ def check_create_extension

def check_bundle_install
cd(install_path) do
open('Gemfile', 'a') { |f| f.puts "gem 'solidus_dev_support', path: '../../..'" }
open('Gemfile', 'a') { |f| f.puts "gem 'solidus_dev_support', path: #{gem_root.inspect}" }
end

expect { bundle_install }.to raise_error(command_failed_error, /invalid gemspec/)
Expand All @@ -115,7 +112,7 @@ def check_bundle_install

def check_default_task
cd(install_path) do
output = sh('bin/rake')
output = unbundled_sh('bin/rake')
expect(output).to include('Generating dummy Rails application')
expect(output).to include('0 examples, 0 failures')
end
Expand All @@ -130,7 +127,7 @@ def check_run_specs
"require 'spec_helper'\nRSpec.describe 'Some test' do it { expect(true).to be_truthy } end\n"
)
cd(install_path) do
output = sh('bundle exec rspec')
output = unbundled_sh('bundle exec rspec')
expect(output).to include('loading test_extension factories')
expect(output).to include('1 example, 0 failures')
expect(output).to include(ENV['CODECOV_TOKEN'] ? 'Coverage reports upload successfully' : 'Provide a CODECOV_TOKEN environment variable to enable Codecov uploads')
Expand All @@ -147,17 +144,15 @@ def check_sandbox
expect(first_run_output).to include("Creating the sandbox app...")
expect(first_run_output).to include('The version of SolidusTestExtension is 0.0.1')

second_run_output = sh(command)
second_run_output = unbundled_sh(command)
expect(second_run_output).not_to include("Creating the sandbox app...")
expect(second_run_output).to include('The version of SolidusTestExtension is 0.0.1')
end
end

def sh(*args)
command = args.size == 1 ? args.first : args.shelljoin
output, status = with_unbundled_env do
Open3.capture2e({ 'CI' => nil }, command)
end
output, status = Open3.capture2e({ 'CI' => nil }, command)

if $DEBUG || ENV['DEBUG']
warn '~' * 80
Expand All @@ -173,12 +168,8 @@ def sh(*args)
output.to_s
end

def with_unbundled_env(&block)
if Bundler.respond_to?(:with_unbundled_env)
Bundler.with_unbundled_env(&block)
else
Bundler.with_clean_env(&block)
end
def unbundled_sh(*args)
Bundler.with_unbundled_env { sh(*args) }
end

def bundle_install
Expand All @@ -187,11 +178,11 @@ def bundle_install
bundle_path = "#{gem_root}/vendor/bundle"

if File.exist?(bundle_path)
sh "bundle config set --local path #{bundle_path.shellescape}"
unbundled_sh "bundle config set --local path #{bundle_path.shellescape}"
end

output = nil
cd(install_path) { output = sh 'bundle install' }
cd(install_path) { output = unbundled_sh 'bundle install' }
output
end
end