Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Clean up some requires #241

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions toys-core/lib/toys/cli.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

require "rbconfig"
require "logger"
require "toys/completion"

module Toys
##
# A Toys-based CLI.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions toys-core/lib/toys/loader.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "monitor"

module Toys
##
# The Loader service loads tools from configuration files, and finds the
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

##
Expand Down
3 changes: 1 addition & 2 deletions toys-core/lib/toys/module_lookup.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "monitor"

module Toys
##
# A helper module that provides methods to do module lookups. This is
Expand Down Expand Up @@ -56,6 +54,7 @@ def path_to_module(path)
# Create an empty ModuleLookup
#
def initialize
require "monitor"
@mutex = ::Monitor.new
@paths = []
@paths_locked = false
Expand Down
18 changes: 8 additions & 10 deletions toys-core/lib/toys/settings.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "date"

module Toys
##
# A settings class defines the structure of application settings, i.e. the
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions toys-core/lib/toys/standard_middleware/show_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions toys-core/lib/toys/standard_mixins/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions toys-core/lib/toys/standard_mixins/fileutils.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "fileutils"

module Toys
module StandardMixins
##
Expand All @@ -20,6 +18,7 @@ module Fileutils
# @private
#
def self.included(mod)
require "fileutils"
mod.include(::FileUtils)
end
end
Expand Down
6 changes: 4 additions & 2 deletions toys-core/lib/toys/standard_mixins/gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion toys-core/lib/toys/standard_mixins/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 0 additions & 2 deletions toys-core/lib/toys/standard_mixins/xdg.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "fileutils"

module Toys
module StandardMixins
##
Expand Down
4 changes: 1 addition & 3 deletions toys-core/lib/toys/tool_definition.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "set"

module Toys
##
# A ToolDefinition describes a single command that can be invoked using Toys.
Expand Down Expand Up @@ -284,7 +282,7 @@ def reset_definition
##
# Settings for this tool
#
# @return [Toys::Tool::Settings]
# @return [Toys::ToolDefinition::Settings]
#
attr_reader :settings

Expand Down
3 changes: 1 addition & 2 deletions toys-core/lib/toys/utils/completion_engine.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "shellwords"

module Toys
module Utils
##
Expand All @@ -21,6 +19,7 @@ class Bash
# @param cli [Toys::CLI] The CLI.
#
def initialize(cli)
require "shellwords"
@cli = cli
end

Expand Down
7 changes: 3 additions & 4 deletions toys-core/lib/toys/utils/exec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

require "rbconfig"
require "logger"
require "shellwords"

module Toys
module Utils
##
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion toys-core/lib/toys/utils/gems.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "monitor"
require "rubygems"

module Toys
module Utils
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions toys-core/lib/toys/utils/git_cache.rb
Original file line number Diff line number Diff line change
@@ -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
##
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions toys-core/lib/toys/utils/help_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions toys-core/lib/toys/utils/pager.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "toys/utils/exec"

module Toys
module Utils
##
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion toys-core/lib/toys/utils/standard_ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
4 changes: 1 addition & 3 deletions toys-core/lib/toys/utils/terminal.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require "stringio"
require "monitor"

begin
require "io/console"
rescue ::LoadError
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions toys-core/lib/toys/utils/xdg.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "toys/compat"

module Toys
module Utils
##
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions toys-core/test/test_dsl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "helper"
require "logger"
require "stringio"

describe Toys::DSL::Tool do
let(:logger) {
Expand Down
1 change: 1 addition & 0 deletions toys-core/test/utils/test_git_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "helper"
require "toys/utils/exec"
require "toys/utils/git_cache"
require "digest"
require "fileutils"
require "net/http"
require "uri"
Expand Down
2 changes: 2 additions & 0 deletions toys/builtins/system/.test/test_git_cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "fileutils"
require "json"
require "psych"
require "toys/utils/exec"
require "toys/utils/git_cache"
Expand Down
1 change: 1 addition & 0 deletions toys/builtins/system/.test/test_tools.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "json"
require "psych"
require "toys/utils/exec"

Expand Down
Loading