From c5840e992355abdf1f8c0db14c32d2048520a2fe Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Sun, 29 Aug 2021 00:54:43 +0900 Subject: [PATCH] Revise path DSL --- lib/steep/project/dsl.rb | 27 +++++++++++------- lib/steep/project/options.rb | 18 ++++++++---- lib/steep/project/target.rb | 25 +++++++++++----- test/steepfile_test.rb | 15 ++++++---- test/target_test.rb | 55 +++++++++++++++++++++++------------- 5 files changed, 94 insertions(+), 46 deletions(-) diff --git a/lib/steep/project/dsl.rb b/lib/steep/project/dsl.rb index 72e7ad14e..985b88ffa 100644 --- a/lib/steep/project/dsl.rb +++ b/lib/steep/project/dsl.rb @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 diff --git a/lib/steep/project/options.rb b/lib/steep/project/options.rb index 9aeb22746..f0669ce22 100644 --- a/lib/steep/project/options.rb +++ b/lib/steep/project/options.rb @@ -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! diff --git a/lib/steep/project/target.rb b/lib/steep/project/target.rb index f7e51affb..81d288571 100644 --- a/lib/steep/project/target.rb +++ b/lib/steep/project/target.rb @@ -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) diff --git a/test/steepfile_test.rb b/test/steepfile_test.rb index 6c153f784..ac01d8c6e 100644 --- a/test/steepfile_test.rb +++ b/test/steepfile_test.rb @@ -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" @@ -27,6 +28,7 @@ def test_config end target :Gemfile, template: :gemfile do + stdlib_path(core_root: false, stdlib_root: false) end EOF @@ -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 @@ -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 @@ -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, @@ -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 diff --git a/test/target_test.rb b/test/target_test.rb index 60126a7e4..24ffa3034 100644 --- a/test/target_test.rb +++ b/test/target_test.rb @@ -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