Merge pull request #1839 from pry/platform-refactoring

Deprecate Pry::Platform and introduce Pry::Helpers::Platform
This commit is contained in:
Kyrylo Silin 2018-11-02 01:57:47 +08:00 committed by GitHub
commit 86bbfde61e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 206 additions and 170 deletions

View File

@ -4,7 +4,7 @@ class Pry::Command::ClearScreen < Pry::ClassCommand
description 'Clear the contents of the screen/window Pry is running in.'
def process
if windows?
if Helpers::Platform.windows?
_pry_.config.system.call(_pry_.output, 'cls', _pry_)
else
_pry_.config.system.call(_pry_.output, 'clear', _pry_)

View File

@ -95,7 +95,7 @@ TEXT
\____/ \________________________|
EOS
if windows_ansi?
if Helpers::Platform.windows_ansi?
move_up = proc { |n| "\e[#{n}F" }
else
move_up = proc { |n| "\e[#{n}A\e[0G" }

View File

@ -64,7 +64,7 @@ class Pry
if Object.respond_to?(:deprecate_constant)
opt.on :d, :dconstants, "Show deprecated constants"
end
if jruby?
if Helpers::Platform.jruby?
opt.on :J, "all-java", "Show all the aliases for methods from java (default is to show only prettiest)"
end
end

View File

@ -4,7 +4,7 @@ class Pry
class Command::Ls < Pry::ClassCommand
class Constants < Pry::Command::Ls::Formatter
DEPRECATED_CONSTANTS = [:Data, :Fixnum, :Bignum, :TimeoutError, :NIL, :FALSE, :TRUE]
DEPRECATED_CONSTANTS << :JavaPackageModuleTemplate if Pry::Helpers::BaseHelpers.jruby?
DEPRECATED_CONSTANTS << :JavaPackageModuleTemplate if Helpers::Platform.jruby?
include Pry::Command::Ls::Interrogatable
def initialize(interrogatee, no_user_opts, opts, _pry_)

View File

@ -14,7 +14,7 @@ module Pry::Command::Ls::MethodsHelper
Pry::Method.all_from_obj(@interrogatee)
end
if Pry::Helpers::BaseHelpers.jruby? && !@jruby_switch
if Pry::Helpers::Platform.jruby? && !@jruby_switch
methods = trim_jruby_aliases(methods)
end

View File

@ -52,7 +52,7 @@ class Pry
},
editor: proc {
Pry.default_editor_for_platform
}, # TODO: Pry::Platform.editor
},
should_load_rc: proc {
true
},
@ -60,7 +60,7 @@ class Pry
true
},
should_trap_interrupts: proc {
Pry::Platform.jruby?
Pry::Helpers::Platform.jruby?
},
disable_auto_reload: proc {
false

View File

@ -81,7 +81,7 @@ class Object
# This fixes the following two spec failures, at https://travis-ci.org/pry/pry/jobs/274470002
# 1) ./spec/pry_spec.rb:360:in `block in (root)'
# 2) ./spec/pry_spec.rb:366:in `block in (root)'
return class_eval {binding} if Pry::Helpers::BaseHelpers.jruby? and self.name == nil
return class_eval {binding} if Pry::Helpers::Platform.jruby? and self.name == nil
# class_eval sets both self and the default definee to this class.
return class_eval("binding")

View File

@ -1,6 +1,5 @@
class Pry
class Editor
include Pry::Helpers::BaseHelpers
include Pry::Helpers::CommandHelpers
attr_reader :_pry_
@ -25,7 +24,7 @@ class Pry
editor_invocation = build_editor_invocation_string(file, line, blocking)
return nil unless editor_invocation
if jruby?
if Helpers::Platform.jruby?
open_editor_on_jruby(editor_invocation)
else
open_editor(editor_invocation)
@ -43,7 +42,7 @@ class Pry
args = [file, line, blocking][0...(_pry_.config.editor.arity)]
_pry_.config.editor.call(*args)
else
sanitized_file = windows? ? file : Shellwords.escape(file)
sanitized_file = Helpers::Platform.windows? ? file : Shellwords.escape(file)
"#{_pry_.config.editor} #{blocking_flag_for_editor(blocking)} #{start_line_syntax_for_editor(sanitized_file, line)}"
end
end
@ -104,7 +103,7 @@ class Pry
when /^redcar/
"-l#{line_number} #{file_name}"
else
if windows?
if Helpers::Platform.windows?
"#{file_name}"
else
"+#{line_number} #{file_name}"

View File

@ -3,3 +3,4 @@ require "pry/helpers/options_helpers"
require "pry/helpers/command_helpers"
require "pry/helpers/text"
require "pry/helpers/table"
require "pry/helpers/platform"

View File

@ -1,6 +1,5 @@
module Pry::Helpers; end
module Pry::Helpers::BaseHelpers
include Pry::Platform
extend self
def silence_warnings
@ -42,7 +41,7 @@ module Pry::Helpers::BaseHelpers
end
def use_ansi_codes?
windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
Pry::Helpers::Platform.windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
end
def colorize_code(code)

View File

@ -0,0 +1,58 @@
class Pry
module Helpers
# Contains methods for querying the platform that Pry is running on
# @api public
# @since v0.12.0
# rubocop:disable Style/DoubleNegation
module Platform
# @return [Boolean]
def self.mac_osx?
!!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
end
# @return [Boolean]
def self.linux?
!!(RbConfig::CONFIG['host_os'] =~ /linux/i)
end
# @return [Boolean] true when Pry is running on Windows with ANSI support,
# false otherwise
def self.windows?
!!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
end
# @return [Boolean]
def self.windows_ansi?
return false unless windows?
!!(defined?(Win32::Console) || ENV['ANSICON'] || mri_2?)
end
# @return [Boolean]
def self.jruby?
RbConfig::CONFIG['ruby_install_name'] == 'jruby'
end
# @return [Boolean]
def self.jruby_19?
jruby? && RbConfig::CONFIG['ruby_version'] == '1.9'
end
# @return [Boolean]
def self.mri?
RbConfig::CONFIG['ruby_install_name'] == 'ruby'
end
# @return [Boolean]
def self.mri_19?
!!(mri? && RUBY_VERSION.start_with?('1.9'))
end
# @return [Boolean]
def self.mri_2?
!!(mri? && RUBY_VERSION.start_with?('2'))
end
end
# rubocop:enable Style/DoubleNegation
end
end

View File

@ -395,7 +395,7 @@ class Pry
cols = Terminal.width!
lines = cols == 0 ? 1 : (line_to_measure.length / cols + 1).to_i
if Pry::Helpers::BaseHelpers.windows_ansi?
if Helpers::Platform.windows_ansi?
move_up = "\e[#{lines}F"
move_down = "\e[#{lines}E"
else

View File

@ -51,7 +51,7 @@ class Pry::Pager
def best_available
if !_pry_.config.pager
NullPager.new(_pry_.output)
elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby?
elsif !SystemPager.available? || Helpers::Platform.jruby?
SimplePager.new(_pry_.output)
else
SystemPager.new(_pry_.output)
@ -139,7 +139,7 @@ class Pry::Pager
if @system_pager.nil?
@system_pager = begin
pager_executable = default_pager.split(' ').first
if Pry::Helpers::BaseHelpers.windows? || Pry::Helpers::BaseHelpers.windows_ansi?
if Helpers::Platform.windows? || Helpers::Platform.windows_ansi?
`where /Q #{pager_executable}`
else
`which #{pager_executable}`

View File

@ -1,94 +1,99 @@
module Pry::Platform
extend self
class Pry
module Platform
extend self
#
# @return [Boolean]
# Returns true if Pry is running on Mac OSX.
#
# @note
# Queries RbConfig::CONFIG['host_os'] with a best guess.
#
def mac_osx?
!!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
#
# @return [Boolean]
# Returns true if Pry is running on Mac OSX.
#
# @note
# Queries RbConfig::CONFIG['host_os'] with a best guess.
#
def mac_osx?
!!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
end
#
# @return [Boolean]
# Returns true if Pry is running on Linux.
#
# @note
# Queries RbConfig::CONFIG['host_os'] with a best guess.
#
def linux?
!!(RbConfig::CONFIG['host_os'] =~ /linux/i)
end
#
# @return [Boolean]
# Returns true if Pry is running on Windows.
#
# @note
# Queries RbConfig::CONFIG['host_os'] with a best guess.
#
def windows?
!!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
end
#
# @return [Boolean]
# Returns true when Pry is running on Windows with ANSI support.
#
def windows_ansi?
return false if not windows?
!!(defined?(Win32::Console) or ENV['ANSICON'] or mri_2?)
end
#
# @return [Boolean]
# Returns true when Pry is being run from JRuby.
#
def jruby?
RbConfig::CONFIG['ruby_install_name'] == 'jruby'
end
#
# @return [Boolean]
# Returns true when Pry is being run from JRuby in 1.9 mode.
#
def jruby_19?
jruby? and RbConfig::CONFIG['ruby_version'] == '1.9'
end
#
# @return [Boolean]
# Returns true when Pry is being run from MRI (CRuby).
#
def mri?
RbConfig::CONFIG['ruby_install_name'] == 'ruby'
end
#
# @return [Boolean]
# Returns true when Pry is being run from MRI v1.9+ (CRuby).
#
def mri_19?
!!(mri? and RUBY_VERSION =~ /\A1\.9/)
end
#
# @return [Boolean]
# Returns true when Pry is being run from MRI v2+ (CRuby).
#
def mri_2?
!!(mri? and RUBY_VERSION =~ /\A2/)
end
#
# @return [Array<Symbol>]
# Returns an Array of Ruby engines that Pry is known to run on.
#
def known_engines
[:jruby, :mri]
end
end
#
# @return [Boolean]
# Returns true if Pry is running on Linux.
#
# @note
# Queries RbConfig::CONFIG['host_os'] with a best guess.
#
def linux?
!!(RbConfig::CONFIG['host_os'] =~ /linux/i)
end
#
# @return [Boolean]
# Returns true if Pry is running on Windows.
#
# @note
# Queries RbConfig::CONFIG['host_os'] with a best guess.
#
def windows?
!!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
end
#
# @return [Boolean]
# Returns true when Pry is running on Windows with ANSI support.
#
def windows_ansi?
return false if not windows?
!!(defined?(Win32::Console) or ENV['ANSICON'] or mri_2?)
end
#
# @return [Boolean]
# Returns true when Pry is being run from JRuby.
#
def jruby?
RbConfig::CONFIG['ruby_install_name'] == 'jruby'
end
#
# @return [Boolean]
# Returns true when Pry is being run from JRuby in 1.9 mode.
#
def jruby_19?
jruby? and RbConfig::CONFIG['ruby_version'] == '1.9'
end
#
# @return [Boolean]
# Returns true when Pry is being run from MRI (CRuby).
#
def mri?
RbConfig::CONFIG['ruby_install_name'] == 'ruby'
end
#
# @return [Boolean]
# Returns true when Pry is being run from MRI v1.9+ (CRuby).
#
def mri_19?
!!(mri? and RUBY_VERSION =~ /\A1\.9/)
end
#
# @return [Boolean]
# Returns true when Pry is being run from MRI v2+ (CRuby).
#
def mri_2?
!!(mri? and RUBY_VERSION =~ /\A2/)
end
#
# @return [Array<Symbol>]
# Returns an Array of Ruby engines that Pry is known to run on.
#
def known_engines
[:jruby, :mri]
end
# Not supported on MRI 2.2 and lower.
deprecate_constant(:Platform) if respond_to?(:deprecate_constant)
end

View File

@ -157,7 +157,7 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
load_requires if Pry.config.should_load_requires
load_history if Pry.config.history.should_load
load_traps if Pry.config.should_trap_interrupts
load_win32console if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi?
load_win32console if Helpers::Platform.windows? && !Helpers::Platform.windows_ansi?
end
# Start a Pry REPL.
@ -296,13 +296,10 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
def self.default_editor_for_platform
return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty?
return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty?
return 'notepad' if Helpers::Platform.windows?
if Helpers::BaseHelpers.windows?
'notepad'
else
%w(editor nano vi).detect do |editor|
system("which #{editor} > /dev/null 2>&1")
end
%w(editor nano vi).detect do |editor|
system("which #{editor} > /dev/null 2>&1")
end
end

View File

@ -332,7 +332,7 @@ class Pry
# This workaround has a side effect: java exceptions specified
# in `Pry.config.exception_whitelist` are ignored.
jruby_exceptions = []
if Pry::Helpers::BaseHelpers.jruby?
if Helpers::Platform.jruby?
jruby_exceptions << Java::JavaLang::Exception
end
@ -347,7 +347,7 @@ class Pry
# Eliminate following warning:
# warning: singleton on non-persistent Java type X
# (http://wiki.jruby.org/Persistence)
if Pry::Helpers::BaseHelpers.jruby? && e.class.respond_to?('__persistent__')
if Helpers::Platform.jruby? && e.class.respond_to?('__persistent__')
e.class.__persistent__ = true
end
self.last_exception = e

View File

@ -49,7 +49,7 @@ class Pry
# Clear the line before starting Pry. This fixes issue #566.
if pry.config.correct_indent
Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
Kernel.print(Helpers::Platform.windows_ansi? ? "\e[0F" : "\e[0G")
end
end
@ -220,7 +220,7 @@ class Pry
def piping?
return false unless $stdout.respond_to?(:tty?)
!$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows?
!$stdout.tty? && $stdin.tty? && !Helpers::Platform.windows?
end
# @return [void]

View File

@ -41,7 +41,7 @@ class Pry::Terminal
end
def screen_size_according_to_io_console
return if Pry::Helpers::BaseHelpers.jruby?
return if Pry::Helpers::Platform.jruby?
begin
require 'io/console'

View File

@ -136,7 +136,7 @@ class Pry
def singleton_instance
raise ArgumentError, "tried to get instance of non singleton class" unless singleton_class?
if Helpers::BaseHelpers.jruby?
if Helpers::Platform.jruby?
wrapped.to_java.attached
else
@singleton_instance ||= ObjectSpace.each_object(wrapped).detect{ |x| (class << x; self; end) == wrapped }
@ -251,7 +251,7 @@ class Pry
y.yield candidate(num)
end
end
Pry::Helpers::BaseHelpers.jruby_19? ? enum.to_a : enum
Helpers::Platform.jruby_19? ? enum.to_a : enum
end
# @return [Boolean] Whether YARD docs are available for this module.

View File

@ -20,7 +20,7 @@ describe "edit" do
# OS-specific tempdir name. For GNU/Linux it's "tmp", for Windows it's
# something "Temp".
@tf_dir =
if Pry::Helpers::BaseHelpers.mri_19?
if Pry::Helpers::Platform.mri_19?
Pathname.new(Dir::Tmpname.tmpdir)
else
Pathname.new(Dir.tmpdir)

View File

@ -238,17 +238,15 @@ describe "ls" do
end
end
if Pry::Helpers::BaseHelpers.jruby?
describe 'on java objects' do
it 'should omit java-esque aliases by default' do
expect(pry_eval('ls java.lang.Thread.current_thread')).to match(/\bthread_group\b/)
expect(pry_eval('ls java.lang.Thread.current_thread')).not_to match(/\bgetThreadGroup\b/)
end
describe 'on java objects', skip: !Pry::Helpers::Platform.jruby? do
it 'should omit java-esque aliases by default' do
expect(pry_eval('ls java.lang.Thread.current_thread')).to match(/\bthread_group\b/)
expect(pry_eval('ls java.lang.Thread.current_thread')).not_to match(/\bgetThreadGroup\b/)
end
it 'should include java-esque aliases if requested' do
expect(pry_eval('ls java.lang.Thread.current_thread -J')).to match(/\bthread_group\b/)
expect(pry_eval('ls java.lang.Thread.current_thread -J')).to match(/\bgetThreadGroup\b/)
end
it 'should include java-esque aliases if requested' do
expect(pry_eval('ls java.lang.Thread.current_thread -J')).to match(/\bthread_group\b/)
expect(pry_eval('ls java.lang.Thread.current_thread -J')).to match(/\bgetThreadGroup\b/)
end
end
end

View File

@ -27,11 +27,9 @@ describe Pry::InputCompleter do
Object.remove_const :SymbolyName
end
# another jruby hack :((
if !Pry::Helpers::BaseHelpers.jruby?
it "should not crash if there's a Module that has a symbolic name." do
expect { Pry::InputCompleter.new(Readline).call "a.to_s.", target: Pry.binding_for(Object.new) }.not_to raise_error
end
it "should not crash if there's a Module that has a symbolic name." do
skip unless Pry::Helpers::Platform.jruby?
expect { Pry::InputCompleter.new(Readline).call "a.to_s.", target: Pry.binding_for(Object.new) }.not_to raise_error
end
it 'should take parenthesis and other characters into account for symbols' do
@ -225,10 +223,12 @@ describe Pry::InputCompleter do
completer_test(self, nil, false).call("[].size.parse_printf_format")
end
if !Pry::Helpers::BaseHelpers.jruby?
unless Pry::Helpers::Platform.jruby?
# Classes that override .hash are still hashable in JRuby, for some reason.
it 'ignores methods from modules that override Object#hash incompatibly' do
_m = Module.new do
# skip unless Pry::Helpers::Platform.jruby?
m = Module.new do
def self.hash(a, b)
end
@ -236,7 +236,7 @@ describe Pry::InputCompleter do
end
end
completer_test(self, nil, false).call("[].size.aaaa")
completer_test(m, nil, false).call("[].size.aaaa")
end
end
end

View File

@ -10,7 +10,7 @@ describe Pry::Editor do
# OS-specific tempdir name. For GNU/Linux it's "tmp", for Windows it's
# something "Temp".
@tf_dir =
if Pry::Helpers::BaseHelpers.mri_19?
if Pry::Helpers::Platform.mri_19?
Pathname.new(Dir::Tmpname.tmpdir)
else
Pathname.new(Dir.tmpdir)
@ -21,26 +21,16 @@ describe Pry::Editor do
@editor = Pry::Editor.new(Pry.new)
end
unless Pry::Helpers::BaseHelpers.windows?
describe "build_editor_invocation_string" do
it 'should shell-escape files' do
invocation_str = @editor.build_editor_invocation_string(@tf_path, 5, true)
expect(invocation_str).to match(/#@tf_dir.+hello\\ world\.rb/)
end
describe "build_editor_invocation_string", skip: !Pry::Helpers::Platform.windows? do
it 'should shell-escape files' do
invocation_str = @editor.build_editor_invocation_string(@tf_path, 5, true)
expect(invocation_str).to match(/#@tf_dir.+hello\\ world\.rb/)
end
end
describe "build_editor_invocation_string on windows" do
before do
class Pry::Editor
def windows?; true; end
end
end
after do
class Pry::Editor
undef windows?
end
allow(Pry::Helpers::Platform).to receive(:windows?).and_return(true)
end
it "should not shell-escape files" do

View File

@ -43,17 +43,6 @@ RSpec.configure do |config|
config.include Pry::Testable::Utility
include Pry::Testable::Evalable
include Pry::Testable::Variables
# Optionally skip a test on specific Ruby engine(s).
# Please use this feature sparingly! It is better that a feature works than not.
# Inapplicable features are OK.
config.before(:each) do |example|
Pry::Platform.known_engines.each do |engine|
example.metadata[:expect_failure].to_a.include?(engine) and
Pry::Platform.public_send(:"#{engine}?") and
skip("This spec is failing or inapplicable on #{engine}.")
end
end
end
puts "Ruby v#{RUBY_VERSION} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"}), Pry v#{Pry::VERSION}, method_source v#{MethodSource::VERSION}, CodeRay v#{CodeRay::VERSION}, Pry::Slop v#{Pry::Slop::VERSION}"

View File

@ -369,9 +369,9 @@ describe Pry do
it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
# JRuby behaves different than CRuby here (seems it always has to some extent, see 'unless' below).
# It didn't seem trivial to work around. Skip for now.
skip "JRuby incompatibility" if Pry::Helpers::BaseHelpers.jruby?
skip "JRuby incompatibility" if Pry::Helpers::Platform.jruby?
[:test, 0, true, false, nil,
(0.0 unless Pry::Helpers::BaseHelpers.jruby?)].each do |val|
(0.0 unless Pry::Helpers::Platform.jruby?)].each do |val|
pry_eval(val, "def hello; end");
expect(val.class.instance_methods(false).map(&:to_sym).include?(:hello)).to eq true
end

View File

@ -26,7 +26,7 @@ describe Pry do
end
# Resolving symlinks doesn't work on jruby 1.9 [jruby issue #538]
unless Pry::Helpers::BaseHelpers.jruby_19?
unless Pry::Helpers::Platform.jruby_19?
it "should not load the rc file twice if it's symlinked differently" do
Pry::HOME_RC_FILE.replace "spec/fixtures/testrc"
Pry::LOCAL_RC_FILE.replace "spec/fixtures/testlinkrc"