From adf90cc0de0ff72c1d1456e62a6d2ea79273f31f Mon Sep 17 00:00:00 2001 From: Christoph Olszowka Date: Tue, 30 Jun 2020 22:33:20 +0200 Subject: [PATCH 1/3] Add basic about command for fetching info about specific gem(s) --- lib/bundler/toolbox/cli.rb | 20 ++++++++++++++++++++ spec/bundler/toolbox/cli_spec.rb | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/bundler/toolbox/cli.rb b/lib/bundler/toolbox/cli.rb index d635e2b..ef2dc9c 100644 --- a/lib/bundler/toolbox/cli.rb +++ b/lib/bundler/toolbox/cli.rb @@ -52,7 +52,27 @@ def call(info: false, **) end end + class About < Dry::CLI::Command + desc "Get information about given gem(s)" + + argument :gems, type: :array, + required: true, + desc: "Name(s) of gems to print information about" + + option :fixtures, type: :boolean, + default: false, + desc: "For testing purposues: Do not make actual API calls, use local fixtures" + + def call(gems:, fixtures:, **) + Bundler::Toolbox.compare(*gems, fixtures: fixtures).each do |project| + puts project.name + puts project.description + end + end + end + register "version", Version, aliases: ["v", "-v", "--version"] + register "about", About, aliases: ["a"] end end end diff --git a/spec/bundler/toolbox/cli_spec.rb b/spec/bundler/toolbox/cli_spec.rb index e2fafc1..5c24557 100644 --- a/spec/bundler/toolbox/cli_spec.rb +++ b/spec/bundler/toolbox/cli_spec.rb @@ -78,4 +78,12 @@ def invoke(*args) .to output(/Execution environment: #{described_class.execution_environment}/).to_stdout end end + + describe "About Command" do + it "prints information about requested gem" do + expect { invoke "about", "simplecov", "--fixtures" } + .to output(/^simplecov/) + .to_stdout + end + end end From 68531ad91a5e1ec6512516cd9302b8e05ac690a0 Mon Sep 17 00:00:00 2001 From: Christoph Olszowka Date: Tue, 30 Jun 2020 23:14:29 +0200 Subject: [PATCH 2/3] Add formatted output to about gem command --- Gemfile.lock | 2 ++ bundler-toolbox.gemspec | 2 ++ lib/bundler/toolbox/cli.rb | 18 ++++++++++++++++-- spec/bundler/toolbox/cli_spec.rb | 2 +- templates/about.liquid | 30 ++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 templates/about.liquid diff --git a/Gemfile.lock b/Gemfile.lock index c33363d..6ca406c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: bundler-toolbox (0.1.0) dry-cli (~> 0.6.0) + liquid rubytoolbox-api (>= 0.2.0) GEM @@ -69,6 +70,7 @@ GEM guard-rubocop (1.3.0) guard (~> 2.0) rubocop (~> 0.20) + liquid (4.0.3) listen (3.2.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) diff --git a/bundler-toolbox.gemspec b/bundler-toolbox.gemspec index 93dc7bd..416e90d 100644 --- a/bundler-toolbox.gemspec +++ b/bundler-toolbox.gemspec @@ -31,6 +31,8 @@ Gem::Specification.new do |spec| spec.add_dependency "dry-cli", "~> 0.6.0" spec.add_dependency "rubytoolbox-api", ">= 0.2.0" + spec.add_dependency "liquid" + spec.add_development_dependency "rubocop" spec.add_development_dependency "rubocop-performance" spec.add_development_dependency "rubocop-rspec" diff --git a/lib/bundler/toolbox/cli.rb b/lib/bundler/toolbox/cli.rb index ef2dc9c..d65b061 100644 --- a/lib/bundler/toolbox/cli.rb +++ b/lib/bundler/toolbox/cli.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "dry/cli" +require "liquid" module Bundler module Toolbox @@ -65,10 +66,23 @@ class About < Dry::CLI::Command def call(gems:, fixtures:, **) Bundler::Toolbox.compare(*gems, fixtures: fixtures).each do |project| - puts project.name - puts project.description + puts format_project(project) end end + + private + + def template_path(filename) + File.join(__dir__, "..", "..", "..", "templates", filename) + end + + def template + @template ||= Liquid::Template.parse File.read(template_path("about.liquid")) + end + + def format_project(project) + template.render(project.to_h) + end end register "version", Version, aliases: ["v", "-v", "--version"] diff --git a/spec/bundler/toolbox/cli_spec.rb b/spec/bundler/toolbox/cli_spec.rb index 5c24557..6bdd18d 100644 --- a/spec/bundler/toolbox/cli_spec.rb +++ b/spec/bundler/toolbox/cli_spec.rb @@ -82,7 +82,7 @@ def invoke(*args) describe "About Command" do it "prints information about requested gem" do expect { invoke "about", "simplecov", "--fixtures" } - .to output(/^simplecov/) + .to output(/Code coverage for Ruby with a powerful configuration library/) .to_stdout end end diff --git a/templates/about.liquid b/templates/about.liquid new file mode 100644 index 0000000..72a596d --- /dev/null +++ b/templates/about.liquid @@ -0,0 +1,30 @@ +# {{ name }} | Score {{ score }} + +{% if description %}*{{ description }}*{% endif %} + +{% if categories %} +## Categories + +{% for category in categories %} +### **{{ category.name }}** [{{ category.urls.toolbox_url }}]({{ category.urls.toolbox_url }}) + +*{{ category.description }}* +{% endfor %} +{% endif %} + +{% if rubygem %} +## Rubygem [{{ rubygem.name }}]({{ rubygem.url}}) + + * **Downloads** {{ rubygem.stats.downloads }} + * **Reverse Dependencies** {{ rubygem.stats.reverse_dependencies_count }} + * **Total Releases** {{ rubygem.stats.releases_count }} +{% endif %} + + +{% if github_repo %} +## Github Repo [{{ github_repo.path }}]({{ github_repo.url}}) + + * **Stargazers** {{ github_repo.stats.stargazers_count }} + * **Forks** {{ github_repo.stats.forks_count }} + * **Watchers** {{ github_repo.stats.watchers_count }} +{% endif %} From 9ad3565486ae165a74667a73389cb16256ec3592 Mon Sep 17 00:00:00 2001 From: Christoph Olszowka Date: Tue, 30 Jun 2020 23:51:26 +0200 Subject: [PATCH 3/3] Utilize rainbow for styling about output --- Gemfile.lock | 1 + bundler-toolbox.gemspec | 1 + lib/bundler/toolbox/cli.rb | 21 ++++++++++++++++++++- spec/bundler/toolbox/cli_spec.rb | 4 ++++ templates/about.liquid | 12 ++++++------ 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6ca406c..8ce914c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,6 +4,7 @@ PATH bundler-toolbox (0.1.0) dry-cli (~> 0.6.0) liquid + rainbow (>= 3.0.0) rubytoolbox-api (>= 0.2.0) GEM diff --git a/bundler-toolbox.gemspec b/bundler-toolbox.gemspec index 416e90d..aabf6da 100644 --- a/bundler-toolbox.gemspec +++ b/bundler-toolbox.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |spec| spec.add_dependency "rubytoolbox-api", ">= 0.2.0" spec.add_dependency "liquid" + spec.add_dependency "rainbow", ">= 3.0.0" spec.add_development_dependency "rubocop" spec.add_development_dependency "rubocop-performance" diff --git a/lib/bundler/toolbox/cli.rb b/lib/bundler/toolbox/cli.rb index d65b061..30e9ade 100644 --- a/lib/bundler/toolbox/cli.rb +++ b/lib/bundler/toolbox/cli.rb @@ -2,6 +2,7 @@ require "dry/cli" require "liquid" +require "rainbow" module Bundler module Toolbox @@ -54,6 +55,24 @@ def call(info: false, **) end class About < Dry::CLI::Command + module RainbowFilter + def color(input, color) + Rainbow(input).color(color.to_sym) + end + + def bg(input, color) + Rainbow(input).background(color.to_sym) + end + + def bold(input) + Rainbow(input).bright + end + + def underline(input) + Rainbow(input).underline + end + end + desc "Get information about given gem(s)" argument :gems, type: :array, @@ -81,7 +100,7 @@ def template end def format_project(project) - template.render(project.to_h) + template.render project.to_h, filters: [RainbowFilter] end end diff --git a/spec/bundler/toolbox/cli_spec.rb b/spec/bundler/toolbox/cli_spec.rb index 6bdd18d..ed0a8a7 100644 --- a/spec/bundler/toolbox/cli_spec.rb +++ b/spec/bundler/toolbox/cli_spec.rb @@ -85,5 +85,9 @@ def invoke(*args) .to output(/Code coverage for Ruby with a powerful configuration library/) .to_stdout end + + it "debug" do + invoke "about", "simplecov", "--fixtures" + end end end diff --git a/templates/about.liquid b/templates/about.liquid index 72a596d..66f39b6 100644 --- a/templates/about.liquid +++ b/templates/about.liquid @@ -1,14 +1,14 @@ -# {{ name }} | Score {{ score }} +{{ name | color: "red" | bold }} | Score {{ score }} -{% if description %}*{{ description }}*{% endif %} +{% if description %}{{ description | color: "darkgray" }}{% endif %} {% if categories %} -## Categories - +{{ "Categories" | bold }} +========== {% for category in categories %} -### **{{ category.name }}** [{{ category.urls.toolbox_url }}]({{ category.urls.toolbox_url }}) +{{ category.name | bold }} {{ category.urls.toolbox_url | color: "darkgray" | underline }} -*{{ category.description }}* +{{ category.description | color: "darkgray" }} {% endfor %} {% endif %}