# frozen_string_literal: true

unless defined?(Spree::InstallGenerator)
  require 'generators/spree/install/install_generator'
end

require 'generators/spree/dummy/dummy_generator'

namespace :common do
  task :test_app, :user_class do |_t, args|
    args.with_defaults(user_class: "Spree::LegacyUser")
    require ENV['LIB_NAME']

    ENV["RAILS_ENV"] = 'test'

    Spree::DummyGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--quiet"]
    Spree::InstallGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--auto-accept", "--migrate=false", "--seed=false", "--sample=false", "--quiet", "--user_class=#{args[:user_class]}"]

    puts "Setting up dummy database..."

    sh "bin/rails db:environment:set RAILS_ENV=test"
    sh "bin/rails db:drop db:create db:migrate VERBOSE=false RAILS_ENV=test"

    begin
      require "generators/#{ENV['LIB_NAME']}/install/install_generator"
      puts 'Running extension installation generator...'
      "#{ENV['LIB_NAME'].camelize}::Generators::InstallGenerator".constantize.start(["--auto-run-migrations"])
    rescue LoadError
      # No extension generator to run
    end
  end

  task :seed do |_t, _args|
    puts "Seeding ..."

    sh "bundle exec rake db:seed RAILS_ENV=test"
  end
end

# frozen_string_literal: true

unless defined?(Spree::InstallGenerator)
  require 'generators/spree/install/install_generator'
end

require 'generators/spree/dummy/dummy_generator'

namespace :common do
  task :test_app, :user_class do |_t, args|
    args.with_defaults(user_class: "Spree::LegacyUser")
    require ENV['LIB_NAME']

    ENV["RAILS_ENV"] = 'test'

    Spree::DummyGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--quiet"]
    Spree::InstallGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--auto-accept", "--migrate=false", "--seed=false", "--sample=false", "--quiet", "--user_class=#{args[:user_class]}"]

    puts "Setting up dummy database..."

    sh "bin/rails db:environment:set RAILS_ENV=test"
    sh "bin/rails db:drop db:create db:migrate VERBOSE=false RAILS_ENV=test"

    begin
      require "generators/#{ENV['LIB_NAME']}/install/install_generator"
      puts 'Running extension installation generator...'
      "#{ENV['LIB_NAMESPACE'] || ENV['LIB_NAME'].camelize}::Generators::InstallGenerator".constantize.start(["--auto-run-migrations"])
    rescue LoadError
      # No extension generator to run
    end
  end

  task :seed do |_t, _args|
    puts "Seeding ..."

    sh "bundle exec rake db:seed RAILS_ENV=test"
  end
end

Enable extension developers to customize the namespace
Enable extension developers to customize the namespace

If you are developing an extension and you want to use
`Rake::Task['extension:test_app']` you are forced to have a file named
exactly as the capitalized version of the extension namespace name, so
if you name your namespace extension e.g. SolidusGraphQL you are forced
to create a file named `solidus_graph_q_l`, otherwise the following
error is raised:

```
> bundle exec rake test_app --trace
** Invoke default (first_time)
** Invoke first_run (first_time)
** Execute first_run
** Invoke test_app (first_time)
** Execute test_app
** Invoke extension:test_app (first_time)
** Execute extension:test_app
** Invoke common:test_app (first_time)
** Execute common:test_app
rake aborted!
LoadError: cannot load such file -- solidus_graph_q_l
```

 This change allows the developer to customize the extension namespace
setting a `LIB_NAMESPACE` environment variable.
