| 1 |
require 'fileutils' |
| 2 |
|
| 3 |
module GitRails |
| 4 |
module Commands |
| 5 |
class Init < GitRails::Command |
| 6 |
def run(remote, message='', commit=false, include_database_yaml=false) |
| 7 |
ignore(".", ".DS_Store") |
| 8 |
ignore("db", "*.sqlite3") |
| 9 |
ignore("log", "*.log") |
| 10 |
ignore("tmp", "[^.]*") |
| 11 |
ignore("public/cache", "[^.]*") |
| 12 |
ignore("doc", ["api","app"]) |
| 13 |
|
| 14 |
unless include_database_yaml then |
| 15 |
ignore("config", "database.yml") |
| 16 |
end |
| 17 |
|
| 18 |
unless sqlite_by_default then |
| 19 |
sqlite = File.new("config/database.sqlite.yml", "w") |
| 20 |
sqlite << "development:\n" |
| 21 |
sqlite << " adapter: sqlite3\n" |
| 22 |
sqlite << " dbfile: db/development.sqlite3\n" |
| 23 |
sqlite << " timeout: 5000\n\n" |
| 24 |
sqlite << "test:\n" |
| 25 |
sqlite << " adapter: sqlite3\n" |
| 26 |
sqlite << " dbfile: db/test.sqlite3\n" |
| 27 |
sqlite << " timeout: 5000\n\n" |
| 28 |
sqlite << "production:\n" |
| 29 |
sqlite << " adapter: sqlite3\n" |
| 30 |
sqlite << " dbfile: db/production.sqlite3\n" |
| 31 |
sqlite << " timeout: 5000\n\n" |
| 32 |
sqlite.close |
| 33 |
if File.exists?("config/database.yml") |
| 34 |
FileUtils.mv("config/database.yml", "config/database.mysql.yml") |
| 35 |
end |
| 36 |
FileUtils.ln_sf("database.sqlite.yml", "config/database.yml") |
| 37 |
end |
| 38 |
|
| 39 |
git = GitRails::Git.new |
| 40 |
git.init |
| 41 |
git.add(".") |
| 42 |
git.commit_all('', message ? {"-m" => "\"#{message}\""} : {}) if commit |
| 43 |
if (remote) |
| 44 |
config = File.new(".git/config", "a") |
| 45 |
config << "[remote \"origin\"]\n" |
| 46 |
config << " url = #{remote}\n" |
| 47 |
config << " fetch = +refs/heads/*:refs/remotes/origin/*\n" |
| 48 |
config << "[branch \"master\"]\n" |
| 49 |
config << " remote = origin\n" |
| 50 |
config << " merge = refs/heads/master\n" |
| 51 |
config.close |
| 52 |
puts "You can now push to the origin #{remote} by using:\n" |
| 53 |
puts " git push origin master\n" |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
private |
| 58 |
def ignore(path, entries) |
| 59 |
file = path + "/.gitignore" |
| 60 |
unless File.exists?(file) |
| 61 |
FileUtils.mkdir_p(path) |
| 62 |
handle = File.new(file, "w") |
| 63 |
entries.each do |entry| |
| 64 |
handle << "#{entry}\n" |
| 65 |
end |
| 66 |
handle.close |
| 67 |
end |
| 68 |
end |
| 69 |
|
| 70 |
def sqlite_by_default |
| 71 |
begin |
| 72 |
require 'config/boot.rb' |
| 73 |
Rails::VERSION::STRING >= "2.0.2" |
| 74 |
rescue LoadError |
| 75 |
true |
| 76 |
end |
| 77 |
end |
| 78 |
end |
| 79 |
end |
| 80 |
end |