# frozen_string_literal: true

def rails_next?
  File.basename(__FILE__) == 'Gemfile_rails_next'
end

def rails_current?
  !rails_next?
end

#---------------------------------------------------------------------------------------------------
# Unless we are in a deployment mode or are running from a script bundle, show a warning
is_install_or_update = (ARGV.empty? || ARGV.include?('install') || ARGV.include?('update'))
not_deployment_mode = !ARGV.include?('--deployment')

if File.basename($PROGRAM_NAME) == 'bundle' && is_install_or_update && not_deployment_mode && !ENV['SCRIPT_BUNDLE'] && !$printed_bundle_warning
  require_relative 'config/initializers/string_colors'
  puts
  puts 'WARNING: You seem to be running a bundle install command directly. This is not recommended.'.yellow
  puts
  puts "Please note: You need to use the #{'./script/bundle'.yellow} script for managing gems to ensure proper rubygems caching within the repository."
  puts 'If you are in a deployment/CI situation, please use the --deployment flag for bundle install'
  puts

  # Only show the warning once
  $printed_bundle_warning = true
end

#---------------------------------------------------------------------------------------------------
jruby_version = File.read(File.join(__dir__, '.ruby-version')).strip.delete_prefix('jruby-')
supported_bundler_version = File.read(File.join(__dir__, '.bundler-version-supported')).strip

ALL_DEVELOPMENT_ENVS = %i[development crawler_development]
ALL_TEST_ENVS = %w[test crawler_test]

#---------------------------------------------------------------------------------------------------
# Pull gem index from rubygems
source 'https://rubygems.org'

# Pin the version of bundle we support
gem 'bundler', supported_bundler_version

# Load Swiftype Gems -- use our private gem repo
source 'https://artifactory.elastic.dev/artifactory/api/gems/swiftype-gems' do
  gem 'fortitude', '= 0.9.6.swiftype02'
end

#---------------------------------------------------------------------------------------------------
# Default dependencies for all environments (including the crawler)
#---------------------------------------------------------------------------------------------------
group :default, :crawler do
  gem 'addressable'
  gem 'bson', '~> 4.2.2'
  gem 'concurrent-ruby', '~> 1.1.4'
  gem 'elasticsearch', '~> 7.5.0'
  gem 'nokogiri', '~> 1.11.0', :require => false
  gem 'rake', '~> 12.3.2'

  if rails_next?
    gem 'rails', '= 5.0.7.2'
  else
    gem 'rails', '= 4.2.11.3'
  end

  # We need to bundle TZ data because on windows and in some minimal Linux installations there is
  # no system-level TZ data info and the app blows up when trying to use timezone information
  # See https://github.com/tzinfo/tzinfo/wiki/Resolving-TZInfo::DataSourceNotFound-Errors for details
  gem 'tzinfo-data'

  # Swiftype utils is a multi-platform gem and we can't use it via a git dependency.
  # Need to release it for both platforms first!
  gem 'swiftype_utils', '~> 0.2.13'
end

#---------------------------------------------------------------------------------------------------
# Default dependencies for all Rails environments (but not for the crawler)
#---------------------------------------------------------------------------------------------------
# If you update this gem, please update the sprockets_rails.rb initializer as well
gem 'sprockets-rails', '~> 2'
gem 'sprockets', '~> 3.7.2'
gem 'webpacker'

# This is used to run our workers under Jetty
gem 'jruby-rack-worker', :require => false, :platform => 'jruby'

gem 'jquery-rails', '~> 4.4.0'

gem 'devise', '~> 4.7.1'
gem 'devise-token_authenticatable', '~> 1.1.0'
gem 'valid_email', '0.0.12', :require => 'valid_email/email_validator'

gem 'ipaddress'
gem 'hashie', '= 3.6.0'

gem 'marco-polo'
gem 'uea-stemmer'
gem 'stringex', :require => 'stringex_lite'

gem 'state_machines', '~> 0.4.0'
gem 'state_machines-activemodel', '~> 0.4.1'

gem 'manticore', '~> 0.7.0', :platform => 'jruby'
gem 'httpclient'
gem 'faraday', '= 0.17.3'
gem 'faraday_middleware', '= 0.12.2'

gem 'statsd-instrument', '= 2.1.1'
gem 'kaminari', '>= 1.2.1'

gem 'rack', '>= 1.6.12'
gem 'rack-attack'

gem 'rack-ssl-enforcer'
gem 'cancancan'

gem 'jwt', '~> 1.5.1'

gem 'file-tail'

# Security updates
gem 'rack-cors', '~> 1.0.4', :require => 'rack/cors'
gem 'json', '~> 2.3.1'

if rails_current?
  gem 'breach-mitigation-rails'
end

# Used in multiple places around the codebase (see all the references to Zip::File)
gem 'rubyzip', '~> 1.3.0', :require => 'zip'

gem 'awesome_print', '~> 1.8.0'

gem 'htmlentities'
gem 'mime-types', '= 3.1'

gem 'gson', :platform => 'jruby'

gem 'childprocess', '3.0.0'

# Frito Pie
gem 'signet', '0.14'
gem 'octokit'
gem 'google-api-client', '0.41.0'
gem 'memoist', '0.16.0'
gem 'zendesk_api', '1.28.0'
gem 'restforce', '3.1.0'

# Connectors
gem 'salesforce_bulk_query', :git => 'git@github.com:gooddata/salesforce_bulk_query', :ref => '9695873'

# Used for config file validation
gem 'json-schema', :platform => :jruby

# We have an ability to inject Elastic APM agent into self-managed versions of the product
gem 'elastic-apm', :require => false, :platform => :jruby

# ^ elastic-apm dependency, remove the pin when upgrading
gem 'http', '4.0.4', :platform => :jruby

gem 'doorkeeper', '4.4.3'

gem 'kramdown', '>= 2.3.0'

gem 'pr_geohash'

#---------------------------------------------------------------------------------------------------
# Crawler-only default dependencies
#---------------------------------------------------------------------------------------------------
# This group acts as a default group for standalone crawler scripts/tests/etc
group :crawler do
  gem 'faux', :path => 'vendor/faux', :require => false
end

#---------------------------------------------------------------------------------------------------
# Special-case dependencies
#---------------------------------------------------------------------------------------------------
# Dependencies used on CI for building Rails assets
group :assets do
  gem 'uglifier', '>= 1.0.3'
  gem 'sass-rails', '~> 5.0'
  gem 'autoprefixer-rails'
  gem 'js-routes'
end

# Dependencies used on CI to build war files for self-managed production deployments
group :togo_not_bundled do
  gem 'jruby-jars', jruby_version, :require => false, :platform => 'jruby'
  gem 'warbler', :require => false, :platform => 'jruby'
end

#---------------------------------------------------------------------------------------------------
# Development dependencies for Rails environments (but not for the crawler)
#---------------------------------------------------------------------------------------------------
group :development do
  gem 'mail_view', '~> 2.0'
  gem 'better_errors'

  gem 'execjs'

  # Add ffaker to create fake people in demo accounts
  gem 'ffaker'
end

#---------------------------------------------------------------------------------------------------
# Development and test dependencies for all environments (including the crawler)
#---------------------------------------------------------------------------------------------------
group(*ALL_DEVELOPMENT_ENVS) do
  gem 'rubocop', '0.52.1'

  gem 'ruby-debug-ide', :platform => 'jruby'
  gem 'pry-remote', :platform => 'jruby'
  gem 'pry-nav'
  gem 'ruby-debug-base', '0.11.0', :platform => 'jruby'

  gem 'tty-prompt', :require => false
end

group(*ALL_TEST_ENVS) do
  # Warning: Keep this version in sync with spec/fixtures/shared_togo/jetty_server/simple_web_app/Gemfile
  # Otherwise, JettyServer specs will fail on CI
  gem 'test-unit', '= 3.3.6'

  gem 'rspec-core', '~> 3.10.1'
  gem 'rspec-rails', '~> 4.0.2'
  gem 'rspec-collection_matchers', '~> 1.2.0'
  gem 'rspec_junit_formatter'

  gem 'webmock'
  gem 'vcr'
  gem 'climate_control'

  gem 'timecop'
  gem 'simplecov', :require => false
  gem 'simplecov-material', :require => false
end

group(*ALL_DEVELOPMENT_ENVS, *ALL_TEST_ENVS) do
  # Please note: We use the ent-search branch for swiftype-dev-services (and master for swiftype/website)
  gem 'swiftype-dev-services', :require => false, :git => 'git@github.com:swiftype/dev-services.git', :branch => 'ent-search'

  gem 'pry'

  gem 'factory_bot_rails', '~> 4.11.0', :require => false
  gem 'rb-fsevent', :require => false
  gem 'rb-inotify', :require => false
  gem 'listen', '~> 1.0'

  gem 'ejson'
end

#---------------------------------------------------------------------------------------------------
# Test dependencies for Rails environments (but not for the crawler)
#---------------------------------------------------------------------------------------------------
group :test do
  gem 'rack-parser', :require => 'rack/parser'
  gem 'rack-test', '~> 0.6.3'
  gem 'rswag-specs', :git => 'git@github.com:jetpackworkflow/rswag.git', :branch => 'allow_oas3_param_schema_array'
  # gem 'rswag-specs', '~> 2.3.1' # TODO - I want this back. Blocked by https://github.com/rswag/rswag/issues/317

  # Used for checking migration fixtures against ES state
  gem 'json-diff'

  if rails_next?
    gem 'rails-controller-testing', '~> 1.0.5'
  end
end
