# frozen_string_literal: true

# Local path overrides for templating development.
# Loaded by the associated non-local gemfile when SMORG_RB_DEV or KETTLE_RB_DEV
# is not "false".

nomono_activation_requirements = ["~> 1.0", ">= 1.0.8"]
nomono_lockfile = File.expand_path("../../Gemfile.lock", __dir__)
if File.file?(nomono_lockfile)
  nomono_locked_spec = Bundler::LockfileParser
    .new(Bundler.read_file(nomono_lockfile))
    .specs
    .find { |spec| spec.name == "nomono" }
  nomono_locked = nomono_locked_spec &&
    Gem::Requirement.new(nomono_activation_requirements).satisfied_by?(nomono_locked_spec.version)
  if nomono_locked
    nomono_activation_requirements = ["= #{nomono_locked_spec.version}"]
  end
end
Kernel.send(:gem, "nomono", *nomono_activation_requirements)
require "nomono/bundler"

smorg_rb_local_gems = %w[
  tree_haver
  ast-merge
  ast-merge-git
  ast-template
  bash-merge
  binary-merge
  commonmarker-merge
  citrus-toml-merge
  dotenv-merge
  go-merge
  json-merge
  kettle-jem
  kramdown-merge
  markdown-merge
  markly-merge
  parslet-toml-merge
  plain-merge
  prism-merge
  psych-merge
  rbs-merge
  ruby-merge
  rust-merge
  smorg-rb
  typescript-merge
  toml-merge
  yaml-merge
  zip-merge
  ast-crispr
  ast-crispr-markdown-markly
  ast-crispr-ruby-prism
]

kettle_rb_local_gems = %w[
  kettle-dev kettle-test kettle-soup-cover kettle-drift
]
declared_gems = instance_variable_get(:@dependencies).to_a.map(&:name)
smorg_rb_local_gems_to_eval = smorg_rb_local_gems - %w[oauth-tty] - declared_gems
kettle_rb_local_gems_to_eval = kettle_rb_local_gems - %w[oauth-tty] - declared_gems

tslp_dev = ENV.fetch("TSLP_DEV", nil)

# export VENDORED_GEMS=
platform :mri do
  unless tslp_dev.to_s.empty?
    gem "tree_sitter_language_pack", path: tslp_dev
  end

  if ENV.fetch("SMORG_RB_DEV", "false").casecmp("false") != 0
    eval_nomono_gems(
      gems: smorg_rb_local_gems_to_eval,
      prefix: "SMORG_RB",
      path_env: "SMORG_RB_DEV",
      vendored_gems_env: "VENDORED_GEMS",
      vendor_gem_dir_env: "VENDOR_GEM_DIR",
      debug_env: "SMORG_RB_DEBUG"
    )
  end

  if ENV.fetch("KETTLE_RB_DEV", "false").casecmp("false") != 0
    eval_nomono_gems(
      gems: kettle_rb_local_gems_to_eval,
      prefix: "KETTLE_RB",
      path_env: "KETTLE_RB_DEV",
      vendored_gems_env: "VENDORED_GEMS",
      vendor_gem_dir_env: "VENDOR_GEM_DIR",
      debug_env: "KETTLE_DEV_DEBUG"
    )
  end
end
