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

[WIP] Create rake task lodge:release:prepare #90

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions Rakefile
Expand Up @@ -4,3 +4,9 @@
require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

require File.expand_path('../rake/common.rb', __FILE__)

Dir.glob('rake/tasks/*.rake').each do |r|
load r
end
26 changes: 26 additions & 0 deletions rake/common.rb
@@ -0,0 +1,26 @@
class LodgeRake

@@log = Logger.new(STDOUT)
@@log.level = Logger::INFO
@@log.formatter = proc { |severity, datetime, progname, msg|
"%s %5s %s\n" % [datetime.strftime('%Y-%m-%d %H:%M:%S,%L'), severity, msg]
}
@@root_path = File.expand_path('../..', __FILE__)

def self.log
@@log
end

def self.root_path
@@root_path
end

def self.log_task_start(task_name)
@@log.info("Task `#{task_name}' started")
end

def self.log_task_end(task_name)
@@log.info("Task `#{task_name}' succeeded")
end

end
55 changes: 55 additions & 0 deletions rake/tasks/release.rake
@@ -0,0 +1,55 @@
require 'bundler'
require 'bundler/cli'

namespace :lodge do

namespace :release do

desc 'Lock gemfile version to release'
task :prepare do
LodgeRake.log_task_start :release

Dir.chdir(LodgeRake.root_path) do
git_status = `git status -s 2>&1`
if $?.exitstatus != 0 or not git_status.empty?
raise "git status failed: #{git_status}"
end

bundle_install = `bundle install 2>&1`
if $?.exitstatus != 0
raise "bundle install failed: #{bundle_install}"
else
puts bundle_install
end
end

lockfile_path = File.expand_path('Gemfile.lock', LodgeRake.root_path)
lockfile = Bundler::LockfileParser::new(Bundler.read_file(lockfile_path))
gems = {}
lockfile.specs.each do |s|
gems[s.name] = s.version.to_s
end

gemfile_path = File.expand_path('Gemfile', LodgeRake.root_path)

replaced_gemfile_contents = ''
File.open(gemfile_path).each_line do |l|
gem_pattern = /^(.*gem\s+["'])([^"']+)(["']\s*,\s*["'])([^"']+)(["'].*)$/
if m = gem_pattern.match(l)
locked_version = gems[m[2]]
replaced_gemfile_contents << l.gsub(gem_pattern, "#{m[1]}#{m[2]}#{m[3]}#{locked_version}#{m[5]}")
else
replaced_gemfile_contents << l
end
end

File.open(gemfile_path, 'w') do |f|
f << replaced_gemfile_contents
end

LodgeRake.log_task_end :release
end

end

end