Class: Gitvers::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gitvers/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



5
6
7
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitvers/cli.rb', line 5

def initialize
  command = ARGV.shift.to_sym rescue nil

  rep = Repository.new(Dir.pwd)

  begin
    case command
    when :bump
      rep.bump(ARGV.shift)
    when :init
      if rep.versions.count > 0
        puts "Repository already have a git tag #{rep.full_version}"
      else
        initial = ask("Please enter the initial version in the form X.Y.Z: ")
        rep.tag(initial)
      end
    when :show
      what = ARGV.shift
      case what
      when 'full'
        puts rep.full_version
      when 'short'
        puts rep.short_version
      when 'revision'
        puts rep.revision_number
      when 'shell'
        opts = ARGV.shift(2)
        export = opts.include?('export')
        noprefix = opts.include?('noprefix')

        v = [rep.revision_number, rep.full_version, rep.short_version]
        k = %w(GITVERS_REVISION GITVERS_FULL GITVERS_SHORT)
        k = %w(REVISION_NUMBER FULL_VERSION SHORT_VERSION) if noprefix

        v.each_with_index do |val, i|
          print 'export ' if export
          puts "#{k[i]}=#{val}"
        end
      when 'lines'
        puts rep.revision_number
        puts rep.full_version
        puts rep.short_version
      else
        puts "Current version: #{rep.summary}\n\n"
        puts "All versions:"
        puts rep.versions.map {|l| "    #{l}"}
      end
    else
      usage
    end
  rescue NoVersionError
    puts "No git tag, use `gitvers init` to create one."
  rescue InvalidVersionError => e
    puts e.message
  end
end

Instance Method Details

#usageObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gitvers/cli.rb', line 62

def usage
  puts %|usage: gitvers <command> [<args>]

Commands:
init            Create an initial git tag
bump <step>     Bump the version, step can be major, minor or patch
bump <version>  Bump the version to a specific destination version
show [<what>]   Display the current version
                'What' can be:
                - full  # x.y.z-commit
                - short # x.y.z
                - revision # revision
                - shell # output GITVERS_<name> variables
                        # supports two options
                  - export # prefix lines with export
                  - noprefix # do not prefix lines with GITVERS_
                - lines # output 3 lines with revision, full, short respectively


Examples:

gitvers bump major
gitvers bump 1.3.4

  |
end