Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rake version helpers #39

Merged
merged 1 commit into from
Dec 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,84 @@ namespace :test do
end
end

# Print the current version of this gem or update it.
#
# @param [Type] target the new version you want to set, or nil if you only want to show
def train_version(target = nil)
path = 'lib/train/version.rb'
require_relative path.sub(/.rb$/, '')

nu_version = target.nil? ? '' : " -> #{target}"
puts "Train: #{Train::VERSION}#{nu_version}"

unless target.nil?
raw = File.read(path)
nu = raw.sub(/VERSION.*/, "VERSION = '#{target}'")
File.write(path, nu)
load(path)
end
end

# Automatically generate a changelog for this project. Only loaded if
# the necessary gem is installed.
begin
require 'github_changelog_generator/task'
GitHubChangelogGenerator::RakeTask.new :changelog
GitHubChangelogGenerator::RakeTask.new :changelog do |config|
require_relative 'lib/train/version'
config.future_release = Train::VERSION
end
rescue LoadError
puts '>>>>> GitHub Changelog Generator not loaded, omitting tasks'
end

# Check if a command is available
#
# @param [Type] x the command you are interested in
# @param [Type] msg the message to display if the command is missing
def require_command(x, msg = nil)
return if system("command -v #{x} || exit 1")
msg ||= 'Please install it first!'
puts "\033[31;1mCan't find command #{x.inspect}. #{msg}\033[0m"
exit 1
end

# Check if a required environment variable has been set
#
# @param [String] x the variable you are interested in
# @param [String] msg the message you want to display if the variable is missing
def require_env(x, msg = nil)
exists = `env | grep "^#{x}="`
return unless exists.empty?
puts "\033[31;1mCan't find environment variable #{x.inspect}. #{msg}\033[0m"
exit 1
end

# Check the requirements for running an update of this repository.
def check_update_requirements
require_command 'git'
require_command 'github_changelog_generator', "\n"\
"For more information on how to install it see:\n"\
" https://github.com/skywinder/github-changelog-generator\n"
require_env 'CHANGELOG_GITHUB_TOKEN', "\n"\
"Please configure this token to make sure you can run all commands\n"\
"against GitHub.\n\n"\
"See github_changelog_generator homepage for more information:\n"\
" https://github.com/skywinder/github-changelog-generator\n"
end

# Show the current version of this gem.
desc 'Show the version of this gem'
task :version do
train_version
end

# Update the version of this gem and create an updated
# changelog. It covers everything short of actually releasing
# the gem.
desc 'Bump the version of this gem'
task :bump_version, [:version] do |_, args|
v = args[:version] || ENV['to']
fail "You must specify a target version! rake release[1.2.3]" if v.empty?
check_update_requirements
train_version(v)
Rake::Task['changelog'].invoke
end