Last active
October 3, 2019 03:14
-
-
Save hellcp/2425c9f0e8ed3fad969c5b2d9356c148 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# | |
# y2bump - bump the version and add a changelog entry | |
# | |
# Author: Stasiek Michalski <hellcp@opensuse.org> | |
# Copyright 2019 | |
# | |
require "optparse" | |
options = {} | |
OptionParser.new do |parser| | |
parser.banner = "Usage: y2bump [options]" | |
parser.on("-h", "--help", "Show this help message") do | |
puts parser | |
end | |
parser.on("-d", "--directory [STRING]", "Directory containing .spec and .changes") do |v| | |
options[:directory] = v | |
end | |
parser.on("-m", "--message [STRING]", "Text of the changelog entry") do |v| | |
options[:message] = v | |
end | |
parser.on("-v", "--version [STRING]", "Version of the package (optional)") do |v| | |
options[:message] = v | |
end | |
parser.on("-p", "--packager [STRING]", "Packager string (optional)") do |v| | |
options[:packager] = v | |
end | |
end.parse! | |
def update_version(directory, text, version, packager) | |
directory = directory.nil? ? '.' : directory | |
Dir.glob("#{directory}/*.spec") do |specfile| | |
File.open("#{specfile}.tmp", 'w') do |fo| | |
File.foreach(specfile) do |li| | |
if li.chomp.include? 'Version:' | |
# Spaces calculated manually in case the number of spaces is non-standard | |
spaces = li.chomp.strip.count(' ') | |
# If version wasn't specified, this checks and bumps the version by 1 | |
if version.nil? | |
version = /(?<=(Version:\s{#{spaces}}))(.*)/.match(li.chomp).to_s | |
version_arr = version.split('.') | |
version_arr[version_arr.length - 1] = Integer(version_arr.last) + 1 | |
version = version_arr.join('.') | |
end | |
version_li = li.chomp.sub(/(?<=(Version:\s{#{spaces}}))(.*)/, version) | |
fo.puts version_li | |
else | |
fo.puts li | |
end | |
end | |
end | |
File.delete(specfile) | |
File.rename("#{specfile}.tmp", specfile) | |
puts "Specfile (#{specfile}) updated to the version #{version}" | |
# Changes file doesn't need to exist for this to work | |
changes = "#{directory}/#{File.basename(specfile, '.spec')}.changes" | |
changelog = '' | |
# Changelog is updated without the help of osc, to avoid additional dependencies | |
File.open("#{changes}.tmp", 'w') do |fo| | |
fo.puts "-" * 67 | |
time = Time.new | |
day_format = time.strftime("%-d") | |
day = day_format.length == 2 ? day_format : " #{day_format}" | |
current_time = time.strftime "%a %b #{day} %H:%M:%S UTC %Y" | |
gitconfig = File.expand_path("~/.gitconfig") | |
if File.exists?(gitconfig) && packager.nil? | |
git_info = Hash[File.open(gitconfig).read.scan(/(.*)?=\s*?(.*)/).map { |k,v| [k.strip, v.strip] }] | |
packager = "#{git_info["name"]} <#{git_info["email"]}>" | |
end | |
fo.puts "#{current_time} - #{packager}\n" | |
text.split('\n') do |li| | |
changelog += "\n- " + li.scan(/.{1,65}.*?(?:\b|$)/).map { |x| x.strip }.join("\n ") | |
end unless text.nil? | |
fo.puts changelog | |
fo.puts "- #{version}\n\n" | |
File.foreach(changes) do |li| | |
fo.puts li | |
end if File.exists?(changes) | |
end | |
File.delete(changes) if File.exists?(changes) | |
File.rename("#{changes}.tmp", changes) | |
output = "Changes (#{changes}) updated to the version #{version}" | |
output += " with a changelog#{changelog}" unless changelog.empty? | |
puts output | |
end | |
end | |
update_version(options[:directory], options[:message], options[:version], options[:packager]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment