-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor dependency detector (#1042)
Refactor DependencyDetector
- Loading branch information
Showing
19 changed files
with
171 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,80 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "singleton" | ||
|
||
module RubyLsp | ||
module DependencyDetector | ||
class << self | ||
extend T::Sig | ||
|
||
sig { returns(String) } | ||
def detected_formatter | ||
# NOTE: Intentionally no $ at end, since we want to match rubocop-shopify, etc. | ||
if direct_dependency?(/^rubocop/) | ||
"rubocop" | ||
elsif direct_dependency?(/^syntax_tree$/) | ||
"syntax_tree" | ||
else | ||
"none" | ||
end | ||
end | ||
class DependencyDetector | ||
include Singleton | ||
extend T::Sig | ||
|
||
sig { returns(String) } | ||
def detected_test_library | ||
# A Rails app may have a dependency on minitest, but we would instead want to use the Rails test runner provided | ||
# by ruby-lsp-rails. | ||
if direct_dependency?(/^rails$/) | ||
"rails" | ||
# NOTE: Intentionally ends with $ to avoid mis-matching minitest-reporters, etc. in a Rails app. | ||
elsif direct_dependency?(/^minitest$/) | ||
"minitest" | ||
elsif direct_dependency?(/^test-unit/) | ||
"test-unit" | ||
elsif direct_dependency?(/^rspec/) | ||
"rspec" | ||
else | ||
"unknown" | ||
end | ||
end | ||
sig { returns(String) } | ||
attr_reader :detected_formatter | ||
|
||
sig { returns(String) } | ||
attr_reader :detected_test_library | ||
|
||
sig { returns(T::Boolean) } | ||
attr_reader :typechecker | ||
|
||
sig { void } | ||
def initialize | ||
@detected_formatter = T.let(detect_formatter, String) | ||
@detected_test_library = T.let(detect_test_library, String) | ||
@typechecker = T.let(detect_typechecker, T::Boolean) | ||
end | ||
|
||
sig { returns(T::Boolean) } | ||
def typechecker? | ||
direct_dependency?(/^sorbet/) || direct_dependency?(/^sorbet-static-and-runtime/) | ||
sig { returns(String) } | ||
def detect_formatter | ||
# NOTE: Intentionally no $ at end, since we want to match rubocop-shopify, etc. | ||
if direct_dependency?(/^rubocop/) | ||
"rubocop" | ||
elsif direct_dependency?(/^syntax_tree$/) | ||
"syntax_tree" | ||
else | ||
"none" | ||
end | ||
end | ||
|
||
sig { params(gem_pattern: Regexp).returns(T::Boolean) } | ||
def direct_dependency?(gem_pattern) | ||
Bundler.with_original_env { Bundler.default_gemfile } && | ||
Bundler.locked_gems.dependencies.keys.grep(gem_pattern).any? | ||
rescue Bundler::GemfileNotFound | ||
false | ||
sig { returns(String) } | ||
def detect_test_library | ||
# A Rails app may have a dependency on minitest, but we would instead want to use the Rails test runner provided | ||
# by ruby-lsp-rails. | ||
if direct_dependency?(/^rails$/) | ||
"rails" | ||
# NOTE: Intentionally ends with $ to avoid mis-matching minitest-reporters, etc. in a Rails app. | ||
elsif direct_dependency?(/^minitest$/) | ||
"minitest" | ||
elsif direct_dependency?(/^test-unit/) | ||
"test-unit" | ||
elsif direct_dependency?(/^rspec/) | ||
"rspec" | ||
else | ||
"unknown" | ||
end | ||
end | ||
|
||
HAS_TYPECHECKER = T.let(typechecker?, T::Boolean) | ||
sig { params(gem_pattern: Regexp).returns(T::Boolean) } | ||
def direct_dependency?(gem_pattern) | ||
dependency_keys.grep(gem_pattern).any? | ||
end | ||
|
||
sig { returns(T::Boolean) } | ||
def detect_typechecker | ||
direct_dependency?(/^sorbet/) || direct_dependency?(/^sorbet-static-and-runtime/) | ||
end | ||
|
||
sig { returns(T::Array[String]) } | ||
def dependency_keys | ||
@dependency_keys ||= T.let( | ||
begin | ||
Bundler.with_original_env { Bundler.default_gemfile } | ||
Bundler.locked_gems.dependencies.keys | ||
rescue Bundler::GemfileNotFound | ||
[] | ||
end, | ||
T.nilable(T::Array[String]), | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# typed: true | ||
|
||
module Singleton | ||
sig { params(klass: Class).void } | ||
def self.__init__(klass); end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule yarp
updated
from afbc31 to 0066cd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.