ruby Gem或项目语义版本控制rake任务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Gem或项目语义版本控制rake任务相关的知识,希望对你有一定的参考价值。
require 'rubygems'
require 'rake'
require 'readline'
require 'fileutils'
require 'pry'
module Bundler
class GemHelper
protected
# Push the gem to your own internal gem inabox server
# def rubygem_push(path)
# sh("gem inabox '#{path}'")
# Bundler.ui.confirm "Pushed #{name} #{version} to #{path}"
# end
end
end
namespace :version do
PROJECT_ROOT = File.expand_path(FileUtils.pwd)
PROJECT_NAME = ENV['PROJECT_NAME'] || File.basename(PROJECT_ROOT)
desc "Write changes to the CHANGELOG"
task :changes do
text = ask("CHANGELOG Entry:")
text.insert(0, "*#{read_version.join('.')}* (#{Time.now.strftime("%B %d, %Y")})\n\n")
text << "\n"
prepend_changelog(text)
system("#{ENV['EDITOR']} CHANGELOG")
end
desc "Increment the patch version and write changes to the changelog"
task :bump_patch do
exit unless check_branch
major, minor, patch = read_version
patch = patch.to_i + 1
write_version([major, minor, patch])
version_string = read_version.join('.')
readme = open('README.md').read
File.open('README.md', 'w') {|f| f.write(readme.gsub(/^\*\*Version: [0-9\.]+\*\*$/, "**Version: #{version_string}**")) }
Rake::Task["version:changes"].invoke
end
desc "Alias for :bump_patch"
task :bump => :bump_patch do; end
desc "Increment the minor version and write changes to the changelog"
task :bump_minor do
exit unless check_branch
major, minor, patch = read_version
minor = minor.to_i + 1
patch = 0
write_version([major, minor, patch])
Rake::Task["version:changes"].invoke
end
desc "Increment the major version and write changes to the changelog"
task :bump_major do
exit unless check_branch
major, minor, patch = read_version
major = major.to_i + 1
minor = 0
patch = 0
write_version([major, minor, patch])
Rake::Task["version:changes"].invoke
end
##################################################
private
def version_file_path
split = PROJECT_NAME.split('-')
"#{PROJECT_ROOT}/lib/#{split.join('/')}/version.rb"
end
def module_name
if PROJECT_NAME.match(/-/)
PROJECT_NAME.split('-').map {|e| e.capitalize}.join('::')
elsif PROJECT_NAME.match(/_/)
PROJECT_NAME.split('_').map {|e| e.capitalize}.join
else
PROJECT_NAME.capitalize
end
end
def read_version
load version_file_path
text = eval("#{module_name}::VERSION")
major, minor, patch = text.split('.')
end
def write_version(version_array)
version = version_array.join('.')
new_version = %Q( VERSION = "#{version}")
lines = File.readlines(version_file_path)
File.open(version_file_path, 'w') do |f|
lines.each do |line|
if line =~ /VERSION/
f.write("#{new_version.to_s}\n")
else
f.write(line)
end
end
end
end
def prepend_changelog(text_array)
# read current changelog
old = File.read("#{PROJECT_ROOT}/CHANGELOG").to_s.chomp
text_array.push(old)
File.open("#{PROJECT_ROOT}/CHANGELOG", 'w') do |f|
text_array.flatten.each do |line|
f.puts(line)
end
end
end
def ask(message)
response = []
puts message
puts "Hit <Control>-D when finished:"
while line = Readline.readline('* ', false)
response << "* #{line.chomp}" unless line.nil?
end
response
end
def current_branch
`git rev-parse --abbrev-ref HEAD`.chomp
end
def check_branch
if current_branch == 'master'
puts "You typically do not want to bump versions on the 'master' branch"
puts "unless you plan to rebase or back-merge into the 'develop'."
puts ""
puts "If you don't care or don't know what I'm talking about just enter 'y'"
puts "and continue."
puts ""
puts "Optionally, you can hit 'n' to abort and switch your branch to 'develop'"
puts "or whatever branch you use for development, bump the version, merge to"
puts "'master' then 'rake release'."
puts ""
puts "Do you really want to bump the version on your 'master' branch? (y/n)"
while (line = $stdin.gets.chomp)
return true if line.match /[yY]/
puts "Aborting version bump."
return false
end
end
return true
end
end
以上是关于ruby Gem或项目语义版本控制rake任务的主要内容,如果未能解决你的问题,请参考以下文章
整理Ruby相关的各种概念(rvm, gem, bundle, rake, rails等)
Ruby on Rails:运行 rake 命令时出现错误消息