Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Upgradinate - How to upgrade while staying close to master

Upgrading like a baws

upgradinate_script.rb

  helper = UpgradinateFileHelper.new 'OLD NESS', 'NEW-NESS'
  helper.upgradinate_files [
      "/path/to/project/Gemfile"
    ]

Gemfile

  # NEW-NESS gem 'awesome_thing_to_put_in'
  gem 'deprecated_badness' # OLD NESS

Usage

ruby upgradinate_script.rb

NOTE:

  • You may need to update grep to use '--exclude-dir'
  • Make sure upgradinate_script.rb is executable
  # Mac OSX
  brew install xz #required for installing grep through homebrew
  brew install https://raw.github.com/Homebrew/homebrew-dupes/master/grep.rb
require 'tempfile'
require 'fileutils'
# We use this to go through the files marked for upgrade, stripping out the lines we no longer want, uncommenting the new lines
class UpgradinateFileHelper
attr_accessor :old_version, :new_version
def initialize old_version, new_version
@old_version = old_version
@new_version = new_version
end
def with_temp_file
file = Tempfile.new('upgradinate')
begin
yield file
ensure
file.close
file.unlink
end
end
def new_version_line? line
# matches any line starting with '# RUBY 193' and any amount of leading whitespace
line.match /^\s*# #{new_version} /
end
def old_version_line? line
# matches any line ending with '# RUBY 187' and any amount of trailing whitespace
line.match /# #{old_version}\s*$/
end
def trimmed line
trimmed = line
# removes leading '# RUBY 193 ' preserving any leading whitespace; see http://goo.gl/BRjmS
trimmed.gsub!(/^(\s*)# #{new_version} /, '\1')
# removes '# RUBY 187' and any trailing whitespace from the line
trimmed.gsub!(/\s*# #{old_version}\s*$/, '')
trimmed
end
def upgradinate_files filenames
filenames.map &method(:upgradinate_file)
end
def upgradinate_file filename
with_temp_file do |tmp|
File.open(filename, 'r') do |file|
file.each_line do |line|
if new_version_line?(line) || ! old_version_line?(line)
tmp.puts trimmed line
end
end
end
tmp.flush
tmp.close
FileUtils.cp tmp.path, filename
end
end
def write filename, contents
File.open(filename, 'w') do |file|
file.write contents
end
end
end
#!/usr/bin/env ruby
#This is one of our basic scripts to upgrade from Jasmine to JasmineRice, there is more to it but wanted a simple example.
Dir.glob("#{File.dirname(__FILE__)}/lib/*", &method(:load))
@project_dir = "/Users/#{ENV['USER']}/projects/atlas/atlas"
@old_version = "JASMINE"
@new_version = "JASMINERICE"
@file_helper = UpgradinateFileHelper.new @old_version, @new_version
def usage
puts <<-USAGE
upgradinate.rb upgrades project code to run with Jasminerice
usage: ruby script/upgradinate/jasmine.rb [--help]
USAGE
end
def quality_check(path)
if system("grep '# JASMINE' #{path}/* -ilR --exclude-dir=upgradinate --exclude-dir=reports --exclude-dir=tmp")
raise 'The above files have not been Upgradinated!'
end
end
def upgradinate
puts 'Upgradinating files in atlas directory'
@file_helper.upgradinate_files [
"#{@project_dir}/Gemfile",
"#{@project_dir}/spec/javascripts/support/jasmine.yml"
]
FileUtils.rm_f [
"#{@project_dir}/spec/javascripts/integration/bootstrap.coffee"
]
quality_check @project_dir
`bundle install` #Update and use new JasmineGem
['spec.css', 'spec.js.coffee'].each do |file_name|
FileUtils.cp "#{@project_dir}/script/upgradinate/jasminerice/#{file_name}", "#{@project_dir}/spec/javascripts/#{file_name}"
end
puts 'Done'
end
if ARGV.include?('--help') || ARGV.include?('-h') || ARGV.include?('/?')
usage
else
upgradinate
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment