Sha256: 3cac0d367fd67a67126ee95422af51d101af0a9f0a445c53f834842026e21187

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require "optparse"
require "lapidarius/version"
require "lapidarius/cutter"
require "lapidarius/renderer"

module Lapidarius
  class CLI
    class NoGemError < ArgumentError; end

    def initialize(args, io = STDOUT)
      @args = args
      @io = io
      @gem = nil
      @recursive = nil
    end

    def call(cmd_klass = Command)
      parser.parse!(@args)
      fail NoGemError, "please specify the name of a gem: '-g gem_name'" unless @gem
      gem = cutter(cmd_klass).call
      renderer(gem).call(@io)
    rescue Gem::NotInstalledError => e
      @io.puts e.message.sub("specified", @gem)
    end

    private def parser
      OptionParser.new do |opts|
        opts.banner = "Usage: ./bin/lapidarius --gem=sinatra --recursive"

        opts.on("-gGEM", "--gem=GEM", "The gem name to scan") do |gem|
          @gem = gem
        end

        opts.on("-r", "--recursive", "Print dependencies recursively") do |recursive|
          @recursive = recursive
        end

        opts.on("-v", "--version", "Print library version") do
          @io.puts VERSION
          exit
        end
        
        opts.on("-h", "--help", "Prints this help") do
          @io.puts opts
          exit
        end
      end
    end

    private def cutter(cmd_klass)
      @cutter = Lapidarius::Cutter.new(gem: @gem, cmd_klass: cmd_klass)
    end

    private def renderer(gem)
      Lapidarius::Renderer::new(gem: gem, recursive: @recursive)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lapidarius-1.1.2 lib/lapidarius/cli.rb