2011-07-14 19:50:51 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'thor'
|
2011-08-18 17:09:49 +00:00
|
|
|
require 'penchant'
|
2011-09-09 18:15:38 +00:00
|
|
|
require 'fileutils'
|
2011-07-14 19:50:51 +00:00
|
|
|
|
2011-08-18 17:09:49 +00:00
|
|
|
class PenchantCLI < Thor
|
2011-07-14 19:50:51 +00:00
|
|
|
include Thor::Actions
|
|
|
|
source_root File.expand_path('../..', __FILE__)
|
|
|
|
|
2012-07-23 14:25:42 +00:00
|
|
|
SCRIPT_DIR = 'script'
|
|
|
|
CLONE_DIR = '..'
|
|
|
|
|
2011-07-14 19:50:51 +00:00
|
|
|
desc "install", "Copy the common scripts to the project"
|
2012-07-23 14:25:42 +00:00
|
|
|
method_options :dir => SCRIPT_DIR
|
2011-07-14 19:50:51 +00:00
|
|
|
def install
|
|
|
|
directory 'template/script', options[:dir]
|
|
|
|
Dir[File.join(options[:dir], '**/*')].each { |file| File.chmod(0755, file) }
|
2011-10-13 18:27:02 +00:00
|
|
|
|
2012-07-23 14:25:42 +00:00
|
|
|
if File.directory?('.git')
|
2012-07-25 12:50:36 +00:00
|
|
|
Penchant::Hooks.install!
|
2012-07-23 14:25:42 +00:00
|
|
|
else
|
|
|
|
puts "No git repository detected here. Skipping git hook installation..."
|
2011-10-13 18:27:02 +00:00
|
|
|
end
|
2012-08-21 14:43:01 +00:00
|
|
|
|
|
|
|
if !File.file?('Gemfile') && !File.file?('Gemfile.penchant')
|
|
|
|
FileUtils.touch('Gemfile.penchant')
|
|
|
|
|
|
|
|
prepend_to_file 'Gemfile.penchant', <<-RB
|
|
|
|
source :rubygems
|
|
|
|
RB
|
|
|
|
|
|
|
|
install_gemfile_penchant
|
|
|
|
end
|
2011-07-14 19:50:51 +00:00
|
|
|
end
|
2011-08-18 17:09:49 +00:00
|
|
|
|
2012-04-20 15:12:56 +00:00
|
|
|
desc "update", "Update the installed scripts"
|
2012-07-23 14:25:42 +00:00
|
|
|
method_options :dir => SCRIPT_DIR
|
2012-04-20 15:12:56 +00:00
|
|
|
def update
|
|
|
|
install
|
|
|
|
end
|
|
|
|
|
2011-08-18 21:37:54 +00:00
|
|
|
desc "convert", "Make an existing project Penchant-isized"
|
2012-07-23 14:25:42 +00:00
|
|
|
method_options :dir => SCRIPT_DIR
|
2011-08-18 21:37:54 +00:00
|
|
|
def convert
|
|
|
|
install
|
2012-06-05 19:29:27 +00:00
|
|
|
FileUtils.mv 'Gemfile', 'Gemfile.penchant'
|
2012-08-21 14:43:01 +00:00
|
|
|
install_gemfile_penchant
|
2011-08-18 21:37:54 +00:00
|
|
|
end
|
|
|
|
|
2011-10-13 14:01:18 +00:00
|
|
|
method_options :deployment => false
|
2012-01-09 15:48:47 +00:00
|
|
|
method_options :switch_back => false
|
2012-08-07 14:55:55 +00:00
|
|
|
method_options :no_auto_update => false
|
2011-08-19 17:57:11 +00:00
|
|
|
desc "gemfile ENV", "Switch the gemfile environment, or rebuild the current environment if not given"
|
|
|
|
def gemfile(env = get_current_env)
|
2012-07-24 14:48:11 +00:00
|
|
|
check_git_hooks!
|
|
|
|
|
2011-08-19 18:01:34 +00:00
|
|
|
if env
|
2012-01-09 15:48:47 +00:00
|
|
|
if options[:switch_back]
|
|
|
|
puts "[penchant] Switching back, fallback: #{env}..."
|
|
|
|
|
|
|
|
Penchant::Gemfile.switch_back!(env)
|
|
|
|
else
|
|
|
|
puts "[penchant] Rebunding for #{env} environment#{options[:deployment] ? ", deployment mode" : ''}..."
|
|
|
|
|
|
|
|
Penchant::Gemfile.do_full_env_switch!(env, options[:deployment])
|
|
|
|
end
|
2011-08-19 18:01:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
gemfile = Penchant::Gemfile.new
|
|
|
|
if !gemfile.has_gemfile?
|
2012-06-05 19:29:27 +00:00
|
|
|
puts "No Gemfile or Gemfile.penchant, exiting."
|
2011-08-19 18:01:34 +00:00
|
|
|
exit 1
|
|
|
|
end
|
2012-08-07 14:55:55 +00:00
|
|
|
|
2011-08-19 18:01:34 +00:00
|
|
|
system %{bundle}
|
2012-08-07 14:55:55 +00:00
|
|
|
|
|
|
|
# it's asking for bundle update, we know what we're doing
|
|
|
|
if $?.exitstatus == 6 and !options[:no_auto_update]
|
|
|
|
system %{bundle update}
|
|
|
|
end
|
2011-08-18 17:09:49 +00:00
|
|
|
end
|
2011-08-18 17:22:01 +00:00
|
|
|
|
|
|
|
desc "gemfile-env", "Get the gemfile environment"
|
|
|
|
def gemfile_env
|
2011-08-19 18:04:13 +00:00
|
|
|
puts get_current_env
|
2011-08-19 17:57:11 +00:00
|
|
|
end
|
|
|
|
|
2012-07-23 14:25:42 +00:00
|
|
|
desc "bootstrap [DIR = #{CLONE_DIR}]", "Download all referred-to git repos to the specified directory"
|
|
|
|
def bootstrap(dir = CLONE_DIR)
|
2012-06-07 14:48:27 +00:00
|
|
|
Penchant::Gemfile.defined_git_repos.each do |repo|
|
2012-07-23 14:25:42 +00:00
|
|
|
puts "Cloning #{repo} to #{dir}"
|
2012-06-07 14:48:27 +00:00
|
|
|
repo.clone_to(dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args)
|
|
|
|
if Penchant::Gemfile.available_environments.include?(method)
|
|
|
|
gemfile(method, *args)
|
|
|
|
else
|
|
|
|
super(method, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-19 17:57:11 +00:00
|
|
|
no_tasks do
|
|
|
|
def get_current_env
|
|
|
|
gemfile = Penchant::Gemfile.new
|
2011-10-13 18:27:02 +00:00
|
|
|
out = [ gemfile.environment ]
|
|
|
|
out << "deployment" if gemfile.deployment?
|
|
|
|
out.join(' ')
|
2011-08-19 17:57:11 +00:00
|
|
|
end
|
2012-07-24 14:48:11 +00:00
|
|
|
|
|
|
|
def check_git_hooks!
|
|
|
|
if !Penchant::Hooks.installed?
|
|
|
|
puts "[penchant] git hooks not installed. Run script/install-git-hooks."
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
end
|
2012-08-21 14:43:01 +00:00
|
|
|
|
|
|
|
def install_gemfile_penchant
|
|
|
|
prepend_to_file 'Gemfile.penchant', <<-RB
|
|
|
|
# ensure git hooks are always installed
|
|
|
|
ensure_git_hooks!
|
|
|
|
|
|
|
|
# everything in the :local env is assumed to be a sibling directory of this one
|
|
|
|
defaults_for env(:local), :path => '../%s'
|
|
|
|
|
|
|
|
# reference a github repository with gem 'my-gem', :github => 'username'
|
|
|
|
property :github, :git => 'git://github.com/$1/%s.git'
|
|
|
|
RB
|
|
|
|
gemfile(:remote)
|
|
|
|
end
|
2011-08-18 17:22:01 +00:00
|
|
|
end
|
2011-09-09 18:15:38 +00:00
|
|
|
|
|
|
|
default_task :gemfile
|
2011-07-14 19:50:51 +00:00
|
|
|
end
|
|
|
|
|
2011-08-18 17:09:49 +00:00
|
|
|
PenchantCLI.start
|
2012-07-24 14:48:11 +00:00
|
|
|
|