Class: Sign::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/sign/runner.rb

Instance Method Summary collapse

Instance Method Details

#create_license(argv, list) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sign/runner.rb', line 79

def create_license(argv, list)
  if list.has_key?(argv[0].downcase)
    license = Sign::Fetcher.new.get_license(argv[0])
  else
    raise ArgumentError, "#{argv[0]} is not available."
  end
  
  name = !!argv[1] ? parse_argument(argv[1]) : nil
  year = !!argv[2] ? parse_argument(argv[2]) : nil
  
  Sign::Generator.new.create(license, name, year)
end

#license_exist?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sign/runner.rb', line 55

def license_exist?
  if File.exist?("LICENSE")
    argv = ARGV.clone     # save user's first inputs for license
    ARGV.clear            # clears ARGV to allow for next input
    
    puts "There seems to be a license file already. Would you like to overwrite it? [y/n]"
    input = gets.chomp.downcase
    
    yes = ["y", "yes"]
    no = ["n", "no"]
    
    if yes.include?(input)
      return false
    elsif no.include?(input)
      puts "Terminating..."
      
      exit
    else
      puts "Sorry, I didn't get that. Let's try again."
      license_exist?
    end
  end
end

#parse_argument(arg) ⇒ Object



92
93
94
# File 'lib/sign/runner.rb', line 92

def parse_argument(arg)
  arg.scan(/=(.*)/).last[0]
end

#show_helpObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sign/runner.rb', line 23

def show_help
  puts ""
  puts "  Generate a license for your project in a matter of seconds."
  puts ""
  puts "  Usage:"
  puts ""
  puts "      sign [option] <license>"
  puts ""
  puts "  Options:"
  puts ""
  puts "      --version | -v    display version number"
  puts "      --help    | -h    display help information"
  puts "      --list    | -l    display list of licenses"
  puts ""
end

#show_list(list) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sign/runner.rb', line 39

def show_list(list)
  format = "%-16s %10s"
  
  puts ""
  puts "List of available licenses:"
  puts ""
  list.each do |name, info| 
    puts format % [name, info]
  end
  puts ""
end

#show_versionObject



51
52
53
# File 'lib/sign/runner.rb', line 51

def show_version
  puts "Sign v#{Sign::VERSION}"
end

#startObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sign/runner.rb', line 3

def start
  list = {}
  
  argv = ARGV.clone
  argv << "--help" if argv.empty?
  
  Sign::Fetcher.get_list(list)

  case argv[0]
  when "--help", "-h"
    show_help
  when "--version", "-v"
    show_version
  when "--list", "-l"
    show_list(list)
  else
    create_license(argv, list) unless license_exist?
  end
end