diff --git a/toys-core/lib/toys/cli.rb b/toys-core/lib/toys/cli.rb index 5bd29d6c..764e7cef 100644 --- a/toys-core/lib/toys/cli.rb +++ b/toys-core/lib/toys/cli.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -require "rbconfig" -require "logger" -require "toys/completion" - module Toys ## # A Toys-based CLI. @@ -566,6 +562,7 @@ def default_error_handler # @return [Proc] # def default_logger_factory + require "logger" proc do logger = ::Logger.new($stderr) logger.level = ::Logger::WARN diff --git a/toys-core/lib/toys/loader.rb b/toys-core/lib/toys/loader.rb index 97af0836..1471ca60 100644 --- a/toys-core/lib/toys/loader.rb +++ b/toys-core/lib/toys/loader.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "monitor" - module Toys ## # The Loader service loads tools from configuration files, and finds the @@ -55,6 +53,7 @@ def initialize(index_file_name: nil, if index_file_name && ::File.extname(index_file_name) != ".rb" raise ::ArgumentError, "Illegal index file name #{index_file_name.inspect}" end + require "monitor" @mutex = ::Monitor.new @mixin_lookup = mixin_lookup || ModuleLookup.new @template_lookup = template_lookup || ModuleLookup.new @@ -477,7 +476,7 @@ def load_block(parent_source, block, words, remaining_words, priority) end end - @git_cache_mutex = ::Monitor.new + @git_cache_mutex = ::Mutex.new @default_git_cache = nil ## diff --git a/toys-core/lib/toys/module_lookup.rb b/toys-core/lib/toys/module_lookup.rb index 4b80ca43..edcede90 100644 --- a/toys-core/lib/toys/module_lookup.rb +++ b/toys-core/lib/toys/module_lookup.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "monitor" - module Toys ## # A helper module that provides methods to do module lookups. This is @@ -56,6 +54,7 @@ def path_to_module(path) # Create an empty ModuleLookup # def initialize + require "monitor" @mutex = ::Monitor.new @paths = [] @paths_locked = false diff --git a/toys-core/lib/toys/settings.rb b/toys-core/lib/toys/settings.rb index 3c8de72c..013cb769 100644 --- a/toys-core/lib/toys/settings.rb +++ b/toys-core/lib/toys/settings.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "date" - module Toys ## # A settings class defines the structure of application settings, i.e. the @@ -488,7 +486,7 @@ def for_scalar(value) def convert(val, klass) return val if val.is_a?(klass) begin - CONVERTERS[klass].call(val) + CONVERTERS[klass.name].call(val) rescue ::StandardError ILLEGAL_VALUE end @@ -564,13 +562,13 @@ def convert(val, klass) # @private # CONVERTERS = { - ::Date => date_converter, - ::DateTime => datetime_converter, - ::Float => float_converter, - ::Integer => integer_converter, - ::Regexp => regexp_converter, - ::Symbol => symbol_converter, - ::Time => time_converter, + "Date" => date_converter, + "DateTime" => datetime_converter, + "Float" => float_converter, + "Integer" => integer_converter, + "Regexp" => regexp_converter, + "Symbol" => symbol_converter, + "Time" => time_converter, }.freeze end diff --git a/toys-core/lib/toys/standard_middleware/show_help.rb b/toys-core/lib/toys/standard_middleware/show_help.rb index 8491868d..06fa0c91 100644 --- a/toys-core/lib/toys/standard_middleware/show_help.rb +++ b/toys-core/lib/toys/standard_middleware/show_help.rb @@ -248,8 +248,10 @@ def run(context) private def terminal - require "toys/utils/terminal" - @terminal ||= Utils::Terminal.new(output: @stream, styled: @styled_output) + @terminal ||= begin + require "toys/utils/terminal" + Utils::Terminal.new(output: @stream, styled: @styled_output) + end end def show_usage(context) diff --git a/toys-core/lib/toys/standard_mixins/exec.rb b/toys-core/lib/toys/standard_mixins/exec.rb index 9377e991..d2773c5d 100644 --- a/toys-core/lib/toys/standard_mixins/exec.rb +++ b/toys-core/lib/toys/standard_mixins/exec.rb @@ -830,6 +830,8 @@ def self._setup_clean_process(cmd) end on_initialize do |**opts| + require "rbconfig" + require "shellwords" require "toys/utils/exec" context = self opts = Exec._setup_exec_opts(opts, context) diff --git a/toys-core/lib/toys/standard_mixins/fileutils.rb b/toys-core/lib/toys/standard_mixins/fileutils.rb index c2d8478f..aac083b0 100644 --- a/toys-core/lib/toys/standard_mixins/fileutils.rb +++ b/toys-core/lib/toys/standard_mixins/fileutils.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "fileutils" - module Toys module StandardMixins ## @@ -20,6 +18,7 @@ module Fileutils # @private # def self.included(mod) + require "fileutils" mod.include(::FileUtils) end end diff --git a/toys-core/lib/toys/standard_mixins/gems.rb b/toys-core/lib/toys/standard_mixins/gems.rb index 24da319f..dab6440c 100644 --- a/toys-core/lib/toys/standard_mixins/gems.rb +++ b/toys-core/lib/toys/standard_mixins/gems.rb @@ -65,9 +65,11 @@ def gem(name, *requirements) # @private # def self.gems - require "toys/utils/gems" # rubocop:disable Naming/MemoizedInstanceVariableName - @__gems ||= Utils::Gems.new(**@__gems_opts) + @__gems ||= begin + require "toys/utils/gems" + Utils::Gems.new(**@__gems_opts) + end # rubocop:enable Naming/MemoizedInstanceVariableName end diff --git a/toys-core/lib/toys/standard_mixins/highline.rb b/toys-core/lib/toys/standard_mixins/highline.rb index 08043ff0..1a5bf1ec 100644 --- a/toys-core/lib/toys/standard_mixins/highline.rb +++ b/toys-core/lib/toys/standard_mixins/highline.rb @@ -131,7 +131,7 @@ def new_scope on_initialize do |*args| require "toys/utils/gems" - Toys::Utils::Gems.activate("highline", "~> 2.0") + ::Toys::Utils::Gems.activate("highline", "~> 2.0") require "highline" self[KEY] = ::HighLine.new(*args) self[KEY].use_color = $stdout.tty? diff --git a/toys-core/lib/toys/standard_mixins/xdg.rb b/toys-core/lib/toys/standard_mixins/xdg.rb index 92482af5..c16864d8 100644 --- a/toys-core/lib/toys/standard_mixins/xdg.rb +++ b/toys-core/lib/toys/standard_mixins/xdg.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "fileutils" - module Toys module StandardMixins ## diff --git a/toys-core/lib/toys/tool_definition.rb b/toys-core/lib/toys/tool_definition.rb index 403007c0..b85cb2c3 100644 --- a/toys-core/lib/toys/tool_definition.rb +++ b/toys-core/lib/toys/tool_definition.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "set" - module Toys ## # A ToolDefinition describes a single command that can be invoked using Toys. @@ -284,7 +282,7 @@ def reset_definition ## # Settings for this tool # - # @return [Toys::Tool::Settings] + # @return [Toys::ToolDefinition::Settings] # attr_reader :settings diff --git a/toys-core/lib/toys/utils/completion_engine.rb b/toys-core/lib/toys/utils/completion_engine.rb index d4c512b1..273a1a26 100644 --- a/toys-core/lib/toys/utils/completion_engine.rb +++ b/toys-core/lib/toys/utils/completion_engine.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "shellwords" - module Toys module Utils ## @@ -21,6 +19,7 @@ class Bash # @param cli [Toys::CLI] The CLI. # def initialize(cli) + require "shellwords" @cli = cli end diff --git a/toys-core/lib/toys/utils/exec.rb b/toys-core/lib/toys/utils/exec.rb index ce165260..4602f343 100644 --- a/toys-core/lib/toys/utils/exec.rb +++ b/toys-core/lib/toys/utils/exec.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -require "rbconfig" -require "logger" -require "shellwords" - module Toys module Utils ## @@ -266,6 +262,9 @@ class Exec # for a description of the options. # def initialize(**opts, &block) + require "rbconfig" + require "logger" + require "stringio" @default_opts = Opts.new(&block).add(opts) end diff --git a/toys-core/lib/toys/utils/gems.rb b/toys-core/lib/toys/utils/gems.rb index 0d5fd1e5..f2fd11b4 100644 --- a/toys-core/lib/toys/utils/gems.rb +++ b/toys-core/lib/toys/utils/gems.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require "monitor" -require "rubygems" module Toys module Utils @@ -123,6 +122,7 @@ def initialize(on_missing: nil, output: nil, suppress_confirm: nil, default_confirm: nil) + require "rubygems" @default_confirm = default_confirm || default_confirm.nil? ? true : false @on_missing = on_missing || if suppress_confirm diff --git a/toys-core/lib/toys/utils/git_cache.rb b/toys-core/lib/toys/utils/git_cache.rb index 0e0be47b..477cb784 100644 --- a/toys-core/lib/toys/utils/git_cache.rb +++ b/toys-core/lib/toys/utils/git_cache.rb @@ -1,12 +1,5 @@ # frozen_string_literal: true -require "digest" -require "fileutils" -require "json" -require "toys/compat" -require "toys/utils/exec" -require "toys/utils/xdg" - module Toys module Utils ## @@ -288,6 +281,11 @@ def initialize(base_dir, sha, git_path, path_data) # a specific directory in the user's XDG cache. # def initialize(cache_dir: nil) + require "digest" + require "fileutils" + require "json" + require "toys/compat" + require "toys/utils/exec" @cache_dir = ::File.expand_path(cache_dir || default_cache_dir) @exec = Utils::Exec.new(out: :capture, err: :capture) end @@ -485,6 +483,7 @@ def repo_base_dir_for(remote) end def default_cache_dir + require "toys/utils/xdg" ::File.join(XDG.new.cache_home, "toys", "git") end diff --git a/toys-core/lib/toys/utils/help_text.rb b/toys-core/lib/toys/utils/help_text.rb index bdd560ee..baa13aed 100644 --- a/toys-core/lib/toys/utils/help_text.rb +++ b/toys-core/lib/toys/utils/help_text.rb @@ -351,6 +351,7 @@ class HelpStringAssembler def initialize(tool, executable_name, delegates, subtools, search_term, show_source_path, separate_sources, indent, indent2, wrap_width, styled) require "toys/utils/terminal" + require "stringio" @tool = tool @executable_name = executable_name @delegates = delegates @@ -667,6 +668,7 @@ class ListStringAssembler def initialize(tool, subtools, recursive, search_term, separate_sources, indent, wrap_width, styled) require "toys/utils/terminal" + require "stringio" @tool = tool @subtools = subtools @recursive = recursive diff --git a/toys-core/lib/toys/utils/pager.rb b/toys-core/lib/toys/utils/pager.rb index ef688433..3bb7be98 100644 --- a/toys-core/lib/toys/utils/pager.rb +++ b/toys-core/lib/toys/utils/pager.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "toys/utils/exec" - module Toys module Utils ## @@ -150,7 +148,10 @@ def default_command # @private # def default_exec_service - @default_exec_service ||= Exec.new + @default_exec_service ||= begin + require "toys/utils/exec" + Utils::Exec.new + end end end end diff --git a/toys-core/lib/toys/utils/standard_ui.rb b/toys-core/lib/toys/utils/standard_ui.rb index d1b1f278..bbea547f 100644 --- a/toys-core/lib/toys/utils/standard_ui.rb +++ b/toys-core/lib/toys/utils/standard_ui.rb @@ -28,8 +28,9 @@ class StandardUI # terminal output. Default is `$stderr`. # def initialize(output: nil) - @terminal = output || $stderr + require "logger" require "toys/utils/terminal" + @terminal = output || $stderr @terminal = Terminal.new(output: @terminal) unless @terminal.is_a?(Terminal) @log_header_severity_styles = { "FATAL" => [:bright_magenta, :bold, :underline], diff --git a/toys-core/lib/toys/utils/terminal.rb b/toys-core/lib/toys/utils/terminal.rb index 3c79cfcf..0e779200 100644 --- a/toys-core/lib/toys/utils/terminal.rb +++ b/toys-core/lib/toys/utils/terminal.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require "stringio" -require "monitor" - begin require "io/console" rescue ::LoadError @@ -116,6 +113,7 @@ def self.remove_style_escapes(str) # setting is inferred from whether the output has a tty. # def initialize(input: $stdin, output: $stdout, styled: nil) + require "monitor" @input = input @output = output @styled = diff --git a/toys-core/lib/toys/utils/xdg.rb b/toys-core/lib/toys/utils/xdg.rb index b16d9e60..edfa0db9 100644 --- a/toys-core/lib/toys/utils/xdg.rb +++ b/toys-core/lib/toys/utils/xdg.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "toys/compat" - module Toys module Utils ## @@ -50,6 +48,8 @@ class XDG # you can omit this argument, as it will default to `::ENV`. # def initialize(env: ::ENV) + require "fileutils" + require "toys/compat" @env = env end diff --git a/toys-core/test/test_dsl.rb b/toys-core/test/test_dsl.rb index 8ccd4918..a1e23298 100644 --- a/toys-core/test/test_dsl.rb +++ b/toys-core/test/test_dsl.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require "helper" +require "logger" +require "stringio" describe Toys::DSL::Tool do let(:logger) { diff --git a/toys-core/test/utils/test_git_cache.rb b/toys-core/test/utils/test_git_cache.rb index c912c816..6438806b 100644 --- a/toys-core/test/utils/test_git_cache.rb +++ b/toys-core/test/utils/test_git_cache.rb @@ -3,6 +3,7 @@ require "helper" require "toys/utils/exec" require "toys/utils/git_cache" +require "digest" require "fileutils" require "net/http" require "uri" diff --git a/toys/builtins/system/.test/test_git_cache.rb b/toys/builtins/system/.test/test_git_cache.rb index 03192884..c45bb969 100644 --- a/toys/builtins/system/.test/test_git_cache.rb +++ b/toys/builtins/system/.test/test_git_cache.rb @@ -1,3 +1,5 @@ +require "fileutils" +require "json" require "psych" require "toys/utils/exec" require "toys/utils/git_cache" diff --git a/toys/builtins/system/.test/test_tools.rb b/toys/builtins/system/.test/test_tools.rb index bdcedf1e..a3d570a9 100644 --- a/toys/builtins/system/.test/test_tools.rb +++ b/toys/builtins/system/.test/test_tools.rb @@ -1,3 +1,4 @@ +require "json" require "psych" require "toys/utils/exec" diff --git a/toys/lib/toys/templates/rake.rb b/toys/lib/toys/templates/rake.rb index 90044583..ba53f362 100644 --- a/toys/lib/toys/templates/rake.rb +++ b/toys/lib/toys/templates/rake.rb @@ -164,12 +164,12 @@ def bundler_settings gem "rake", *template.gem_version require "rake" - rakefile_path = Templates::Rake.find_rakefile( + rakefile_path = ::Toys::Templates::Rake.find_rakefile( template.rakefile_path, template.context_directory || context_directory ) raise "Cannot find #{template.rakefile_path}" unless rakefile_path rake_context_dir = ::File.dirname(rakefile_path) - rake = Templates::Rake.prepare_rake(rakefile_path, rake_context_dir) + rake = ::Toys::Templates::Rake.prepare_rake(rakefile_path, rake_context_dir) rake.tasks.each do |task| comments = task.full_comment.to_s.split("\n") @@ -190,7 +190,7 @@ def bundler_settings if template.use_flags task.arg_names.each do |arg| - specs = Templates::Rake.flag_specs(arg) + specs = ::Toys::Templates::Rake.flag_specs(arg) flag(arg, *specs) unless specs.empty? end