|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rake' |
| 4 | +require 'readline' |
| 5 | +require 'fileutils' |
| 6 | + |
| 7 | +# rubocop:disable Metrics/BlockLength |
| 8 | +namespace :version do |
| 9 | + PROJECT_ROOT = File.expand_path(FileUtils.pwd).freeze |
| 10 | + PROJECT_NAME = ENV['PROJECT_NAME'] || File.basename(PROJECT_ROOT) |
| 11 | + |
| 12 | + desc 'Write changes to the CHANGELOG' |
| 13 | + task :changes do |
| 14 | + text = ask('CHANGELOG Entry:') |
| 15 | + text.insert( |
| 16 | + 0, |
| 17 | + "*#{read_version.join('.')}* (#{Time.now.strftime('%B %d, %Y')})\n\n" |
| 18 | + ) |
| 19 | + text << "\n" |
| 20 | + prepend_changelog(text) |
| 21 | + launch_editor(changelog) |
| 22 | + end |
| 23 | + |
| 24 | + desc 'Increment the patch version and write changes to the changelog' |
| 25 | + task :bump_patch do |
| 26 | + exit unless check_branch_and_warn |
| 27 | + major, minor, patch = read_version |
| 28 | + patch = patch.to_i + 1 |
| 29 | + write_version_file([major, minor, patch]) |
| 30 | + update_readme_version_strings |
| 31 | + Rake::Task['version:changes'].invoke |
| 32 | + end |
| 33 | + |
| 34 | + desc 'Alias for :bump_patch' |
| 35 | + task bump: :bump_patch |
| 36 | + |
| 37 | + desc 'Increment the minor version and write changes to the changelog' |
| 38 | + task :bump_minor do |
| 39 | + exit unless check_branch_and_warn |
| 40 | + major, minor, _patch = read_version |
| 41 | + minor = minor.to_i + 1 |
| 42 | + patch = 0 |
| 43 | + write_version_file([major, minor, patch]) |
| 44 | + update_readme_version_strings |
| 45 | + Rake::Task['version:changes'].invoke |
| 46 | + end |
| 47 | + |
| 48 | + desc 'Increment the major version and write changes to the changelog' |
| 49 | + task :bump_major do |
| 50 | + exit unless check_branch_and_warn |
| 51 | + major, _minor, _patch = read_version |
| 52 | + major = major.to_i + 1 |
| 53 | + minor = 0 |
| 54 | + patch = 0 |
| 55 | + write_version_file([major, minor, patch]) |
| 56 | + update_readme_version_strings |
| 57 | + Rake::Task['version:changes'].invoke |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + def version_file_path |
| 63 | + split = PROJECT_NAME.split('-') |
| 64 | + "#{PROJECT_ROOT}/lib/#{split.join('/')}/version.rb" |
| 65 | + end |
| 66 | + |
| 67 | + def module_name |
| 68 | + if PROJECT_NAME =~ /-/ |
| 69 | + PROJECT_NAME.split('-').map(&:capitalize).join('::') |
| 70 | + elsif PROJECT_NAME =~ /_/ |
| 71 | + PROJECT_NAME.split('_').map(&:capitalize).join |
| 72 | + else |
| 73 | + PROJECT_NAME.capitalize |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def read_version |
| 78 | + silence_warnings do |
| 79 | + load version_file_path |
| 80 | + end |
| 81 | + text = eval("#{module_name}::VERSION") |
| 82 | + text.split('.') |
| 83 | + end |
| 84 | + |
| 85 | + def write_version_file(version_array) |
| 86 | + version = version_array.join('.') |
| 87 | + new_version = %( VERSION = '#{version}'.freeze) |
| 88 | + lines = File.readlines(version_file_path) |
| 89 | + File.open(version_file_path, 'w') do |f| |
| 90 | + lines.each do |line| |
| 91 | + if line =~ /VERSION/ |
| 92 | + f.write("#{new_version}\n") |
| 93 | + else |
| 94 | + f.write(line) |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def update_readme_version_strings |
| 101 | + version_string = read_version.join('.') |
| 102 | + readme = open('README.md').read |
| 103 | + regex = /^\*\*Version: [0-9\.]+\*\*$/i |
| 104 | + return nil unless readme =~ regex |
| 105 | + File.open('README.md', 'w') do |f| |
| 106 | + f.write(readme.gsub(regex, "**Version: #{version_string}**")) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + def changelog |
| 111 | + return @changelog_path if @changelog_path |
| 112 | + @changelog_path = File.join(PROJECT_ROOT, 'CHANGELOG') |
| 113 | + FileUtils.touch(@changelog_path) |
| 114 | + @changelog_path |
| 115 | + end |
| 116 | + |
| 117 | + def prepend_changelog(text_array) |
| 118 | + old = File.read(changelog).to_s.chomp |
| 119 | + text_array.push(old) |
| 120 | + File.open(changelog, 'w') do |f| |
| 121 | + text_array.flatten.each do |line| |
| 122 | + f.puts(line) |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + # rubocop:disable Lint/AssignmentInCondition |
| 128 | + def ask(message) |
| 129 | + response = [] |
| 130 | + puts message |
| 131 | + puts 'Hit <Control>-D when finished:' |
| 132 | + while line = Readline.readline('* ', false) |
| 133 | + response << "* #{line.chomp}" unless line.nil? |
| 134 | + end |
| 135 | + response |
| 136 | + end |
| 137 | + # rubocop:enable Lint/AssignmentInCondition |
| 138 | + |
| 139 | + def current_branch |
| 140 | + `git symbolic-ref --short HEAD`.chomp |
| 141 | + end |
| 142 | + |
| 143 | + def branch_warning_message |
| 144 | + <<~STRING |
| 145 | + You typically do not want to bump versions on the 'master' branch |
| 146 | + unless you plan to rebase or back-merge into 'develop'. |
| 147 | +
|
| 148 | + If you don't care or don't know what I'm talking about just enter 'y' |
| 149 | + and continue. |
| 150 | +
|
| 151 | + Optionally, you can hit 'n' to abort and switch your branch to 'develop' |
| 152 | + or whatever branch you use for development, bump the version, merge to |
| 153 | + 'master' then 'rake release'. |
| 154 | +
|
| 155 | + Do you really want to bump the version on your 'master' branch? (y/n) |
| 156 | + STRING |
| 157 | + end |
| 158 | + |
| 159 | + def check_branch_and_warn |
| 160 | + return true unless current_branch == 'master' |
| 161 | + puts(branch_warning_message) |
| 162 | + while (line = $stdin.gets.chomp) |
| 163 | + return true if line =~ /[yY]/ |
| 164 | + puts 'Aborting version bump.' |
| 165 | + return false |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + def launch_editor(file) |
| 170 | + system("#{ENV['EDITOR']} #{file}") if ENV['EDITOR'] |
| 171 | + end |
| 172 | + |
| 173 | + def silence_warnings |
| 174 | + original_verbosity = $VERBOSE |
| 175 | + $VERBOSE = nil |
| 176 | + yield |
| 177 | + ensure |
| 178 | + $VERBOSE = original_verbosity |
| 179 | + end |
| 180 | +end |
| 181 | +# rubocop:enable Metrics/BlockLength |
0 commit comments