Last active
April 11, 2025 08:11
-
-
Save amkisko/b042d94303ca848716eeeb35ffdad44a to your computer and use it in GitHub Desktop.
CHANGELOG.md application version bump script suitable for ruby, Rails, npm and Flutter projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
tag_prefix = "version/" | |
changelog_path = "CHANGELOG.md" | |
changelog = File.read(changelog_path) | |
if changelog.empty? | |
puts "No changelog found in #{changelog_path}" | |
exit | |
end | |
version = changelog.scan(/^##\s*(\S+)/).first&.first | |
if version.nil? | |
puts "No version found in #{changelog_path}" | |
exit | |
end | |
`git fetch --tags` | |
current_tag = "#{tag_prefix}#{version}" | |
current_tag_exists = !`git tag -l "#{current_tag}"`.empty? | |
git_log = if current_tag_exists | |
`git log --pretty=format:"%h %s" #{current_tag}..HEAD` | |
end | |
next_version_parts = version.split(".") | |
3.times { |idx| next_version_parts[idx] = next_version_parts[idx].to_i } | |
git_log_lines_count = git_log&.lines&.size || 0 | |
if git_log_lines_count < 100 | |
next_version_parts[2] += 1 | |
else | |
next_version_parts[1] += 1 | |
next_version_parts[2] = 0 | |
end | |
next_version = next_version_parts.join(".") | |
next_version_tag = "#{tag_prefix}#{next_version}" | |
puts "Next version will be #{next_version} (based on #{git_log_lines_count} commits since #{version})" | |
puts "Do you want to continue? (y/n)" | |
if gets.chomp.downcase != "y" | |
puts "Exiting" | |
exit | |
end | |
puts "Updating CHANGELOG.md" | |
File.open("CHANGELOG.md", "w") do |file| | |
file.puts "# CHANGELOG\n\n" | |
file.puts "## #{next_version}\n\n" | |
file.puts "Diff ref: #{current_tag}...#{next_version_tag}\n" | |
file.puts git_log.lines.map { |line| " * #{line}" }.join if git_log | |
file.puts changelog.lines[1..].join | |
end | |
puts "Updating webapp/package.json" | |
require "json" | |
webapp_package_json_path = "webapp/package.json" | |
webapp_package_json = JSON.parse(File.read(webapp_package_json_path)) | |
webapp_package_json["version"] = next_version | |
File.open(webapp_package_json_path, "w") do |file| | |
file.puts JSON.pretty_generate(webapp_package_json) | |
end | |
puts "Updating flutter_app/pubspec.yaml" | |
flutter_app_pubspec_yaml_path = "flutter_app/pubspec.yaml" | |
flutter_app_pubspec_yaml = YAML.load_file(flutter_app_pubspec_yaml_path) | |
flutter_app_pubspec_yaml["version"] = next_version | |
File.open(flutter_app_pubspec_yaml_path, "w") do |file| | |
file.puts YAML.dump(flutter_app_pubspec_yaml) | |
end | |
puts "Creating git tag #{next_version_tag}" | |
`git tag "#{next_version_tag}" && git push --tags` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment