create_project
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'rubygems'
require 'activesupport'
require 'pathname'
require 'digest/md5'
project_name = ARGV[0]
fail("Usage: #{File.basename(__FILE__)} new_project_name") unless project_name
fail("Project name must only contain [a-z0-9_]") unless project_name =~ /^[a-z0-9_]+$/
base_directory = Pathname.new(File.join(File.dirname(__FILE__), '..', '..')).realpath
project_directory = base_directory + project_name
fail("Project directory (#{project_directory}) already exists") if project_directory.exist?
template_url = "git://github.com/dancroak/heroku_suspenders.git"
changeme = "rebirth"
changesession = "CHANGESESSION"
changetitle = "Rebirth"
def run(cmd)
puts "Running '#{cmd}'"
out = `#{cmd}`
if $? != 0
fail "Command #{cmd} failed: #$?\n#{out}"
end
out
end
def search_and_replace(file, search, replace)
if File.file?(file)
contents = File.read(file)
if contents[search]
puts "Replacing #{search} with #{replace} in #{file}"
contents.gsub!(search, replace)
File.open(file, "w") { |f| f << contents }
end
end
end
def not_installed?(gem_name)
gems = Gem.source_index.find_name(gem_name)
gems.detect { |gem| gem.name == gem_name }.nil?
end
def installed?(gem_name)
installed_gems = Gem.source_index.find_name(gem_name)
installed_gems.any?
end
run("cp -r . #{project_directory}")
Dir.chdir(project_directory) or fail("Couldn't change to #{project_directory}")
run("git init")
run("git remote add heroku_suspenders #{template_url}")
Dir.glob("#{project_directory}/**/*").each do |file|
search_and_replace(file, changeme, project_name)
search_and_replace(file, changetitle, project_name.titleize)
end
Dir.glob("#{project_directory}/**/session_store.rb").each do |file|
datestring = Time.now.strftime("%j %U %w %A %B %d %Y %I %M %S %p %Z")
search_and_replace(file, changesession, Digest::MD5.hexdigest("#{project_name} #{datestring}"))
end
run("git mv public/stylesheets/screen.css public/stylesheets/#{project_name}.css")
run("sudo chmod 0666 public/stylesheets/#{project_name}.css")
run("git add .")
run("git commit -m 'New Heroku Suspenders app'")
unless installed?("rails")
run "sudo gem install rails"
end
# can't vendor nokogiri because it has native extensions
unless installed?("nokogiri")
run "sudo gem install nokogiri --version='1.3.3'"
end
# need to install heroku gem for the 'heroku' commands
unless installed?("heroku")
run "sudo gem install heroku"
end
unless installed?("sqlite3-ruby")
run "sudo gem install sqlite3-ruby"
end
# can't install rubaidh-google_analytics via rake gems:install because
# it is a production gem and we don't have a production database
unless installed?("rubaidh-google_analytics")
run "sudo gem install rubaidh-google_analytics --version='1.1.4'"
end
run("sudo touch log/development.log")
run("sudo chmod 0666 log/development.log")
run("sudo touch log/test.log")
run("sudo chmod 0666 log/test.log")
run("sudo touch log/cucumber.log")
run("sudo chmod 0666 log/cucumber.log")
run("rake db:migrate RAILS_ENV=development")
run("rake db:migrate RAILS_ENV=test")
run("rake db:migrate RAILS_ENV=cucumber")
puts
puts "Next: run the tests, configure Heroku, deploy!"
puts
puts "cd ../#{project_name} && rake && rake heroku:setup"