Class: Tmg::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tmg/cli.rb,
lib/tmg/cli/info.rb,
lib/tmg/cli/list.rb,
lib/tmg/cli/user.rb,
lib/tmg/cli/wiki.rb,
lib/tmg/cli/about.rb,
lib/tmg/cli/login.rb,
lib/tmg/cli/version.rb,
lib/tmg/cli/homepage.rb,
lib/tmg/cli/documentation.rb

Overview

Commands: tmg about # Displays version number and information. tmg help [COMMAND] # Describe available commands or one specific command tmg info [GEM] # Shows information about a specific gem. tmg list # Show a list of your published gems. tmg login # Request access to RubyGems.org

Colorize: Yellow = Warnings || Things that needs your attention Green = Notice || Messages of interest White = Regular strings Red = Errors || Really important things. Red banner looks cool though.

Constant Summary collapse

@@credentials_file =
"#{Dir.home}/.gem/credentials"

Instance Method Summary collapse

Instance Method Details

#aboutObject

Displays information about the installed TMG gem such as: version, author, developer twitter profile and blog, and a banner.



6
7
8
9
10
11
12
13
14
# File 'lib/tmg/cli/about.rb', line 6

def about
  puts Tmg::BANNER.bold.red
  puts 'version: '.bold     + Tmg::VERSION.green
  puts 'author: '.bold      + 'Franccesco Orozco'.green
  puts 'Twitter: '.bold     + '@__franccesco'.green
  puts 'homepage: '.bold    + 'https://github.com/franccesco/tmg'.green
  puts 'learn more: '.bold  + 'https://codingdose.info'.green
  puts # extra line, somehow I like them.
end

#documentation(gem) ⇒ Object

Open browser and navigates to gem’s documentation



5
6
7
8
9
10
11
12
13
14
# File 'lib/tmg/cli/documentation.rb', line 5

def documentation(gem)
  documentation_uri = Gems.info(gem)['documentation_uri']
  if documentation_uri.nil?
    puts "No documentation page for ".red.bold + gem.yellow.bold
    exit(1)
  else
    puts 'Opening documentation for: '.green.bold + gem
    Launchy.open(documentation_uri)
  end
end

#homepage(gem) ⇒ Object

Open browser and navigates to gem’s homepage



5
6
7
8
9
10
11
12
13
14
# File 'lib/tmg/cli/homepage.rb', line 5

def homepage(gem)
  homepage_uri = Gems.info(gem)['homepage_uri']
  if homepage_uri.nil?
    puts "No homepage for ".red.bold + gem.yellow.bold
    exit(1)
  else
    puts 'Opening homepage of: '.green.bold + gem
    Launchy.open(homepage_uri)
  end
end

#info(gem) ⇒ Object

Displays information about a gem, optionally displaying runtime dependencies and development dependencies.



10
11
12
# File 'lib/tmg/cli/info.rb', line 10

def info(gem)
  display_gem_info(false, options[:dependencies], gem)
end

#listObject

Shows a list of your gems published on RubyGems.org If forces you to login first if it’s unable to find the credentials under your home folder. After that, it retrieves a summary consisting in the most relevant information only such as info, downloads, version, and homepage.



13
14
15
16
17
18
# File 'lib/tmg/cli/list.rb', line 13

def list
  unless File.file?(@@credentials_file)
     unless ENV['api_key']
  end
  display_gem_info(true, options[:dependencies])
end

#loginObject

Retrieves the API key for the user and writes it to ~/.gem/credentials. It also makes sure to set the permissions to 0600 as adviced on the RubyGems.org webpage. If the credentials file is on the system, then it should warn the user before overwriting the file.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tmg/cli/login.rb', line 8

def 
  # check if file credential exists
  if File.file?(@@credentials_file)
    puts 'Credentials file found!'.bold
    unless yes?("Overwrite #{@@credentials_file}? |no|".bold.yellow)
      puts "Aborted.".red.bold
      exit
    end
  end

  # Ask for username and password, mask the password and make it
  # green if it's the correct password, red if the access was denied.
  # Aborts if the password is empty.
  puts 'Write your username and password for ' + 'RubyGems.org'.yellow.bold
  username = ask('Username:'.yellow.bold)
  password = ask('Password:'.yellow.bold, echo: false)
  (puts "Aborted.".red.bold; exit) if password.empty?

  # fakes a masked password as long as the username,
  # for style purposes only.
  masked_password = '*' * username.length
  print masked_password unless options[:show_password]

  begin
    Tmg.write_credentials(username, password)
  rescue RuntimeError
    puts "\b" * masked_password.length + masked_password.red.bold
    puts 'Access Denied.'.red.bold
    exit
  else
    puts "\b" * masked_password.length + masked_password.green.bold
    puts "Credentials written to #{@@credentials_file}".green.bold
  end
end

#user(username) ⇒ Object

Displays the gems that belongs to another user, optionally flag the dependencies (runtime and development)



10
11
12
# File 'lib/tmg/cli/user.rb', line 10

def user(username)
  display_gem_info(false, options[:dependencies], nil, username)
end

#version(*gems) ⇒ Object

Displays the latest versions of gems



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tmg/cli/version.rb', line 9

def version(*gems)
  (puts 'No gems provided'.red.bold; exit ) if gems.empty?
  gems_versions = {}
  gems.each do |gem_name|
    gems_versions[gem_name] = Thread.new {
      Thread.current[gem_name] = Gems.latest_version(gem_name)['version']
    }
  end

  puts "\nGems:".upcase.yellow.bold
  gems_versions.each do |gem_name, version|
    version.join
    if version[gem_name] != 'unknown'
      puts ''.green.bold + gem_name.bold + ''.green.bold + version[gem_name].green
    else
      puts "#{gem_name}".red.bold + version[gem_name].yellow if options[:invalid]
    end
  end
  puts
end

#wiki(gem) ⇒ Object

Open browser and navigates to gem’s wiki



5
6
7
8
9
10
11
12
13
14
# File 'lib/tmg/cli/wiki.rb', line 5

def wiki(gem)
  wiki_uri = Gems.info(gem)['wiki_uri']
  if wiki_uri.nil? || wiki_uri.empty?
    puts "No wiki page for ".red.bold + gem.yellow.bold
    exit(1)
  else
    puts 'Opening wiki for: '.green.bold + gem
    Launchy.open(wiki_uri)
  end
end