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

[Collection] Simple colorize collection text like Bundler #1558

Merged
merged 3 commits into from
Oct 17, 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
1 change: 1 addition & 0 deletions lib/rbs/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'yaml'
require 'bundler'

require_relative './collection/colored_io'
require_relative './collection/sources'
require_relative './collection/config'
require_relative './collection/config/lockfile'
Expand Down
41 changes: 41 additions & 0 deletions lib/rbs/collection/colored_io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module RBS
module Collection
class ColoredIO
attr_reader :stdout

def initialize(stdout:)
@stdout = stdout
end

def puts_green(string)
if can_display_colors?
puts "\e[32m#{string}\e[m"
else
puts string
end
end

def puts(string)
stdout.puts(string)
end

private

# https://github.com/rubygems/rubygems/blob/ed65279100234a17d65d71fe26de5083984ac5b8/bundler/lib/bundler/vendor/thor/lib/thor/shell/color.rb#L99-L109
def can_display_colors?
are_colors_supported? && !are_colors_disabled?
end

def are_colors_supported?
stdout.tty? && ENV["TERM"] != "dumb"
end

def are_colors_disabled?
!ENV['NO_COLOR'].nil? && !ENV.fetch('NO_COLOR', '').empty?
end
end
private_constant :ColoredIO
end
end
2 changes: 1 addition & 1 deletion lib/rbs/collection/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def install_from_lockfile
stdout: stdout
)
end
stdout.puts "It's done! #{selected.size} gems' RBSs now installed."
ColoredIO.new(stdout: stdout).puts_green("It's done! #{selected.size} gems' RBSs now installed.")
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/rbs/collection/sources/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,25 @@ def install(dest:, name:, version:, stdout:)

gem_dir = dest.join(name, version)

colored_io = ColoredIO.new(stdout: stdout)

case
when gem_dir.symlink?
stdout.puts "Updating to #{format_config_entry(name, version)} from a local source"
colored_io.puts_green("Updating to #{format_config_entry(name, version)} from a local source")
gem_dir.unlink
_install(dest: dest, name: name, version: version)
when gem_dir.directory?
prev = load_metadata(dir: gem_dir)

if prev == metadata_content(name: name, version: version)
stdout.puts "Using #{format_config_entry(name, version)}"
colored_io.puts "Using #{format_config_entry(name, version)}"
else
stdout.puts "Updating to #{format_config_entry(name, version)} from #{format_config_entry(prev["name"], prev["version"])}"
colored_io.puts_green("Updating to #{format_config_entry(name, version)} from #{format_config_entry(prev["name"], prev["version"])}")
FileUtils.remove_entry_secure(gem_dir.to_s)
_install(dest: dest, name: name, version: version)
end
when !gem_dir.exist?
stdout.puts "Installing #{format_config_entry(name, version)}"
colored_io.puts_green("Installing #{format_config_entry(name, version)}")
_install(dest: dest, name: name, version: version)
else
raise
Expand Down
12 changes: 7 additions & 5 deletions lib/rbs/collection/sources/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Local
include Base

attr_reader :path, :full_path

def initialize(path:, base_directory:)
# TODO: resolve relative path from dir of rbs_collection.yaml
@path = Pathname(path)
Expand All @@ -33,22 +33,24 @@ def install(dest:, name:, version:, stdout:)
from = @full_path.join(name, version)
gem_dir = dest.join(name, version)

colored_io = ColoredIO.new(stdout: stdout)

case
when gem_dir.symlink? && gem_dir.readlink == from
stdout.puts "Using #{name}:#{version} (#{from})"
colored_io.puts "Using #{name}:#{version} (#{from})"
when gem_dir.symlink?
prev = gem_dir.readlink
gem_dir.unlink
_install(from, dest.join(name, version))
stdout.puts "Updating #{name}:#{version} to #{from} from #{prev}"
colored_io.puts_green("Updating #{name}:#{version} to #{from} from #{prev}")
when gem_dir.directory?
# TODO: Show version of git source
FileUtils.remove_entry_secure(gem_dir.to_s)
_install(from, dest.join(name, version))
stdout.puts "Updating #{name}:#{version} from git source"
colored_io.puts_green("Updating #{name}:#{version} from git source")
when !gem_dir.exist?
_install(from, dest.join(name, version))
stdout.puts "Installing #{name}:#{version} (#{from})"
colored_io.puts_green("Installing #{name}:#{version} (#{from})")
else
raise
end
Expand Down
2 changes: 2 additions & 0 deletions sig/cli.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module RBS
interface _IO
def puts: (*untyped) -> void

def tty?: () -> bool

def print: (*untyped) -> void

def flush: () -> void
Expand Down
14 changes: 14 additions & 0 deletions sig/collection/colored_io.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module RBS
module Collection
class ColoredIO
attr_reader stdout: CLI::_IO
def initialize: (stdout: CLI::_IO) -> void
def puts_green: (String) -> void
def puts: (String) -> void

private def can_display_colors?: () -> bool
private def are_colors_supported?: () -> bool
private def are_colors_disabled?: () -> bool
end
end
end
2 changes: 1 addition & 1 deletion test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def test_collection_install_gemspec

stdout, _ = run_rbs("collection", "install", bundler: true)

assert_match(/^Installing ast:(\d(\.\d)*)/, stdout)
assert_match(/Installing ast:(\d(\.\d)*)/, stdout)
refute_match(/^Using hola:(\d(\.\d)*)/, stdout)

assert dir.join('rbs_collection.lock.yaml').exist?
Expand Down