Class: PrChangelog::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/pr_changelog/cli.rb,
lib/pr_changelog/cli/args.rb

Overview

Used for the implementation of the exposed executable for this gem

Defined Under Namespace

Classes: Args, CannotDetermineRelease, HelpWanted, InvalidInputs

Constant Summary collapse

HELP_TEXT =
<<~HELP
  Usage: pr_changelog [options] from_reference to_reference

  [Options]

    -h, --help\tShow this help
    -l, --last-release\tSets from_reference and to_reference to the last release and the previous one
    --format FORMAT_NAME\t(default "plain"), options ("pretty", "plain")
    --strategy STRATEGY_NAME\tIs the strategy used to merge pull requests (default "merge"), options ("merge", "squash").

  [Examples]

    Listing the unreleased changes

    $ pr_changelog

    Listing the changes from the last release

    $ pr_changelog --last-release

    Listing the changes between two given git references

    $ pr_changelog reference_A reference_B
HELP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_args, releases = nil) ⇒ CLI

Returns a new instance of CLI.

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pr_changelog/cli.rb', line 42

def initialize(raw_args, releases = nil)
  args = Args.new(raw_args)
  raise HelpWanted if args.include_flags?('-h', '--help')

  @format = args.value_for('--format') || PrChangelog.config.default_format

  @strategy = args.value_for('--strategy') || PrChangelog.config.default_strategy

  @releases = releases || Releases.new

  @from_reference, @to_reference = args.last(2)
  @from_reference ||= @releases.last_release
  @to_reference ||= 'master'

  if args.include_flags?('-l', '--last-release')
    last_release_pair = @releases.last_release_pair
    raise CannotDetermineRelease if last_release_pair.length != 2

    @from_reference, @to_reference = last_release_pair
  end

  return if @from_reference && @to_reference

  raise InvalidInputs
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



37
38
39
# File 'lib/pr_changelog/cli.rb', line 37

def format
  @format
end

#from_referenceObject (readonly)

Returns the value of attribute from_reference.



37
38
39
# File 'lib/pr_changelog/cli.rb', line 37

def from_reference
  @from_reference
end

#strategyObject (readonly)

Returns the value of attribute strategy.



37
38
39
# File 'lib/pr_changelog/cli.rb', line 37

def strategy
  @strategy
end

#to_referenceObject (readonly)

Returns the value of attribute to_reference.



37
38
39
# File 'lib/pr_changelog/cli.rb', line 37

def to_reference
  @to_reference
end

Instance Method Details

#build_strategyObject



68
69
70
71
72
73
74
75
76
# File 'lib/pr_changelog/cli.rb', line 68

def build_strategy
  if strategy == 'merge'
    MergeCommitStrategy.new(from_reference, to_reference)
  elsif strategy == 'squash'
    SquashCommitStrategy.new(from_reference, to_reference)
  else
    raise "Strategy '#{strategy}' not recognized."
  end
end

#runObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/pr_changelog/cli.rb', line 78

def run
  changes = NotReleasedChanges.new(build_strategy)
  puts "## Changes since #{from_reference} to #{to_reference} (#{strategy})\n\n"

  if format == 'pretty'
    puts changes.grouped_formatted_changelog
  else
    puts changes.formatted_changelog
  end
end