Sha256: b7779b4713434ae4ee7b6d2e90f34d6606f98b826c5c18cfc689651521f85c42

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

# typed: strict
# frozen_string_literal: true

require "pathname"
require "shellwords"

module Tapioca
  module SorbetHelper
    extend T::Sig

    SORBET_GEM_SPEC = T.let(
      ::Gem::Specification.find_by_name("sorbet-static"),
      ::Gem::Specification
    )

    SORBET_BIN = T.let(
      Pathname.new(SORBET_GEM_SPEC.full_gem_path) / "libexec" / "sorbet",
      Pathname
    )

    SORBET_EXE_PATH_ENV_VAR = "TAPIOCA_SORBET_EXE"

    FEATURE_REQUIREMENTS = T.let({
      to_ary_nil_support: ::Gem::Requirement.new(">= 0.5.9220"),         # https://github.com/sorbet/sorbet/pull/4706
      print_payload_sources: ::Gem::Requirement.new(">= 0.5.9818"),      # https://github.com/sorbet/sorbet/pull/5504
      type_variable_block_syntax: ::Gem::Requirement.new(">= 0.5.9892"), # https://github.com/sorbet/sorbet/pull/5639
    }.freeze, T::Hash[Symbol, ::Gem::Requirement])

    sig { params(sorbet_args: String).returns(Spoom::ExecResult) }
    def sorbet(*sorbet_args)
      Spoom::Sorbet.srb(sorbet_args.join(" "), sorbet_bin: sorbet_path, capture_err: true)
    end

    sig { returns(String) }
    def sorbet_path
      sorbet_path = ENV.fetch(SORBET_EXE_PATH_ENV_VAR, SORBET_BIN)
      sorbet_path = SORBET_BIN if sorbet_path.empty?
      sorbet_path.to_s.shellescape
    end

    sig { params(feature: Symbol, version: T.nilable(::Gem::Version)).returns(T::Boolean) }
    def sorbet_supports?(feature, version: nil)
      version = SORBET_GEM_SPEC.version unless version
      requirement = FEATURE_REQUIREMENTS[feature]

      Kernel.raise "Invalid Sorbet feature #{feature}" unless requirement

      requirement.satisfied_by?(version)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tapioca-0.8.3 lib/tapioca/helpers/sorbet_helper.rb
tapioca-0.8.2 lib/tapioca/helpers/sorbet_helper.rb
tapioca-0.8.1 lib/tapioca/helpers/sorbet_helper.rb
tapioca-0.8.0 lib/tapioca/helpers/sorbet_helper.rb