require 'railsonfire/helpers/base' require 'railsonfire/helpers/key' require 'railsonfire/helpers/constants' require 'active_support/core_ext/hash/deep_merge' class CLI < BASECLI def self.source_root File.dirname(__FILE__) end desc "create", "Create a new Railsonfire project" def create invoke "auth:login" send_signal :create_started return unless is_git_managed? if git_remotes.empty? say "We couldn't find any GitHub remotes. Please add your GitHub remote with git remote add YOUR_GITHUB_REMOTE and start again." return false end remote = ask_for_git_remote set_project_data remote if yes? "Create Project (y/n)?" and project.create if(is_github_remote? remote) send_signal :configure_github configure_github else send_signal :create_post_receive invoke "post_receive" end configure true else send_signal :create_cancelled false end end private def project Railsonfire.project end def ask_for_git_remote return git_remotes[0] if git_remotes.size == 1 select "Select the repository you store your code in and we should clone from:", git_remotes end def git_remotes Git.open("./").remotes.collect(&:url).select{|u| is_github_remote? u} end def set_project_data remote if(is_github_remote? remote) project.name = github_repo_name_from remote project.owner = github_repo_owner_from remote send_signal :create_with_github else project.url = remote project.name = ask "Name of your Project:" send_signal :create_with_generic_git end say "Owner: #{project.owner}" if project.owner say "Name: #{project.name}" say "Repository: #{project.url}" if project.url end def is_github_remote? remote remote["github.com"] != nil end def github_repo_name_from remote if remote.start_with?("https") remote.split("/")[4].split(".")[0] else split_git_remote(remote).last end end def github_repo_owner_from remote if remote.start_with?("https") remote.split("/")[3] else split_git_remote(remote).first end end def split_git_remote remote remote.sub("git@github.com:", "").sub(".git", "").split("/") end def configure_github invoke "github:upload_key" invoke "github:add_hook" end def configure merge_new_project_config configure_heroku write_config_file end def merge_new_project_config new = { "id" => Railsonfire.project.uuid, "setup" => { "commands" => ["bundle install", "bundle exec rake db:schema:load"] }, "test" => { "commands" => ["bundle exec rake"] }, "deploy" => { }, "ruby_versions" => [RUBY_VERSION] } Railsonfire.config.deep_merge!(new) end def configure_heroku invoke "railsonfire:heroku:configure" if yes? "Do you want to configure deployment to Heroku (y/n)?" end end