Skip to content

Commit

Permalink
Merge pull request #421 from soutaro/dsl-paths
Browse files Browse the repository at this point in the history
Revise path DSL
  • Loading branch information
soutaro committed Aug 29, 2021
2 parents 9a873b1 + c5840e9 commit c47a969
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 46 deletions.
27 changes: 17 additions & 10 deletions lib/steep/project/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class TargetDSL
attr_reader :libraries
attr_reader :signatures
attr_reader :ignored_sources
attr_reader :vendor_dir
attr_reader :stdlib_root
attr_reader :core_root
attr_reader :strictness_level
attr_reader :typing_option_hash
attr_reader :repo_paths
Expand All @@ -18,9 +19,10 @@ def initialize(name, sources: [], libraries: [], signatures: [], ignored_sources
@libraries = libraries
@signatures = signatures
@ignored_sources = ignored_sources
@vendor_dir = nil
@strictness_level = :default
@typing_option_hash = {}
@core_root = nil
@stdlib_root = nil
@repo_paths = []
end

Expand All @@ -30,10 +32,11 @@ def initialize_copy(other)
@libraries = other.libraries.dup
@signatures = other.signatures.dup
@ignored_sources = other.ignored_sources.dup
@vendor_dir = other.vendor_dir
@strictness_level = other.strictness_level
@typing_option_hash = other.typing_option_hash
@repo_paths = other.repo_paths.dup
@core_root = other.core_root
@stdlib_root = other.stdlib_root
end

def check(*args)
Expand Down Expand Up @@ -68,15 +71,16 @@ def update(name: self.name, sources: self.sources, libraries: self.libraries, ig
end

def no_builtin!(value = true)
Steep.logger.error "`no_builtin!` in Steepfile is deprecated and ignored. Use `vendor` instead."
Steep.logger.error "`#no_builtin!` in Steepfile is deprecated and ignored. Use `#stdlib_path` instead."
end

def vendor(dir = "vendor/sigs", stdlib: nil, gems: nil)
if stdlib || gems
Steep.logger.warn { "#vendor with stdlib: or gems: keyword is deprecated." }
end
Steep.logger.error "`#vendor` in Steepfile is deprecated and ignored. Use `#stdlib_path` instead."
end

@vendor_dir = Pathname(dir)
def stdlib_path(core_root:, stdlib_root:)
@core_root = core_root ? Pathname(core_root) : core_root
@stdlib_root = stdlib_root ? Pathname(stdlib_root) : stdlib_root
end

def repo_path(*paths)
Expand Down Expand Up @@ -129,8 +133,11 @@ def target(name, template: nil, &block)
signature_pattern: signature_pattern,
options: Options.new.tap do |options|
options.libraries.push(*target.libraries)
options.repository_paths.push(*target.repo_paths)
options.vendor_path = target.vendor_dir
options.paths = Options::PathOptions.new(
core_root: target.core_root,
stdlib_root: target.stdlib_root,
repo_paths: target.repo_paths
)

case target.strictness_level
when :strict
Expand Down
18 changes: 13 additions & 5 deletions lib/steep/project/options.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
module Steep
class Project
class Options
PathOptions = Struct.new(:core_root, :stdlib_root, :repo_paths, keyword_init: true) do
def customized_stdlib?
stdlib_root != nil
end

def customized_core?
core_root != nil
end
end

attr_accessor :allow_fallback_any
attr_accessor :allow_missing_definitions
attr_accessor :allow_unknown_constant_assignment
attr_accessor :allow_unknown_method_calls
attr_accessor :vendor_path

attr_reader :libraries
attr_reader :repository_paths
attr_accessor :paths

def initialize
apply_default_typing_options!
self.vendor_path = nil

@paths = PathOptions.new(repo_paths: [])
@libraries = []
@repository_paths = []
end

def apply_default_typing_options!
Expand Down
25 changes: 18 additions & 7 deletions lib/steep/project/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,27 @@ def new_env_loader(project:)
end

def self.construct_env_loader(options:, project:)
repo = RBS::Repository.new(no_stdlib: options.vendor_path)
options.repository_paths.each do |path|
repo = RBS::Repository.new(no_stdlib: options.paths.customized_stdlib?)

if options.paths.stdlib_root
repo.add(project.absolute_path(options.paths.stdlib_root))
end

options.paths.repo_paths.each do |path|
repo.add(project.absolute_path(path))
end

loader = RBS::EnvironmentLoader.new(
core_root: options.vendor_path ? nil : RBS::EnvironmentLoader::DEFAULT_CORE_ROOT,
repository: repo
)
loader.add(path: options.vendor_path) if options.vendor_path
core_root_path =
if options.paths.customized_core?
if options.paths.core_root
project.absolute_path(options.paths.core_root)
end
else
RBS::EnvironmentLoader::DEFAULT_CORE_ROOT
end

loader = RBS::EnvironmentLoader.new(core_root: core_root_path, repository: repo)

options.libraries.each do |lib|
name, version = lib.split(/:/, 2)
loader.add(library: name, version: version)
Expand Down
15 changes: 10 additions & 5 deletions test/steepfile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def test_config
typing_options :strict
check "app"
ignore "app/views"
vendor
stdlib_path(core_root: "vendor/rbs/core", stdlib_root: "vendor/rbs/stdlib")
signature "sig", "sig-private"
Expand All @@ -27,6 +28,7 @@ def test_config
end
target :Gemfile, template: :gemfile do
stdlib_path(core_root: false, stdlib_root: false)
end
EOF

Expand All @@ -38,7 +40,8 @@ def test_config
assert_equal ["app/views"], target.source_pattern.ignores
assert_equal ["sig", "sig-private"], target.signature_pattern.patterns
assert_equal ["set", "strong_json"], target.options.libraries
assert_equal Pathname("vendor/sigs"), target.options.vendor_path
assert_equal Pathname("vendor/rbs/core"), target.options.paths.core_root
assert_equal Pathname("vendor/rbs/stdlib"), target.options.paths.stdlib_root
assert_equal false, target.options.allow_missing_definitions
end

Expand All @@ -48,7 +51,8 @@ def test_config
assert_equal [], target.source_pattern.ignores
assert_equal [], target.signature_pattern.patterns
assert_equal ["gemfile"], target.options.libraries
assert_nil target.options.vendor_path
assert_equal false, target.options.paths.core_root
assert_equal false, target.options.paths.stdlib_root
assert_equal true, target.options.allow_missing_definitions
end
end
Expand All @@ -62,7 +66,6 @@ def test_config_typing_options
target :app do
check "app"
ignore "app/views"
vendor
typing_options :strict,
allow_missing_definitions: true,
Expand Down Expand Up @@ -102,7 +105,9 @@ def test_repo_path
repo_path "vendor/rbs/internal"
end
EOF
assert_equal [Pathname("vendor/rbs/internal")], project.targets[0].options.repository_paths
project.targets[0].tap do |target|
assert_equal [Pathname("vendor/rbs/internal")], target.options.paths.repo_paths
end
end
end
end
55 changes: 36 additions & 19 deletions test/target_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,58 @@ module Steep
class TargetTest < Minitest::Test
include TestHelper

def test_environment_loader
def with_dir
Dir.mktmpdir do |dir|
path = Pathname(dir)

(path + "vendor/repo").mkpath
(path + "vendor/core").mkpath
(path + "vendor/stdlib").mkpath

project = Project.new(steepfile_path: path + "Steepfile")
yield project, path
end
end

Project::Target.construct_env_loader(
options: Project::Options.new.tap {|opts|
opts.repository_paths << path + "vendor/repo"
},
project: project
).tap do |loader|
refute_nil loader.core_root
def test_environment_loader_default
with_dir do |project, path|
options = Project::Options.new()
options.paths.core_root = nil
options.paths.stdlib_root = nil

Project::Target.construct_env_loader(options: options, project: project).tap do |loader|
assert_equal RBS::EnvironmentLoader::DEFAULT_CORE_ROOT, loader.core_root
assert_includes loader.repository.dirs, RBS::Repository::DEFAULT_STDLIB_ROOT
assert_includes loader.repository.dirs, path + "vendor/repo"
end
end
end

Project::Target.construct_env_loader(
options: Project::Options.new.tap {|opts|
opts.vendor_path = path + "vendor/core"
opts.repository_paths << path + "vendor/repo"
},
project: project
).tap do |loader|
assert_nil loader.core_root
def test_environment_loader_custom
with_dir do |project, path|
options = Project::Options.new()
options.paths.core_root = Pathname("vendor/core")
options.paths.stdlib_root = Pathname("vendor/stdlib")
options.paths.repo_paths << Pathname("vendor/repo")

assert_includes loader.dirs, path + "vendor/core"
refute_includes loader.repository.dirs, RBS::Repository::DEFAULT_STDLIB_ROOT
Project::Target.construct_env_loader(options: options, project: project).tap do |loader|
assert_equal path + "vendor/core", loader.core_root
assert_includes loader.repository.dirs, path + "vendor/stdlib"
assert_includes loader.repository.dirs, path + "vendor/repo"
end
end
end

def test_environment_loader_none
with_dir do |project, path|
options = Project::Options.new()
options.paths.core_root = false
options.paths.stdlib_root = false

Project::Target.construct_env_loader(options: options, project: project).tap do |loader|
assert_nil loader.core_root
assert_empty loader.repository.dirs
end
end
end
end
end

0 comments on commit c47a969

Please sign in to comment.