Created
March 24, 2014 16:35
-
-
Save erbmicha/9743963 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
# encoding: utf-8 | |
# Copyright 2013-2014, Manfred Stienstra, <[email protected]> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to | |
# deal in the Software without restriction, including without limitation the | |
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
# sell copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
# DEALINGS IN THE SOFTWARE. | |
# USAGE | |
# | |
# 1. Update your checkouts to the latest version and make sure they're in the | |
# right branch. | |
# 2. Run this script from anywhere with Rails installed and at least ruby 2.0. | |
# ruby rails4.rb /path/to/project | |
# 3. $ cd /path/to/project && bundle install | |
# 4. Run your test suite. | |
# 5. Commit the changes. | |
# 6. Deploy. | |
# | |
# NOTE: This script changes files and might destroy your code. Deal with it! | |
require 'find' | |
require 'yaml' | |
require 'fileutils' | |
class Upgrader | |
module ClassWithAttributes | |
def initialize(attributes={}) | |
attributes.each do |key, value| | |
send("#{key}=", value) | |
end | |
end | |
end | |
include ClassWithAttributes | |
class Configuration | |
include ClassWithAttributes | |
attr_accessor :path | |
def log(message) | |
puts "#{rails_root}: #{message}" | |
end | |
def app_slug | |
File.basename(path) | |
end | |
def tmp_path | |
File.join(path, 'tmp') | |
end | |
def tmp_app_path | |
File.join(tmp_path, app_slug) | |
end | |
def database_config_path | |
File.join(path, 'config/database.yml') | |
end | |
def raw_database | |
config = YAML.load_file(database_config_path) | |
config.each do |env, settings| | |
if env == 'development' | |
return settings['adapter'] | |
end | |
end | |
end | |
def database | |
case raw_database | |
when 'mysql2' | |
'mysql' | |
else | |
raw_database | |
end | |
end | |
def rails_command | |
"rails new #{tmp_app_path} --force --skip-gemfile --skip-bundle --skip-git --skip-keeps --skip-sprockets --database=#{database}" | |
end | |
def expand_tmp_path(relative_path) | |
File.join(tmp_app_path, relative_path) | |
end | |
def expand_app_path(relative_path) | |
File.join(path, relative_path) | |
end | |
def bootfile_paths | |
%w( | |
Rakefile | |
config/application.rb | |
config/boot.rb | |
config/environment.rb | |
config/environments | |
config/initializers | |
) | |
end | |
def gemfile_path | |
File.join(path, 'Gemfile') | |
end | |
end | |
attr_accessor :path | |
def configuration | |
@configuration ||= Configuration.new(path: path) | |
end | |
def generate_tmp_app | |
system(configuration.rails_command) | |
end | |
def copy(paths) | |
paths.each do |path| | |
tmp_path = configuration.expand_tmp_path(path) | |
if File.directory?(tmp_path) | |
Find.find(tmp_path) do |filename| | |
unless File.directory?(filename) | |
relative_path = filename[tmp_path.length + 1..-1] | |
destination_path = configuration.expand_app_path(File.join(path, relative_path)) | |
FileUtils.mkdir_p(File.dirname(destination_path)) | |
FileUtils.cp(filename, destination_path) | |
end | |
end | |
else | |
FileUtils.cp(tmp_path, configuration.expand_app_path(path)) | |
end | |
end | |
end | |
def copy_bootfiles | |
copy(configuration.bootfile_paths) | |
end | |
def generate_gemfile | |
File.open(configuration.gemfile_path, 'w') do |file| | |
file.puts("source 'https://rubygems.org'\n\ngem 'rails', '~> 4.0.0'") | |
end | |
end | |
def remove_tmp_app | |
FileUtils.rm_rf(configuration.tmp_app_path) | |
end | |
def upgrade | |
generate_tmp_app | |
copy_bootfiles | |
generate_gemfile | |
remove_tmp_app | |
end | |
def self.upgrade(path) | |
new(path: File.expand_path(path)).upgrade | |
end | |
end | |
if __FILE__ == $0 | |
Upgrader.upgrade(ARGV[0]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment