Class: XSemVer::Runner

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

Overview

Contains the logic for performing SemVer operations from the command line.

Defined Under Namespace

Classes: CommandError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Run a semver command. Raise a CommandError if the command does not exist. Expects an array of commands, such as ARGV.

Raises:



14
15
16
17
18
19
20
# File 'lib/runner.rb', line 14

def initialize(*args)
  @args = args
  command = @args.shift || :tag
  method = "run_#{command}"
  raise CommandError, "invalid command #{command}" unless self.class.method_defined?(method)
  send method
end

Class Method Details

.help_textObject

Return the text to be displayed when the ‘help’ command is run.



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

def self.help_text
  <<-HELP
semver commands
---------------

init[ialze]                        # initialize semantic version tracking
inc[rement] major | minor | patch  # increment a specific version number
pre[release] [STRING]              # set a pre-release version suffix
spe[cial] [STRING]                 # set a pre-release version suffix (deprecated)
meta[data] [STRING]                # set a metadata version suffix
format                             # printf like format: %M, %m, %p, %s
tag                                # equivalent to format 'v%M.%m.%p%s'
help

PLEASE READ http://semver.org
  HELP
end

Instance Method Details

#run_formatObject

Output the semver as specified by a format string. See: SemVer#format



104
105
106
107
108
# File 'lib/runner.rb', line 104

def run_format
  version = SemVer.find
  format_str = @args.shift or raise CommandError, "required: format string"
  puts version.format(format_str)
end

#run_helpObject

Output instructions for using the semvar command.



120
121
122
# File 'lib/runner.rb', line 120

def run_help
  puts self.class.help_text
end

#run_incrementObject Also known as: run_inc

Increment the major, minor, or patch of the .semver file.



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

def run_increment
  version = SemVer.find
  dimension = @args.shift or raise CommandError, "required: major | minor | patch"
  case dimension
  when 'major'
    version.major += 1
    version.minor =  0
    version.patch =  0
  when 'minor'
    version.minor += 1
    version.patch =  0
  when 'patch'
    version.patch += 1
  else
    raise CommandError, "#{dimension} is invalid: major | minor | patch"
  end
  version.special = ''
  version. = ''
  version.save
end

#run_initializeObject Also known as: run_init

Create a new .semver file if the file does not exist.



44
45
46
47
48
49
50
51
52
# File 'lib/runner.rb', line 44

def run_initialize
  file = SemVer.file_name
  if File.exist? file
    puts "#{file} already exists"
  else
    version = SemVer.new
    version.save file
  end
end

#run_metadataObject Also known as: run_meta

Set the metadata of the .semver file.



93
94
95
96
97
98
# File 'lib/runner.rb', line 93

def 
  version = SemVer.find
  special_str = @args.shift or raise CommandError, "required: an arbitrary string (beta, alfa, romeo, etc)"
  version. = special_str
  version.save
end

#run_specialObject Also known as: run_spe, run_pre, run_prerelease

Set the pre-release of the .semver file.



81
82
83
84
85
86
# File 'lib/runner.rb', line 81

def run_special
  version = SemVer.find
  special_str = @args.shift or raise CommandError, "required: an arbitrary string (beta, alfa, romeo, etc)"
  version.special = special_str
  version.save
end

#run_tagObject

Output the semver with the default formatting. See: SemVer#to_s



113
114
115
116
# File 'lib/runner.rb', line 113

def run_tag
  version = SemVer.find
  puts version.to_s
end