Created
October 10, 2012 16:59
-
-
Save gazay/3866900 to your computer and use it in GitHub Desktop.
Autocreating heroku app
This file contains hidden or 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
require 'fileutils' | |
class Helper | |
attr_accessor :dir, :temp_dir | |
attr_reader :template_path, :original_dir | |
def initialize(dir = nil) | |
@template_path = File.expand_path('../../app_template', __FILE__) | |
@original_dir = FileUtils.pwd | |
@dir = dir | |
end | |
def create_proxies(count = 1) | |
puts "creating #{count} proxies" | |
apps = Array.new count | |
apps.map do | |
create_proxy | |
end | |
cd_to original_dir | |
apps | |
end | |
def create_proxy | |
create_directory | |
copy_template | |
cd_to temp_dir | |
initialize_git_and_commit_app | |
app_url = create_heroku_app | |
push_application | |
rename_dir app_url | |
app_url | |
end | |
def create_directory | |
@temp_dir = File.join(dir, generate_dir_name) | |
while Dir.exists?(temp_dir) | |
@temp_dir = File.join(dir, generate_dir_name) | |
end | |
FileUtils.mkdir_p(temp_dir) | |
puts "created #{temp_dir} directory" | |
end | |
def copy_template | |
FileUtils.cp_r(template_path + '/.', temp_dir) | |
puts "template copied" | |
end | |
def cd_to(path) | |
FileUtils.cd path | |
puts "cd to #{path}" | |
end | |
def initialize_git_and_commit_app | |
`git init` | |
`git add .` | |
`git commit -m 'Init commit'` | |
puts "git initialized" | |
end | |
def create_heroku_app | |
out = `heroku create`.match(/http:\/\/(.+)\/ \|/)[1] | |
puts "heroku app #{out} created" | |
out | |
end | |
def push_application | |
`git push heroku master` | |
puts "app pushed to heroku" | |
end | |
def rename_dir(new_name) | |
new_name = (temp_dir.split('/')[0..-2] << new_name).join('/') | |
FileUtils.mv(temp_dir, new_name) | |
puts "directory renamed. Full path: #{new_name}" | |
end | |
private | |
def generate_dir_name | |
(0..9).map { ('a'..'z').to_a[rand(26)] }.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment