Class: GFSM::Commands::Changelog

Inherits:
BaseCommand show all
Defined in:
lib/commands/changelog.rb

Constant Summary collapse

NEXT_ENTRY_MARKER =
"<!--- next entry here -->"

Instance Attribute Summary

Attributes inherited from BaseCommand

#stderr, #stdout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from GFSM::Commands::BaseCommand

Class Method Details

.helpObject



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
# File 'lib/commands/changelog.rb', line 9

def self.help
  cli_info = GFSM::Tools::VersionBumperSettings.cli_info

  <<~HELP
  Usage:
  gfsm changelog [help|generate|extract] [--output-file <path>] [--input-file <path>] [--no-incremental] [--only-new-entries] [--extract-version <version>] #{cli_info[:usage]}

  Commands:
  help                                  # Prints this help
  generate                              # Generate the changelog for the current version
  extract                               # Extract the changelog for the latest or specified version

  Options:
  --output-file <path>                  # Path to the output changelog file. Defaults to 'CHANGELOG.md'. If not specified, the generate changelog content will be written to stdout
  --no-incremental                      # When provided, the generated changelog won't look for an existing changelog file. When outputting to stdout the changelog will never be incremental
  --only-new-entries                    # When provided, the generated changelog won't look for an existing changelog file and will contain only the new entries for the current version, without the Changelog or version headings
  --input-file <path>                   # Path to the input changelog file. Defaults to 'CHANGELOG.md'
  --extract-version <version>           # Version from which to extract the changelog. Defaults to the latest one on the changelog file
  #{cli_info[:options]}

  Environment variables:
  OUTPUT_FILE                           # Equivalent to --output-file
  NO_INCREMENTAL                        # Equivalent to --no-incremental
  ONLY_NEW_ENTRIES                      # Equivalent to --only-new-entries
  INPUT_FILE                            # Equivalent to --input-file
  EXTRACT_VERSION                       # Equivalent to --extract-version
  #{cli_info[:environment_variables]}
  HELP
end

Instance Method Details

#run(args = []) ⇒ Object



39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/commands/changelog.rb', line 39

def run(args = [])
  case args.shift
  when 'help'
    GFSM::Output.puts(GFSM::Commands::Changelog.help)
  when 'generate'
    no_incremental = ENV.has_key?("NO_INCREMENTAL") || args.include?("--no-incremental")
    only_new_entries = ENV.has_key?("ONLY_NEW_ENTRIES") || args.include?("--only-new-entries")
    output_file_path = get_output_file_path(args)
    changelog_section = compute_this_version_section(args, only_new_entries)

    if only_new_entries
      GFSM::Output.puts changelog_section
    elsif !output_file_path
      GFSM::Output.puts <<~CHANGELOG
        # Changelog
        #{changelog_section}
        CHANGELOG
    else
      if File.file?(output_file_path) && !no_incremental
        existing_content = File.read(output_file_path)

        file_content = existing_content.gsub(/#{Regexp.quote(NEXT_ENTRY_MARKER)}/i, changelog_section)

        File.open(output_file_path, 'w') { |file| file.write file_content }
      else
        File.open(output_file_path, 'w') do |file|
          file.write <<~CHANGELOG
          # Changelog
          #{changelog_section}
          CHANGELOG
        end
      end
    end
  when 'extract'
    input_file_path = get_input_file_path(args)
    version_to_extract = get_version_to_extract(args)

    unless File.file?(input_file_path)
      GFSM::Output.error "No changelog file found at #{input_file_path}"
    else
      section = extract_version_section(input_file_path, version_to_extract)

      if section.nil?
        GFSM::Output.error "No changelog section found for version #{version_to_extract}"
      else
        GFSM::Output.puts section
      end
    end
  else
    GFSM::Output.warn GFSM::Commands::Changelog.help
  end

  true
end